From bd846123ce47c937132dd47f13924490f78eec35 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 11 Jan 2020 11:54:29 -0500 Subject: [PATCH] Fix #2560 - handle special case of array_filter on single-element array --- .../ArrayFilterReturnTypeProvider.php | 12 ++++++++++++ tests/FunctionCallTest.php | 7 +++++++ 2 files changed, 19 insertions(+) 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]); + }' + ], ]; }