Skip to content

Commit

Permalink
Fix #2627 - infer arrow function effects for array_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 15, 2020
1 parent c07fd13 commit 1b551b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Expand Up @@ -152,7 +152,8 @@ function (\Psalm\Storage\FunctionLikeParameter $param) {
}
}

if ($second_arg_value instanceof PhpParser\Node\Expr\Closure
if (($second_arg_value instanceof PhpParser\Node\Expr\Closure
|| $second_arg_value instanceof PhpParser\Node\Expr\ArrowFunction)
&& ($second_arg_type = $statements_source->node_data->getType($second_arg_value))
&& ($closure_types = $second_arg_type->getClosureTypes())
) {
Expand All @@ -171,9 +172,9 @@ function (\Psalm\Storage\FunctionLikeParameter $param) {
return Type::getArray();
}

if (count($second_arg_value->stmts) === 1 && count($second_arg_value->params)) {
if (count($second_arg_value->getStmts()) === 1 && count($second_arg_value->params)) {
$first_param = $second_arg_value->params[0];
$stmt = $second_arg_value->stmts[0];
$stmt = $second_arg_value->getStmts()[0];

if ($first_param->variadic === false
&& $first_param->var instanceof PhpParser\Node\Expr\Variable
Expand Down
23 changes: 22 additions & 1 deletion tests/FunctionCallTest.php
Expand Up @@ -2453,7 +2453,28 @@ function (array $element) : array {
)
);
}'
]
],
'arrayFilterArrowFunction' => [
'<?php
class A {}
class B {}
$a = \array_filter(
[new A(), new B()],
function($x) {
return $x instanceof B;
}
);
$b = \array_filter(
[new A(), new B()],
fn($x) => $x instanceof B
);',
'assertions' => [
'$a' => 'array<int, B>',
'$b' => 'array<int, B>',
],
],
];
}

Expand Down

0 comments on commit 1b551b8

Please sign in to comment.