1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #2294 - detect invalid callble due to __invoke

This commit is contained in:
Brown 2019-11-07 09:39:24 -05:00
parent 64996f464e
commit 86b6801066
2 changed files with 43 additions and 0 deletions

View File

@ -1683,6 +1683,21 @@ class TypeAnalyzer
// do nothing
}
}
} elseif ($input_type_part instanceof TNamedObject
&& $codebase->classExists($input_type_part->value)
&& $codebase->methodExists($input_type_part->value . '::__invoke')
) {
return new TCallable(
'callable',
$codebase->getMethodParams(
$input_type_part->value . '::__invoke'
),
$codebase->getMethodReturnType(
$input_type_part->value . '::__invoke',
$input_type_part->value,
[]
)
);
}
return null;

View File

@ -1203,6 +1203,34 @@ class FunctionTemplateTest extends TestCase
makeResultSet([A::class, "returnsObjectOrNull"]);',
'error_message' => 'InvalidArgument',
],
'templateInvokeArg' => [
'<?php
/**
* @template T
* @param callable(T):void $c
* @param T $param
*/
function apply(callable $c, $param):void{
call_user_func($c, $param);
}
class A {
public function __toString(){
return "a";
}
}
class B {}
class Printer{
public function __invoke(A $a) : void {
echo $a;
}
}
apply(new Printer(), new B());',
'error_message' => 'InvalidArgument'
],
];
}
}