Skip to content

Commit

Permalink
SA: PHPStan 1.6.0 upgrade.
Browse files Browse the repository at this point in the history
PHPStan 1.6.0 now understand conditional returns.
This version let us remove `@phpstan-ignore-next-line` and fix a couple of minor things.
  • Loading branch information
drupol committed Apr 26, 2022
1 parent 545bd60 commit 843c9c4
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/Contract/Operation/Allable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ interface Allable
*
* @param bool $normalize
*
* @return list<T>|array<TKey, T>
* @psalm-return ($normalize is true ? list<T> : array<TKey, T>)
*/
public function all(bool $normalize = true): array;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(): Closure
{
return
/**
* @return Closure(iterable<TKey, T>): iterable<int, T>|iterable<TKey, T>
* @return Closure(iterable<TKey, T>): Generator<int, T>|Generator<TKey, T>
*/
static fn (bool $normalize): Closure =>
/**
Expand Down
4 changes: 1 addition & 3 deletions src/Operation/Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ final class Current extends AbstractOperation
/**
* @template V
*
* @return Closure(TKey): Closure(V): Closure(iterable<TKey, T>): Generator<int, T|V>
* @return Closure(int): Closure(V): Closure(iterable<TKey, T>): Generator<int, T|V>
*/
public function __invoke(): Closure
{
return
/**
* @param TKey $index
*
* @return Closure(V): Closure(iterable<TKey, T>): Generator<int, T|V>
*/
static fn (int $index): Closure =>
Expand Down
6 changes: 0 additions & 6 deletions tests/static-analysis/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
6 changes: 3 additions & 3 deletions tests/static-analysis/first.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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]);
6 changes: 3 additions & 3 deletions tests/static-analysis/head.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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]);
6 changes: 3 additions & 3 deletions tests/static-analysis/last.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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]);

0 comments on commit 843c9c4

Please sign in to comment.