Skip to content
Permalink
Browse files

Don’t modify AST when analyzing array_filter

  • Loading branch information...
muglug committed Jun 20, 2019
1 parent 18554b6 commit c93c2a48433e7fc6cfde4a84a334f9ceead1aa50
Showing with 14 additions and 12 deletions.
  1. +14 −12 src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php
@@ -59,16 +59,18 @@ 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)
$first_arg_value = $function_call_arg->value;
if ($first_arg_value instanceof PhpParser\Node\Scalar\String_
&& CallMap::inCallMap($first_arg_value->value)
) {
$callables = CallMap::getCallablesFromCallMap($function_call_arg->value->value);
$callables = CallMap::getCallablesFromCallMap($first_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([
$first_arg_value = new PhpParser\Node\Expr\Closure([
'params' => array_map(
function (\Psalm\Storage\FunctionLikeParameter $param) {
return new PhpParser\Node\Param(
@@ -81,7 +83,7 @@ function (\Psalm\Storage\FunctionLikeParameter $param) {
new PhpParser\Node\Stmt\Return_(
new PhpParser\Node\Expr\FuncCall(
new PhpParser\Node\Name\FullyQualified(
$function_call_arg->value->value
$first_arg_value->value
),
array_map(
function (\Psalm\Storage\FunctionLikeParameter $param) {
@@ -102,14 +104,14 @@ function (\Psalm\Storage\FunctionLikeParameter $param) {
$callable->return_type
);
$function_call_arg->value->inferredType = new Type\Union([$closure_atomic_type]);
$first_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'])
if ($first_arg_value instanceof PhpParser\Node\Expr\Closure
&& isset($first_arg_value->inferredType)
&& ($closure_atomic_type = $first_arg_value->inferredType->getTypes()['Closure'])
&& $closure_atomic_type instanceof Type\Atomic\TFn
) {
$closure_return_type = $closure_atomic_type->return_type ?: Type::getMixed();
@@ -126,9 +128,9 @@ function (\Psalm\Storage\FunctionLikeParameter $param) {
return Type::getArray();
}
if (count($function_call_arg->value->stmts) === 1 && count($function_call_arg->value->params)) {
$first_param = $function_call_arg->value->params[0];
$stmt = $function_call_arg->value->stmts[0];
if (count($first_arg_value->stmts) === 1 && count($first_arg_value->params)) {
$first_param = $first_arg_value->params[0];
$stmt = $first_arg_value->stmts[0];
if ($first_param->variadic === false
&& $first_param->var instanceof PhpParser\Node\Expr\Variable

0 comments on commit c93c2a4

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