From e9f3d5ba06c93db5eb805b00ae87cf35f3d6c292 Mon Sep 17 00:00:00 2001 From: orklah Date: Fri, 17 Sep 2021 19:52:11 +0200 Subject: [PATCH] assert from TClassString --- .../Statements/Expression/AssertionFinder.php | 2 ++ tests/ClassLikeStringTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) 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' => [ '