Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"ext-intl": "*"
},
"require-dev": {
"mediawiki/mediawiki-codesniffer": "45.0.0",
"mediawiki/mediawiki-codesniffer": "46.0.0",
"mediawiki/minus-x": "1.1.3",
"php-parallel-lint/php-console-highlighter": "1.0.0",
"php-parallel-lint/php-parallel-lint": "1.4.0",
Expand Down
34 changes: 19 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function __toString(): string {

$onlyContainsHttpCodepoints = Utf8Utils::onlyContains(
$value,
fn ( string $s ) => Utf8Utils::isHttpTokenCodepoint( $s ) );
static fn ( string $s ) => Utf8Utils::isHttpTokenCodepoint( $s ) );

if ( $value === '' || !$onlyContainsHttpCodepoints ) {
$serializedValue = $this->serializeParameterValue( $value );
Expand Down
21 changes: 11 additions & 10 deletions src/MediaTypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public function parseOrThrow( string|null $s ): MediaType {
private function collectType( string $s, int $length, int &$position ): string {
$type = Utf8Utils::collectCodepoints(
$s, $position,
fn ( string $c ) => $c !== Token::Slash->value
static fn ( string $c ) => $c !== Token::Slash->value
);

if ( $type === '' ) {
throw new MediaTypeParserException( "type: is empty" );
}

$onlyContainsHttpCodepoints = Utf8Utils::onlyContains(
$type, fn ( string $c ) => Utf8Utils::isHttpTokenCodepoint( $c ) );
$type, static fn ( string $c ) => Utf8Utils::isHttpTokenCodepoint( $c ) );
if ( !$onlyContainsHttpCodepoints ) {
throw new MediaTypeParserException( "type: should only contain HTTP codepoints" );
}
Expand All @@ -82,7 +82,7 @@ private function collectType( string $s, int $length, int &$position ): string {
private function collectSubType( string $s, int &$position ): string {
$subType = Utf8Utils::collectCodepoints(
$s, $position,
fn ( string $c ) => $c !== Token::Semicolon->value
static fn ( string $c ) => $c !== Token::Semicolon->value
);
$subType = Utf8Utils::trimHttpWhitespace( $subType );

Expand All @@ -91,7 +91,7 @@ private function collectSubType( string $s, int &$position ): string {
}

$onlyContainsHttpCodepoints = Utf8Utils::onlyContains(
$subType, fn ( string $c ) => Utf8Utils::isHttpTokenCodepoint( $c ) );
$subType, static fn ( string $c ) => Utf8Utils::isHttpTokenCodepoint( $c ) );
if ( !$onlyContainsHttpCodepoints ) {
throw new MediaTypeParserException( "subtype: should only contain HTTP codepoints" );
}
Expand All @@ -110,13 +110,13 @@ private function collectParameters( string $s, int $length, int &$position ): ar
// skip whitespace
Utf8Utils::collectCodepoints(
$s, $position,
fn ( string $c ) => Utf8Utils::isHttpWhitespace( $c )
static fn ( string $c ) => Utf8Utils::isHttpWhitespace( $c )
);

// collect parameter name
$parameterName = Utf8Utils::collectCodepoints(
$s, $position,
fn ( string $c ) => $c !== Token::Semicolon->value && $c !== Token::Equal->value
static fn ( string $c ) => $c !== Token::Semicolon->value && $c !== Token::Equal->value
);
$parameterName = \strtolower( $parameterName );

Expand All @@ -135,10 +135,11 @@ private function collectParameters( string $s, int $length, int &$position ): ar
$parameterValue = null;
if ( $s[$position] === '"' ) {
$parameterValue = Utf8Utils::collectHttpQuotedString( $s, $position, true );
Utf8Utils::collectCodepoints( $s, $position, fn ( string $c ) => $c !== Token::Semicolon->value );
Utf8Utils::collectCodepoints( $s, $position,
static fn ( string $c ) => $c !== Token::Semicolon->value );
} else {
$parameterValue = Utf8Utils::collectCodepoints( $s, $position,
fn ( string $c ) => $c !== Token::Semicolon->value );
static fn ( string $c ) => $c !== Token::Semicolon->value );
$parameterValue = Utf8Utils::trimHttpWhitespace( $parameterValue );

if ( $parameterValue === '' ) {
Expand All @@ -150,9 +151,9 @@ private function collectParameters( string $s, int $length, int &$position ): ar
if (
$parameterName !== ''
&& Utf8Utils::onlyContains( $parameterName,
fn ( string $c ) => Utf8Utils::isHttpTokenCodepoint( $c ) )
static fn ( string $c ) => Utf8Utils::isHttpTokenCodepoint( $c ) )
&& Utf8Utils::onlyContains( $parameterValue,
fn ( string $c ) => Utf8Utils::isHttpQuotedStringTokenCodepoint( $c ) )
static fn ( string $c ) => Utf8Utils::isHttpQuotedStringTokenCodepoint( $c ) )
&& !\array_key_exists( $parameterName, $parameters )
) {
$parameters[$parameterName] = $parameterValue;
Expand Down
8 changes: 4 additions & 4 deletions src/Utf8Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ public static function collectHttpQuotedString(
$positionStart = $position;
$value = '';

$expectedQuote = fn ( string $input, int $position, string $value ) =>
$expectedQuote = static fn ( string $input, int $position, string $value ) =>
"Codepoint in \"{$input}\" at position {$position} is {$value}, expected U+0022 (\")";
Assert::invariant(
fn () => $input[$position] === '"',
static fn () => $input[$position] === '"',
$expectedQuote( $input, $position, $input[$position] ) );

$position++;

while ( true ) {
$value .= self::collectCodepoints(
$input, $position,
fn ( string $c ) => $c !== '"' && $c !== '\\' );
static fn ( string $c ) => $c !== '"' && $c !== '\\' );

if ( $position >= \strlen( $input ) ) {
break;
Expand All @@ -90,7 +90,7 @@ public static function collectHttpQuotedString(
$position++;
} else {
Assert::invariant(
fn () => $quoteOrBackslash === '"',
static fn () => $quoteOrBackslash === '"',
$expectedQuote( $input, $position, $quoteOrBackslash ) );
break;
}
Expand Down
22 changes: 11 additions & 11 deletions tests/Utf8UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ public function testIsHttpWhitespace(

public static function provideOnlyContains(): array {
return [
[ '', fn ( string $c ) => \ctype_alpha( $c ), true ],
[ 'test', fn ( string $c ) => \ctype_alpha( $c ), true ],
[ '1234', fn ( string $c ) => \ctype_digit( $c ), true ],
[ '1234test', fn ( string $c ) => \ctype_digit( $c ), false ],
[ 'test1234', fn ( string $c ) => \ctype_alpha( $c ), false ],
[ 'test1234', fn ( string $c ) => \ctype_alnum( $c ), true ],
[ '', static fn ( string $c ) => \ctype_alpha( $c ), true ],
[ 'test', static fn ( string $c ) => \ctype_alpha( $c ), true ],
[ '1234', static fn ( string $c ) => \ctype_digit( $c ), true ],
[ '1234test', static fn ( string $c ) => \ctype_digit( $c ), false ],
[ 'test1234', static fn ( string $c ) => \ctype_alpha( $c ), false ],
[ 'test1234', static fn ( string $c ) => \ctype_alnum( $c ), true ],
];
}

Expand Down Expand Up @@ -151,11 +151,11 @@ public static function provideCollectHttpQuotedString(): array {

public static function provideCollectCodepoints(): array {
return [
[ '', 0, '', 0, fn () => true ],
[ '1234test', 0, '1234', 4, fn ( string $s ) => \ctype_digit( $s ) ],
[ 'test1234', 0, 'test', 4, fn ( string $s ) => \ctype_alpha( $s ) ],
[ 'foo/bar', 0, 'foo', 3, fn ( string $s ) => $s !== '/' ],
[ 'foo/bar;', 0, 'foo/bar', 7, fn ( string $s ) => $s !== ';' ],
[ '', 0, '', 0, static fn () => true ],
[ '1234test', 0, '1234', 4, static fn ( string $s ) => \ctype_digit( $s ) ],
[ 'test1234', 0, 'test', 4, static fn ( string $s ) => \ctype_alpha( $s ) ],
[ 'foo/bar', 0, 'foo', 3, static fn ( string $s ) => $s !== '/' ],
[ 'foo/bar;', 0, 'foo/bar', 7, static fn ( string $s ) => $s !== ';' ],
];
}

Expand Down