Skip to content

Commit

Permalink
Fix handling Assert::isInstanceOf
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 21, 2023
1 parent 1e3ff2c commit d1ff286
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions tests/Type/WebMozartAssert/data/impossible-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down

0 comments on commit d1ff286

Please sign in to comment.