1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #2215 - refine closure type if it doesn’t clash

This commit is contained in:
Matthew Brown 2019-10-15 20:34:41 -04:00
parent b29227aaf6
commit 9115ffd237
2 changed files with 40 additions and 6 deletions

View File

@ -437,12 +437,6 @@ class CallAnalyzer
&& $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')
@ -497,6 +491,22 @@ class CallAnalyzer
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;
}
}

View File

@ -933,6 +933,30 @@ class CallableTest extends TestCase
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
);
}'
],
];
}