From d1ff28697bd4e1c9ef5d3f871367ce9092871fec Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 21 Feb 2023 19:44:34 +0100 Subject: [PATCH] Fix handling Assert::isInstanceOf --- .../AssertTypeSpecifyingExtension.php | 14 ++++++++++---- .../Type/WebMozartAssert/data/impossible-check.php | 8 ++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index 600ac33..b4c4f8b 100644 --- a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php +++ b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php @@ -410,15 +410,21 @@ private function getExpressionResolvers(): array }, 'isInstanceOf' => static function (Scope $scope, Arg $expr, Arg $class): ?Expr { $classType = $scope->getType($class->value); - $classNameType = $classType->getObjectTypeOrClassStringObjectType(); - $classNames = $classNameType->getObjectClassNames(); - if (count($classNames) !== 1) { + $classNameStrings = $classType->getConstantStrings(); + if (count($classNameStrings) !== 1) { + $classNames = $classType->getObjectClassNames(); + if (count($classNames) === 1) { + return new Instanceof_( + $expr->value, + new Name($classNames[0]) + ); + } return null; } return new Instanceof_( $expr->value, - new Name($classNames[0]) + new Name($classNameStrings[0]->getValue()) ); }, 'isInstanceOfAny' => function (Scope $scope, Arg $expr, Arg $classes): ?Expr { diff --git a/tests/Type/WebMozartAssert/data/impossible-check.php b/tests/Type/WebMozartAssert/data/impossible-check.php index 8175e26..271efc3 100644 --- a/tests/Type/WebMozartAssert/data/impossible-check.php +++ b/tests/Type/WebMozartAssert/data/impossible-check.php @@ -111,6 +111,14 @@ public function implementsInterface($a, string $b, $c): void Assert::implementsInterface($c, self::class); } + /** + * @param class-string<\Exception> $name + */ + public function testInstanceOfClassString(\Exception $e, string $name): void + { + Assert::isInstanceOf($e, $name); + } + } interface Bar {};