From e3f3460f625f63bb8157f0dd9418a0580f12fdf1 Mon Sep 17 00:00:00 2001 From: Lisachenko Alexander Date: Sun, 9 Apr 2017 22:22:48 +0300 Subject: [PATCH] Method isSubclassOf() should also look at interfaces, fixes #72 --- src/Traits/ReflectionClassLikeTrait.php | 13 ++++++++++--- tests/ReflectionClassTest.php | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) 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);