diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php index 87dba6408..34834617b 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php @@ -71,7 +71,7 @@ class ArrayFilterReturnTypeProvider implements \Psalm\Plugin\EventHandler\Functi $new_properties = \array_filter( array_map( - function ($keyed_type) use ($statements_source, $context) { + static function ($keyed_type) use ($statements_source, $context) { $prev_keyed_type = $keyed_type; $keyed_type = \Psalm\Internal\Type\AssertionReconciler::reconcile( @@ -85,16 +85,13 @@ class ArrayFilterReturnTypeProvider implements \Psalm\Plugin\EventHandler\Functi $statements_source->getSuppressedIssues() ); - $keyed_type->possibly_undefined = ($prev_keyed_type->hasInt() - && !$prev_keyed_type->hasLiteralInt()) - || $prev_keyed_type->hasFloat() - || $prev_keyed_type->getId() !== $keyed_type->getId(); + $keyed_type->possibly_undefined = !$prev_keyed_type->isAlwaysTruthy(); return $keyed_type; }, $first_arg_array->properties ), - function ($keyed_type) { + static function ($keyed_type) { return !$keyed_type->isEmpty(); } ); diff --git a/tests/ArrayFunctionCallTest.php b/tests/ArrayFunctionCallTest.php index 39ded49fc..43bf2ff40 100644 --- a/tests/ArrayFunctionCallTest.php +++ b/tests/ArrayFunctionCallTest.php @@ -28,6 +28,19 @@ class ArrayFunctionCallTest extends TestCase '$e' => 'array|null>', ], ], + 'positiveIntArrayFilter' => [ + ' $d + * @param int<1,12> $f + * @psalm-return array{a: numeric, b?: int, c: positive-int, d?: int<0, 12>, f: int<1,12>} + */ + function makeAList($a, int $anyInt, int $positiveOne, int $d, int $f): array { + return array_filter(["a" => "1", "b" => $anyInt, "c" => $positiveOne, "d" => $d, "f" => $f]); + }' + ], 'arrayFilterAdvanced' => [ ' 5, "b" => 12, "c" => null], function(?int $val, string $key): bool {