-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
$firstKeyFromTableFilterByTrue()
with match(true)
expr …
…like `switch(true)` stmt and merge `is_int($cursor)` branch with the default one as they yield the same `''` @ `encodeNextCursor()` * remove the branch for `$prefix === null` as it had `?? ''` before * now testing `$cursor` against `'0'` instead of `$prefix` as it would be `''` for cursor `0` @ `decodeCursor()` @ `App\Http\PostsQuery\BaseQuery` * covering some of escaped mutations @ `Tests\Feature\App\Http\PostsQuery\BaseQuery` * ignore mutation `MethodCallRemoval` on any `$this->debugbar->` @ infection.json5 @ be
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,20 +126,16 @@ public function encodeNextCursor(Collection $postsKeyByTypePluralName): string | |
]) | ||
->map(static fn(array $cursors) => collect($cursors) | ||
->map(static function (int|string $cursor): string { | ||
if (\is_int($cursor) && $cursor === 0) { | ||
// quick exit to keep 0 as is | ||
if ($cursor === 0) { // quick exit to keep 0 as is | ||
// to prevent packed 0 with the default format 'P' after 0x00 trimming is an empty string | ||
// that will be confused with post types without a cursor that is a blank encoded cursor ',,' | ||
return '0'; | ||
} | ||
|
||
$firstKeyFromTableFilterByTrue = static fn(array $table, string $default): string => | ||
array_keys(array_filter($table, static fn(bool $f) => $f === true))[0] ?? $default; | ||
$prefix = $firstKeyFromTableFilterByTrue([ | ||
'-' => \is_int($cursor) && $cursor < 0, | ||
'' => \is_int($cursor), | ||
'S' => \is_string($cursor), | ||
], ''); | ||
$prefix = match (true) { | ||
\is_int($cursor) && $cursor < 0 => '-', | ||
Check warning on line 135 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (ubuntu-latest) / phpunit-infection
Check warning on line 135 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (windows-latest) / phpunit-infection
Check warning on line 135 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (macos-latest) / phpunit-infection
|
||
\is_string($cursor) => 'S', | ||
default => '', | ||
}; | ||
|
||
$value = \is_int($cursor) | ||
// remove trailing 0x00 for an unsigned int or 0xFF for a signed negative int | ||
|
@@ -174,25 +170,27 @@ public function decodeCursor(string $encodedCursors): Collection | |
->combine(Str::of($encodedCursors) | ||
->explode(',') | ||
->map(static function (string $encodedCursor): int|string|null { | ||
/** | ||
* @var string $cursor | ||
* @var string $prefix | ||
*/ | ||
[$prefix, $cursor] = array_pad(explode(':', $encodedCursor), 2, null); | ||
Check warning on line 177 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (ubuntu-latest) / phpunit-infection
Check warning on line 177 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (windows-latest) / phpunit-infection
Check warning on line 177 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (macos-latest) / phpunit-infection
|
||
if ($cursor === null) { // no prefix being provided means the value of cursor is a positive int | ||
$cursor = $prefix; | ||
$prefix = ''; | ||
} | ||
return match ($prefix) { | ||
null => null, // original encoded cursor is an empty string | ||
'0' => 0, // keep 0 as is | ||
return $cursor === '0' ? 0 : match ($prefix) { // keep 0 as is | ||
'S' => $cursor, // string literal is not base64 encoded | ||
default => ((array) ( | ||
Check warning on line 184 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (ubuntu-latest) / phpunit-infection
Check warning on line 184 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (windows-latest) / phpunit-infection
Check warning on line 184 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (macos-latest) / phpunit-infection
|
||
unpack( | ||
'P', | ||
str_pad( // re-add removed trailing 0x00 or 0xFF | ||
format: 'P', | ||
string: str_pad( // re-add removed trailing 0x00 or 0xFF | ||
base64_decode( | ||
// https://en.wikipedia.org/wiki/Base64#URL_applications | ||
str_replace(['-', '_'], ['+', '/'], $cursor), | ||
), | ||
8, | ||
$prefix === '-' ? "\xFF" : "\x00", | ||
length: 8, | ||
Check warning on line 192 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (ubuntu-latest) / phpunit-infection
Check warning on line 192 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (windows-latest) / phpunit-infection
Check warning on line 192 in be/app/Http/PostsQuery/BaseQuery.php GitHub Actions / runs-on (macos-latest) / phpunit-infection
|
||
pad_string: $prefix === '-' ? "\xFF" : "\x00", | ||
), | ||
) | ||
))[1], // the returned array of unpack() will starts index from 1 | ||
|