1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #2560 - handle special case of array_filter on single-element array

This commit is contained in:
Matthew Brown 2020-01-11 11:54:29 -05:00
parent 40406c7ff0
commit bd846123ce
2 changed files with 19 additions and 0 deletions

View File

@ -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);
}

View File

@ -2422,6 +2422,13 @@ class FunctionCallTest extends TestCase
'$array' => 'list<int>',
],
],
'specialCaseArrayFilterOnSingleEntry' => [
'<?php
/** @psalm-return list<int> */
function makeAList(int $ofThisInteger): array {
return array_filter([$ofThisInteger]);
}'
],
];
}