From 843c9c47e26afb4b5bbd437f744fc8c2fcec6517 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 26 Apr 2022 16:45:47 +0200 Subject: [PATCH] SA: PHPStan 1.6.0 upgrade. PHPStan 1.6.0 now understand conditional returns. This version let us remove `@phpstan-ignore-next-line` and fix a couple of minor things. --- src/Contract/Operation/Allable.php | 1 - src/Operation/All.php | 2 +- src/Operation/Current.php | 4 +--- tests/static-analysis/all.php | 6 ------ tests/static-analysis/first.php | 6 +++--- tests/static-analysis/head.php | 6 +++--- tests/static-analysis/last.php | 6 +++--- 7 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/Contract/Operation/Allable.php b/src/Contract/Operation/Allable.php index dbf7143f1..c8b779a3d 100644 --- a/src/Contract/Operation/Allable.php +++ b/src/Contract/Operation/Allable.php @@ -22,7 +22,6 @@ interface Allable * * @param bool $normalize * - * @return list|array * @psalm-return ($normalize is true ? list : array) */ public function all(bool $normalize = true): array; diff --git a/src/Operation/All.php b/src/Operation/All.php index aa186b24a..b2d26ffd1 100644 --- a/src/Operation/All.php +++ b/src/Operation/All.php @@ -29,7 +29,7 @@ public function __invoke(): Closure { return /** - * @return Closure(iterable): iterable|iterable + * @return Closure(iterable): Generator|Generator */ static fn (bool $normalize): Closure => /** diff --git a/src/Operation/Current.php b/src/Operation/Current.php index 250ce35c9..04d3d340d 100644 --- a/src/Operation/Current.php +++ b/src/Operation/Current.php @@ -23,14 +23,12 @@ final class Current extends AbstractOperation /** * @template V * - * @return Closure(TKey): Closure(V): Closure(iterable): Generator + * @return Closure(int): Closure(V): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param TKey $index - * * @return Closure(V): Closure(iterable): Generator */ static fn (int $index): Closure => diff --git a/tests/static-analysis/all.php b/tests/static-analysis/all.php index e52fea6bc..a4ead9535 100644 --- a/tests/static-analysis/all.php +++ b/tests/static-analysis/all.php @@ -41,14 +41,8 @@ function all_checkMixed(array $array): void /** @psalm-suppress InvalidScalarArgument @phpstan-ignore-next-line */ all_checkList(Collection::fromIterable(['foo' => 1, 'bar' => 2])->all(false)); -// PHPStan limitation -> does not support conditional return types, but Psalm does -/** @phpstan-ignore-next-line */ all_checkMap(Collection::fromIterable(['foo' => 1, 'bar' => 2])->all(false)); - -// Limitation with `empty` and PHPStan -> uses `mixed` typing -/** @phpstan-ignore-next-line */ all_checkList(Collection::empty()->all()); /** @phpstan-ignore-next-line */ all_checkMap(Collection::empty()->all(false)); -/** @phpstan-ignore-next-line */ all_checkMixed(Collection::empty()->all()); diff --git a/tests/static-analysis/first.php b/tests/static-analysis/first.php index f9a1fec37..640f6fe52 100644 --- a/tests/static-analysis/first.php +++ b/tests/static-analysis/first.php @@ -49,8 +49,8 @@ function first_checkNullableString(?string $value): void // This retrieval method doesn't cause static analysis complaints // but is not always reliable because of that. first_checkIntElement(Collection::fromIterable([1, 2, 3])->first()->all()[0]); -first_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->first()->all()['foo']); -first_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->first()->all()['baz']); +first_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->first()->all(false)['foo']); +first_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->first()->all(false)['baz']); // VALID failures - `current` returns T|null /** @psalm-suppress NullArgument @phpstan-ignore-next-line */ @@ -61,5 +61,5 @@ function first_checkNullableString(?string $value): void // VALID failures - these keys don't exist /** @psalm-suppress InvalidArrayOffset */ first_checkIntElement(Collection::fromIterable([1, 2, 3])->first()->all(false)[4]); -/** @psalm-suppress InvalidArrayOffset */ +/** @psalm-suppress InvalidArrayOffset @phpstan-ignore-next-line */ first_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->first()->all(false)[0]); diff --git a/tests/static-analysis/head.php b/tests/static-analysis/head.php index c4d7a80b0..e4571ba7f 100644 --- a/tests/static-analysis/head.php +++ b/tests/static-analysis/head.php @@ -49,8 +49,8 @@ function head_checkNullableString(?string $value): void // This retrieval method doesn't cause static analysis complaints // but is not always reliable because of that. head_checkIntElement(Collection::fromIterable([1, 2, 3])->head()->all()[0]); -head_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->head()->all()['foo']); -head_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->head()->all()['baz']); +head_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->head()->all(false)['foo']); +head_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->head()->all(false)['baz']); // VALID failures - `current` returns T|null /** @psalm-suppress NullArgument @phpstan-ignore-next-line */ @@ -61,5 +61,5 @@ function head_checkNullableString(?string $value): void // VALID failures - these keys don't exist /** @psalm-suppress InvalidArrayOffset */ head_checkIntElement(Collection::fromIterable([1, 2, 3])->head()->all(false)[4]); -/** @psalm-suppress InvalidArrayOffset */ +/** @psalm-suppress InvalidArrayOffset @phpstan-ignore-next-line */ head_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->head()->all(false)[0]); diff --git a/tests/static-analysis/last.php b/tests/static-analysis/last.php index 928750fb1..5ef3acf3c 100644 --- a/tests/static-analysis/last.php +++ b/tests/static-analysis/last.php @@ -49,8 +49,8 @@ function last_checkNullableString(?string $value): void // This retrieval method doesn't cause static analysis complaints // but is not always reliable because of that. last_checkIntElement(Collection::fromIterable([1, 2, 3])->last()->all()[0]); -last_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->last()->all()['foo']); -last_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->last()->all()['baz']); +last_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->last()->all(false)['foo']); +last_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->last()->all(false)['baz']); // VALID failures - `current` returns T|null /** @psalm-suppress NullArgument @phpstan-ignore-next-line */ @@ -61,5 +61,5 @@ function last_checkNullableString(?string $value): void // VALID failures - these keys don't exist /** @psalm-suppress InvalidArrayOffset */ last_checkIntElement(Collection::fromIterable([1, 2, 3])->last()->all(false)[4]); -/** @psalm-suppress InvalidArrayOffset */ +/** @psalm-suppress InvalidArrayOffset @phpstan-ignore-next-line */ last_checkStringElement(Collection::fromIterable(['foo' => 'bar', 'baz' => 'bar'])->last()->all(false)[0]);