mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Fix InvalidCast after is_callable([$val, '__toString']) check (#3372)
Support is_callable() with an array argument similar to method_exists()
This commit is contained in:
parent
e7db21fd44
commit
abef8008bc
@ -1897,6 +1897,20 @@ class AssertionFinder
|
||||
} elseif (self::hasCallableCheck($expr)) {
|
||||
if ($first_var_name) {
|
||||
$if_types[$first_var_name] = [[$prefix . 'callable']];
|
||||
} elseif ($expr->args[0]->value instanceof PhpParser\Node\Expr\Array_
|
||||
&& isset($expr->args[0]->value->items[0], $expr->args[0]->value->items[1])
|
||||
&& $expr->args[0]->value->items[1]->value instanceof PhpParser\Node\Scalar\String_
|
||||
) {
|
||||
$first_var_name_in_array_argument = ExpressionAnalyzer::getArrayVarId(
|
||||
$expr->args[0]->value->items[0]->value,
|
||||
$this_class_name,
|
||||
$source
|
||||
);
|
||||
if ($first_var_name_in_array_argument) {
|
||||
$if_types[$first_var_name_in_array_argument] = [
|
||||
[$prefix . 'hasmethod-' . $expr->args[0]->value->items[1]->value->value]
|
||||
];
|
||||
}
|
||||
}
|
||||
} elseif (self::hasIterableCheck($expr)) {
|
||||
if ($first_var_name) {
|
||||
|
@ -140,7 +140,13 @@ class ToStringTest extends TestCase
|
||||
if (method_exists($object, \'__toString\')) {
|
||||
$a = (string) $object;
|
||||
echo $a;
|
||||
}'
|
||||
}
|
||||
|
||||
if (is_callable([$object, \'__toString\'])) {
|
||||
$a = (string) $object;
|
||||
echo $a;
|
||||
}
|
||||
'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user