Skip to content
Permalink
Browse files

Fix #1816 - convert array_filter strings to full closures

  • Loading branch information...
muglug committed Jun 19, 2019
1 parent f06cd85 commit 84e002e9a719783ca31ab586653acae8d893ecbb
@@ -5,6 +5,7 @@
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Codebase\CallMap;
use Psalm\IssueBuffer;
use Psalm\Issue\InvalidReturnType;
use Psalm\StatementsSource;
@@ -58,6 +59,54 @@ public static function getFunctionReturnType(
} elseif (!isset($call_args[2])) {
$function_call_arg = $call_args[1];
if ($function_call_arg->value instanceof PhpParser\Node\Scalar\String_
&& CallMap::inCallMap($function_call_arg->value->value)
) {
$callables = CallMap::getCallablesFromCallMap($function_call_arg->value->value);
if ($callables) {
$callable = clone $callables[0];
if ($callable->params !== null && $callable->return_type) {
$function_call_arg->value = new PhpParser\Node\Expr\Closure([
'params' => array_map(
function (\Psalm\Storage\FunctionLikeParameter $param) {
return new PhpParser\Node\Param(
new PhpParser\Node\Expr\Variable($param->name)
);
},
$callable->params
),
'stmts' => [
new PhpParser\Node\Stmt\Return_(
new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name\FullyQualified(
$function_call_arg->value->value
),
array_map(
function (\Psalm\Storage\FunctionLikeParameter $param) {
return new PhpParser\Node\Arg(
new PhpParser\Node\Expr\Variable($param->name)
);
},
$callable->params
)
)
)
],
]);
$closure_atomic_type = new Type\Atomic\TFn(
'Closure',
$callable->params,
$callable->return_type
);
$function_call_arg->value->inferredType = new Type\Union([$closure_atomic_type]);
}
}
}
if ($function_call_arg->value instanceof PhpParser\Node\Expr\Closure
&& isset($function_call_arg->value->inferredType)
&& ($closure_atomic_type = $function_call_arg->value->inferredType->getTypes()['Closure'])
@@ -95,6 +95,16 @@ function (int $arg) {
);
}',
],
'arrayFilterNamedFunction' => [
'<?php
/**
* @param array<int, DateTimeImmutable|null> $a
* @return array<int, DateTimeImmutable>
*/
function foo(array $a) : array {
return array_filter($a, "is_object");
}',
],
'typedArrayWithDefault' => [
'<?php
class A {}

0 comments on commit 84e002e

Please sign in to comment.
You can’t perform that action at this time.