Skip to content

Commit

Permalink
Fix #2560 - handle special case of array_filter on single-element array
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 11, 2020
1 parent 40406c7 commit bd84612
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public static function getFunctionReturnType(
$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);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,13 @@ function getCharPairs(string $line) : array {
'$array' => 'list<int>',
],
],
'specialCaseArrayFilterOnSingleEntry' => [
'<?php
/** @psalm-return list<int> */
function makeAList(int $ofThisInteger): array {
return array_filter([$ofThisInteger]);
}'
],
];
}

Expand Down

0 comments on commit bd84612

Please sign in to comment.