Skip to content

Commit

Permalink
next() dynamic return type extension also valid for prev()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 12, 2021
1 parent a056d34 commit 2fd7bfe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Type/Php/ArrayNextDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ArrayNextDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFuncti

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'next';
return in_array($functionReflection->getName(), ['next', 'prev'], true);
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/data/array-next.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ public function doBaz(array $a)

}

class Foo2
{

public function doFoo()
{
$array = [];
assertType('false', prev($array));
}

/**
* @param int[] $a
*/
public function doBar(array $a)
{
assertType('int|false', prev($a));
}

/**
* @param non-empty-array<int, string> $a
*/
public function doBaz(array $a)
{
assertType('string|false', prev($a));
}

}

interface HttpClientPoolItem
{
public function isDisabled(): bool;
Expand Down

0 comments on commit 2fd7bfe

Please sign in to comment.