Skip to content

Commit

Permalink
Method isSubclassOf() should also look at interfaces, fixes #72
Browse files Browse the repository at this point in the history
  • Loading branch information
lisachenko committed Apr 9, 2017
1 parent 4897038 commit e3f3460
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Traits/ReflectionClassLikeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e3f3460

Please sign in to comment.