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

Support backslash in is_a/is_subclass_of string name

This commit is contained in:
Matthew Brown 2019-01-09 08:42:27 -05:00
parent 11bfaaa5f7
commit 01e41951e7
2 changed files with 24 additions and 1 deletions

View File

@ -1404,7 +1404,11 @@ class AssertionFinder
$is_a_prefix = $third_arg_value === 'true' ? 'isa-string-' : 'isa-';
if ($second_arg instanceof PhpParser\Node\Scalar\String_) {
$if_types[$first_var_name] = [[$prefix . $is_a_prefix . $second_arg->value]];
$fq_class_name = $second_arg->value;
if ($fq_class_name[0] === '\\') {
$fq_class_name = substr($fq_class_name, 1);
}
$if_types[$first_var_name] = [[$prefix . $is_a_prefix . $fq_class_name]];
} elseif ($second_arg instanceof PhpParser\Node\Expr\ClassConstFetch
&& $second_arg->class instanceof PhpParser\Node\Name
&& $second_arg->name instanceof PhpParser\Node\Identifier

View File

@ -395,6 +395,25 @@ class ClassStringTest extends TestCase
return $s;
}',
],
'createClassOfTypeFromStringUsingIsSubclassOfString' => [
'<?php
class A {}
/**
* @return class-string<A> $s
*/
function foo(string $s) : string {
if (!class_exists($s)) {
throw new \UnexpectedValueException("bad");
}
if (!is_subclass_of($s, "\A")) {
throw new \UnexpectedValueException("bad");
}
return $s;
}',
],
'checkSubclassOfAbstract' => [
'<?php
interface Foo {