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

allow assertion to work on $var::class

This commit is contained in:
orklah 2021-11-04 20:33:30 +01:00
parent 73fb04fce7
commit 5282e4728b
2 changed files with 42 additions and 2 deletions

View File

@ -1416,6 +1416,11 @@ class AssertionFinder
&& $conditional->right->name instanceof PhpParser\Node\Identifier
&& strtolower($conditional->right->name->name) === 'class';
$right_variable_class_const = $conditional->right instanceof PhpParser\Node\Expr\ClassConstFetch
&& $conditional->right->class instanceof PhpParser\Node\Expr\Variable
&& $conditional->right->name instanceof PhpParser\Node\Identifier
&& strtolower($conditional->right->name->name) === 'class';
$left_class_string = $conditional->left instanceof PhpParser\Node\Expr\ClassConstFetch
&& $conditional->left->class instanceof PhpParser\Node\Name
&& $conditional->left->name instanceof PhpParser\Node\Identifier
@ -1434,7 +1439,9 @@ class AssertionFinder
}
}
if (($right_get_class || $right_static_class) && ($left_class_string || $left_class_string_t)) {
if (($right_get_class || $right_static_class || $right_variable_class_const)
&& ($left_class_string || $left_class_string_t)
) {
return self::ASSIGNMENT_TO_RIGHT;
}
@ -1448,6 +1455,11 @@ class AssertionFinder
&& $conditional->left->name instanceof PhpParser\Node\Identifier
&& strtolower($conditional->left->name->name) === 'class';
$left_variable_class_const = $conditional->left instanceof PhpParser\Node\Expr\ClassConstFetch
&& $conditional->left->class instanceof PhpParser\Node\Expr\Variable
&& $conditional->left->name instanceof PhpParser\Node\Identifier
&& strtolower($conditional->left->name->name) === 'class';
$right_class_string = $conditional->right instanceof PhpParser\Node\Expr\ClassConstFetch
&& $conditional->right->class instanceof PhpParser\Node\Name
&& $conditional->right->name instanceof PhpParser\Node\Identifier
@ -1466,7 +1478,9 @@ class AssertionFinder
}
}
if (($left_get_class || $left_static_class) && ($right_class_string || $right_class_string_t)) {
if (($left_get_class || $left_static_class || $left_variable_class_const)
&& ($right_class_string || $right_class_string_t)
) {
return self::ASSIGNMENT_TO_LEFT;
}
@ -2551,6 +2565,12 @@ class AssertionFinder
$this_class_name,
$source
);
} elseif ($getclass_expr instanceof PhpParser\Node\Expr\ClassConstFetch) {
$var_name = ExpressionIdentifier::getArrayVarId(
$getclass_expr->class,
$this_class_name,
$source
);
} else {
$var_name = '$this';
}
@ -3248,6 +3268,12 @@ class AssertionFinder
$this_class_name,
$source
);
} elseif ($getclass_expr instanceof PhpParser\Node\Expr\ClassConstFetch) {
$var_name = ExpressionIdentifier::getArrayVarId(
$getclass_expr->class,
$this_class_name,
$source
);
} else {
$var_name = '$this';
}

View File

@ -2807,6 +2807,20 @@ class ConditionalTest extends \Psalm\Tests\TestCase
$foo->foo();
}',
],
'variable::classAssertion' => [
'<?php
abstract class A {}
class B extends A {}
function a(A $a): void {
if($a::class == B::class) {
b($a);
}
}
function b(B $_b): void {
}',
],
];
}