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

Fixed methods cache in ReflectionClass #590

Merged
merged 1 commit into from May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions src/Reflection/ReflectionClass.php
Expand Up @@ -296,7 +296,7 @@ private function getMethodsIndexedByName() : array
return $this->cachedMethods;
}

$this->cachedMethods = [];
$cachedMethods = [];

$traitAliases = $this->getTraitAliases();

Expand All @@ -309,20 +309,22 @@ private function getMethodsIndexedByName() : array
continue;
}

if (isset($this->cachedMethods[$methodAlias])) {
if (isset($cachedMethods[$methodAlias])) {
continue;
}

$this->cachedMethods[$methodAlias] = $method;
$cachedMethods[$methodAlias] = $method;
}

if (isset($this->cachedMethods[$methodName])) {
if (isset($cachedMethods[$methodName])) {
continue;
}

$this->cachedMethods[$methodName] = $method;
$cachedMethods[$methodName] = $method;
}

$this->cachedMethods = $cachedMethods;

return $this->cachedMethods;
}

Expand Down
8 changes: 8 additions & 0 deletions test/unit/Fixture/ClassWithMissingParent.php
@@ -0,0 +1,8 @@
<?php

namespace Roave\BetterReflectionTest\Fixture;

class ClassWithMissingParent extends ParentThatDoesNotExist
{

}
20 changes: 20 additions & 0 deletions test/unit/Reflection/ReflectionClassTest.php
Expand Up @@ -29,6 +29,7 @@
use Roave\BetterReflection\Reflection\ReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionProperty;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Type\ComposerSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
Expand All @@ -47,6 +48,7 @@
use Roave\BetterReflectionTest\Fixture;
use Roave\BetterReflectionTest\Fixture\AbstractClass;
use Roave\BetterReflectionTest\Fixture\ClassForHinting;
use Roave\BetterReflectionTest\Fixture\ClassWithMissingParent;
use Roave\BetterReflectionTest\Fixture\ExampleClass;
use Roave\BetterReflectionTest\Fixture\ExampleClassWhereConstructorIsNotFirstMethod;
use Roave\BetterReflectionTest\Fixture\ExampleInterface;
Expand Down Expand Up @@ -236,6 +238,24 @@ public function testGetMethodsReturnsInheritedMethods() : void
self::assertSame('Qux', $classInfo->getMethod('f')->getDeclaringClass()->getName());
}

public function testGetMethodsWithBrokenClass() : void
{
$classInfo = (new ClassReflector(new SingleFileSourceLocator(
__DIR__ . '/../Fixture/ClassWithMissingParent.php',
$this->astLocator
)))->reflect(ClassWithMissingParent::class);

try {
$classInfo->getMethods();
} catch (IdentifierNotFound $e) {
// Ignore error for the first time
}

self::expectException(IdentifierNotFound::class);

$classInfo->getMethods();
}

public function testGetMethodsOrder() : void
{
$classInfo = (new ClassReflector(new SingleFileSourceLocator(
Expand Down