Skip to content

Commit b18c7e0

Browse files
committed
Make ArrayFindFunctionReturnTypeExtension a wrapper for array_filter()
1 parent be71b31 commit b18c7e0

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/Type/Php/ArrayFindFunctionReturnTypeExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace PHPStan\Type\Php;
44

55
use PhpParser\Node\Expr\FuncCall;
6+
use PhpParser\Node\Name;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Reflection\FunctionReflection;
89
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
910
use PHPStan\Type\NullType;
1011
use PHPStan\Type\Type;
1112
use PHPStan\Type\TypeCombinator;
13+
use function array_map;
1214
use function count;
1315

1416
final class ArrayFindFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
@@ -30,7 +32,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3032
return null;
3133
}
3234

33-
return TypeCombinator::union($arrayType->getIterableValueType(), new NullType());
35+
$resultTypes = $scope->getType(new FuncCall(new Name('\array_filter'), $functionCall->getArgs()))->getArrays();
36+
37+
return TypeCombinator::union(new NullType(), ...array_map(fn ($type) => $type->getIterableValueType(), $resultTypes));
3438
}
3539

3640
}

tests/PHPStan/Analyser/nsrt/array-find.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function array_find(array $array, callable $callback)
2828
function testMixed(array $array, callable $callback): void
2929
{
3030
assertType('mixed', array_find($array, $callback));
31-
assertType('mixed', array_find($array, 'is_int'));
31+
assertType('int|null', array_find($array, 'is_int'));
3232
}
3333

3434
/**
@@ -37,7 +37,7 @@ function testMixed(array $array, callable $callback): void
3737
function testConstant(array $array, callable $callback): void
3838
{
3939
assertType("1|'foo'|DateTime|null", array_find($array, $callback));
40-
assertType("1|'foo'|DateTime|null", array_find($array, 'is_int'));
40+
assertType("1|null", array_find($array, 'is_int'));
4141
}
4242

4343
}

0 commit comments

Comments
 (0)