From b9d8dd9d3f01ba145582f77b7eecb65d07b280c2 Mon Sep 17 00:00:00 2001 From: Fabien Villepinte Date: Thu, 6 Jan 2022 21:22:18 +0100 Subject: [PATCH] Fix analysis when __invoke() exists --- .../Statements/Expression/Call/FunctionCallAnalyzer.php | 6 +++--- tests/CallableTest.php | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php index becd4b5ea11..9b6bf31b1ea 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php @@ -52,6 +52,7 @@ use Psalm\Type\Atomic\TMixed; use Psalm\Type\Atomic\TNamedObject; use Psalm\Type\Atomic\TNull; +use Psalm\Type\Atomic\TObjectWithProperties; use Psalm\Type\Atomic\TString; use Psalm\Type\Atomic\TTemplateParam; use Psalm\Type\Reconciler; @@ -690,12 +691,11 @@ private static function getAnalyzeNamedExpression( ); } elseif ($var_type_part instanceof TCallableObject || $var_type_part instanceof TCallableString + || ($var_type_part instanceof TNamedObject && $var_type_part->value === 'Closure') + || ($var_type_part instanceof TObjectWithProperties && isset($var_type_part->methods['__invoke'])) ) { // this is fine $has_valid_function_call_type = true; - } elseif (($var_type_part instanceof TNamedObject && $var_type_part->value === 'Closure')) { - // this is fine - $has_valid_function_call_type = true; } elseif ($var_type_part instanceof TString || $var_type_part instanceof TArray || $var_type_part instanceof TList diff --git a/tests/CallableTest.php b/tests/CallableTest.php index 3864ec7a00c..65fa422056c 100644 --- a/tests/CallableTest.php +++ b/tests/CallableTest.php @@ -316,6 +316,15 @@ function foo(callable $c): void { $c2 = new C(); $c2();', ], + 'invokeMethodExists' => [ + ' [ '