Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClassReflection->is() shortcut #1468

Merged
merged 2 commits into from Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = get_class($this);
janedbal marked this conversation as resolved.
Show resolved Hide resolved

$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));
}

}