diff --git a/src/Traits/ReflectionClassLikeTrait.php b/src/Traits/ReflectionClassLikeTrait.php index f2e02743..604dde2b 100644 --- a/src/Traits/ReflectionClassLikeTrait.php +++ b/src/Traits/ReflectionClassLikeTrait.php @@ -758,9 +758,16 @@ public function isSubclassOf($class) if (!$this->classLikeNode instanceof Class_) { return false; - } else { - $extends = $this->classLikeNode->extends; - if ($extends && $extends->toString() == $class) { + } + + $extends = $this->classLikeNode->extends; + if ($extends && $extends->toString() == $class) { + return true; + } + + $implementedInterfaces = $this->classLikeNode->implements; + foreach ($implementedInterfaces as $implementedInterface) { + if ($implementedInterface->toString() == $class) { return true; } } diff --git a/tests/ReflectionClassTest.php b/tests/ReflectionClassTest.php index 5d5fc346..6fc07522 100644 --- a/tests/ReflectionClassTest.php +++ b/tests/ReflectionClassTest.php @@ -2,11 +2,13 @@ namespace Go\ParserReflection; use Go\ParserReflection\Stub\ClassWithConstantsAndInheritance; +use Go\ParserReflection\Stub\ClassWithInterface; use Go\ParserReflection\Stub\ClassWithMethodsAndProperties; use Go\ParserReflection\Stub\ClassWithScalarConstants; use Go\ParserReflection\Stub\FinalClass; use Go\ParserReflection\Stub\ImplicitAbstractClass; use Go\ParserReflection\Stub\SimpleAbstractInheritance; +use Go\ParserReflection\Stub\SimpleInterface; class ReflectionClassTest extends AbstractTestCase { @@ -147,6 +149,18 @@ public function testGetProperties($fileName) } } + /** + * Test isSubclassOf() method parity + */ + public function testIsSubclassOf() + { + $parsedClass = $this->parsedRefFileNamespace->getClass(ClassWithInterface::class); + $actualValue = $parsedClass->isSubclassOf(SimpleInterface::class); + $originalClass = new \ReflectionClass(ClassWithInterface::class); + $expectedValue = $originalClass->isSubclassOf(SimpleInterface::class); + $this->assertSame($expectedValue, $actualValue, "Class should also check interfaces for isSubclassOf"); + } + public function testNewInstanceMethod() { $parsedRefClass = $this->parsedRefFileNamespace->getClass(FinalClass::class);