1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix analysis when __invoke() exists

This commit is contained in:
Fabien Villepinte 2022-01-06 21:22:18 +01:00 committed by GitHub
parent 46bcb626f5
commit b9d8dd9d3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -52,6 +52,7 @@ use Psalm\Type\Atomic\TLiteralString;
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 @@ class FunctionCallAnalyzer extends CallAnalyzer
);
} 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

View File

@ -316,6 +316,15 @@ class CallableTest extends TestCase
$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; };