Skip to content

Commit 2fd7bfe

Browse files
committed
next() dynamic return type extension also valid for prev()
1 parent a056d34 commit 2fd7bfe

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Type/Php/ArrayNextDynamicReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ArrayNextDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFuncti
1515

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

2121
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type

tests/PHPStan/Analyser/data/array-next.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ public function doBaz(array $a)
3131

3232
}
3333

34+
class Foo2
35+
{
36+
37+
public function doFoo()
38+
{
39+
$array = [];
40+
assertType('false', prev($array));
41+
}
42+
43+
/**
44+
* @param int[] $a
45+
*/
46+
public function doBar(array $a)
47+
{
48+
assertType('int|false', prev($a));
49+
}
50+
51+
/**
52+
* @param non-empty-array<int, string> $a
53+
*/
54+
public function doBaz(array $a)
55+
{
56+
assertType('string|false', prev($a));
57+
}
58+
59+
}
60+
3461
interface HttpClientPoolItem
3562
{
3663
public function isDisabled(): bool;

0 commit comments

Comments
 (0)