Skip to content

Commit

Permalink
Add ClassReflection->is() shortcut (#1468)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Jun 23, 2022
1 parent 895e90e commit e15f8f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Reflection/ClassReflection.php
Expand Up @@ -639,6 +639,11 @@ public function isAnonymous(): bool
return $this->anonymousFilename !== null;
}

public function is(string $className): bool
{
return $this->getName() === $className || $this->isSubclassOf($className);
}

public function isSubclassOf(string $className): bool
{
if (isset($this->subclasses[$className])) {
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Reflection/ClassReflectionTest.php
Expand Up @@ -28,7 +28,9 @@
use NestedTraits\BazTrait;
use NestedTraits\NoTrait;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\IntegerType;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use WrongClassConstantFile\SecuredRouter;
use function array_map;
Expand Down Expand Up @@ -307,4 +309,17 @@ public function testBackedEnumType(): void
$this->assertInstanceOf(IntegerType::class, $enum->getBackedEnumType());
}

public function testIs(): void
{
$className = static::class;

$reflectionProvider = $this->createReflectionProvider();
$classReflection = $reflectionProvider->getClass($className);

$this->assertTrue($classReflection->is($className));
$this->assertTrue($classReflection->is(PHPStanTestCase::class));
$this->assertTrue($classReflection->is(TestCase::class));
$this->assertFalse($classReflection->is(RuleTestCase::class));
}

}

0 comments on commit e15f8f7

Please sign in to comment.