From 9115ffd237664fb14d25593f2551d4bef9809158 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 15 Oct 2019 20:34:41 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20#2215=20-=20refine=20closure=20type=20if?= =?UTF-8?q?=20it=20doesn=E2=80=99t=20clash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Statements/Expression/CallAnalyzer.php | 22 ++++++++++++----- tests/CallableTest.php | 24 +++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php index e0529936f..1795639cc 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php @@ -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; } } diff --git a/tests/CallableTest.php b/tests/CallableTest.php index ffebef0ce..af6928f93 100644 --- a/tests/CallableTest.php +++ b/tests/CallableTest.php @@ -933,6 +933,30 @@ class CallableTest extends TestCase if (is_callable($setter)) {} }' ], + 'refineCallableTypeWithTypehint' => [ + ' [ + '