diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php index 279a4b25e..7eb395814 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php @@ -77,6 +77,18 @@ class ArrayFilterReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionReturn $statements_source->getSuppressedIssues() ); + if ($first_arg_array instanceof Type\Atomic\ObjectLike + && $first_arg_array->is_list + && $key_type->isSingleIntLiteral() + && $key_type->getSingleIntLiteral()->value === 0 + ) { + return new Type\Union([ + new Type\Atomic\TList( + $inner_type, + ), + ]); + } + if ($key_type->getLiteralStrings()) { $key_type->addType(new Type\Atomic\TString); } diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index 1006f7778..1cefde30b 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -2422,6 +2422,13 @@ class FunctionCallTest extends TestCase '$array' => 'list', ], ], + 'specialCaseArrayFilterOnSingleEntry' => [ + ' */ + function makeAList(int $ofThisInteger): array { + return array_filter([$ofThisInteger]); + }' + ], ]; }