Skip to content

Commit

Permalink
Fix analysis when __invoke() exists
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored Jan 6, 2022
1 parent 46bcb62 commit b9d8dd9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/CallableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,15 @@ function foo(callable $c): void {
$c2 = new C();
$c2();',
],
'invokeMethodExists' => [
'<?php
function call(object $obj): void {
if (!method_exists($obj, "__invoke")) {
return;
}
$obj();
}',
],
'correctParamType' => [
'<?php
$take_string = function(string $s): string { return $s; };
Expand Down

0 comments on commit b9d8dd9

Please sign in to comment.