Skip to content

Commit d1ff286

Browse files
committed
Fix handling Assert::isInstanceOf
1 parent 1e3ff2c commit d1ff286

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,21 @@ private function getExpressionResolvers(): array
410410
},
411411
'isInstanceOf' => static function (Scope $scope, Arg $expr, Arg $class): ?Expr {
412412
$classType = $scope->getType($class->value);
413-
$classNameType = $classType->getObjectTypeOrClassStringObjectType();
414-
$classNames = $classNameType->getObjectClassNames();
415-
if (count($classNames) !== 1) {
413+
$classNameStrings = $classType->getConstantStrings();
414+
if (count($classNameStrings) !== 1) {
415+
$classNames = $classType->getObjectClassNames();
416+
if (count($classNames) === 1) {
417+
return new Instanceof_(
418+
$expr->value,
419+
new Name($classNames[0])
420+
);
421+
}
416422
return null;
417423
}
418424

419425
return new Instanceof_(
420426
$expr->value,
421-
new Name($classNames[0])
427+
new Name($classNameStrings[0]->getValue())
422428
);
423429
},
424430
'isInstanceOfAny' => function (Scope $scope, Arg $expr, Arg $classes): ?Expr {

tests/Type/WebMozartAssert/data/impossible-check.php

+8
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ public function implementsInterface($a, string $b, $c): void
111111
Assert::implementsInterface($c, self::class);
112112
}
113113

114+
/**
115+
* @param class-string<\Exception> $name
116+
*/
117+
public function testInstanceOfClassString(\Exception $e, string $name): void
118+
{
119+
Assert::isInstanceOf($e, $name);
120+
}
121+
114122
}
115123

116124
interface Bar {};

0 commit comments

Comments
 (0)