1
0
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:
Nobu Funaki 2020-05-16 21:50:43 +09:00 committed by GitHub
parent e7db21fd44
commit abef8008bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -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) {

View File

@ -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;
}
'
],
];
}