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:
parent
40406c7ff0
commit
bd846123ce
@ -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);
|
||||
}
|
||||
|
@ -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]);
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user