Skip to content

Commit

Permalink
Fix #2215 - refine closure type if it doesn’t clash
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 16, 2019
1 parent b29227a commit 9115ffd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,6 @@ protected static function checkFunctionArguments(
&& $param
&& $param->type
&& !$arg->value->getDocComment()
&& !array_filter(
$arg->value->params,
function (PhpParser\Node\Param $closure_param) : bool {
return !!$closure_param->type;
}
)
) {
if (count($args) === 2
&& (($argument_offset === 1 && $method_id === 'array_filter')
Expand Down Expand Up @@ -497,6 +491,22 @@ function (PhpParser\Node\Param $closure_param) : bool {
if (isset($replaced_type_part->params[$closure_param_offset]->type)
&& !$replaced_type_part->params[$closure_param_offset]->type->hasTemplate()
) {
if ($param_storage->type) {
if ($param_storage->type !== $param_storage->signature_type) {
continue;
}

$type_match_found = TypeAnalyzer::isContainedBy(
$codebase,
$replaced_type_part->params[$closure_param_offset]->type,
$param_storage->type
);

if (!$type_match_found) {
continue;
}
}

$param_storage->type = $replaced_type_part->params[$closure_param_offset]->type;
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/CallableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,30 @@ function foo(string $key) : void {
if (is_callable($setter)) {}
}'
],
'refineCallableTypeWithTypehint' => [
'<?php
/** @param string[][] $arr */
function foo(array $arr) : void {
array_map(
function(array $a) {
return reset($a);
},
$arr
);
}'
],
'refineCallableTypeWithoutTypehint' => [
'<?php
/** @param string[][] $arr */
function foo(array $arr) : void {
array_map(
function($a) {
return reset($a);
},
$arr
);
}'
],
];
}

Expand Down

0 comments on commit 9115ffd

Please sign in to comment.