diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php index cee52afa8..85c67189a 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssertionFinder.php @@ -1173,6 +1173,8 @@ class AssertionFinder $literal_class_strings[] = $atomic_type->value; } elseif ($atomic_type instanceof Type\Atomic\TTemplateParamClass) { $literal_class_strings[] = $atomic_type->param_name; + } elseif ($atomic_type instanceof Type\Atomic\TClassString && $atomic_type->as !== 'object') { + $literal_class_strings[] = $atomic_type->as; } } diff --git a/tests/ClassLikeStringTest.php b/tests/ClassLikeStringTest.php index e1dc00053..553152484 100644 --- a/tests/ClassLikeStringTest.php +++ b/tests/ClassLikeStringTest.php @@ -458,6 +458,23 @@ class ClassLikeStringTest extends TestCase } }', ], + 'instanceofClassStringNotLiteral' => [ + ' $class + */ + private string $class = stdClass::class; + + public function go(object $object): ?stdClass { + $a = $this->class; + if ($object instanceof $a) { + return $object; + } + return null; + } + }' + ], 'returnTemplatedClassString' => [ '