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

Methods are case insensitive, and should be mapped and looked up without considering casing #601

Merged
merged 1 commit into from
May 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,15 @@ public function getConstructor()
*/
public function hasMethod($name)
{
return $this->betterReflectionClass->hasMethod($this->getMethodRealName($name));
return $this->betterReflectionClass->hasMethod($name);
}

/**
* {@inheritDoc}
*/
public function getMethod($name)
{
return new ReflectionMethod($this->betterReflectionClass->getMethod($this->getMethodRealName($name)));
}

private function getMethodRealName(string $name) : string
{
$realMethodNames = array_map(static function (BetterReflectionMethod $method) : string {
return $method->getName();
}, $this->betterReflectionClass->getMethods());

$methodNames = array_combine(array_map(static function (string $methodName) : string {
return strtolower($methodName);
}, $realMethodNames), $realMethodNames);

return $methodNames[strtolower($name)] ?? $name;
return new ReflectionMethod($this->betterReflectionClass->getMethod($name));
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ReflectionClass implements Reflection

/**
* @var ReflectionMethod[]|null
* @psalm-var ?array<string, ReflectionMethod>
* @psalm-var ?array<lowercase-string, ReflectionMethod>
*/
private $cachedMethods;

Expand Down Expand Up @@ -341,7 +341,7 @@ private function createMethodsFromTrait(ReflectionMethod $method) : array
*
* @return ReflectionMethod[] indexed by method name
*
* @psalm-return array<string, ReflectionMethod>
* @psalm-return array<lowercase-string, ReflectionMethod>
*/
private function getMethodsIndexedByName() : array
{
Expand All @@ -352,7 +352,7 @@ private function getMethodsIndexedByName() : array
$cachedMethods = [];

foreach ($this->getAllMethods() as $method) {
$methodName = $method->getName();
$methodName = strtolower($method->getName());

if (isset($cachedMethods[$methodName])) {
continue;
Expand Down Expand Up @@ -444,13 +444,14 @@ function (ClassMethod $methodNode) : ReflectionMethod {
*/
public function getMethod(string $methodName) : ReflectionMethod
{
$methods = $this->getMethodsIndexedByName();
$lowercaseMethodName = strtolower($methodName);
$methods = $this->getMethodsIndexedByName();

if (! isset($methods[$methodName])) {
if (! isset($methods[$lowercaseMethodName])) {
throw new OutOfBoundsException('Could not find method: ' . $methodName);
}

return $methods[$methodName];
return $methods[$lowercaseMethodName];
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/unit/Fixture/ClassWithCaseInsensitiveMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Roave\BetterReflectionTest\Fixture;

class ParentForClassWithCaseInsensitiveMethods
{
public function FOO()
{
}
}

class ClassWithCaseInsensitiveMethods extends ParentForClassWithCaseInsensitiveMethods
{
public function foo()
{
}
}
44 changes: 0 additions & 44 deletions test/unit/Reflection/Adapter/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,50 +190,6 @@ public function testGetParentClassReturnsFalseWhenNoParent() : void
self::assertFalse($reflectionClassAdapter->getParentClass());
}

public function testHasMethodIsCaseInsensitive() : void
{
$betterReflectionMethod = $this->createMock(BetterReflectionMethod::class);
$betterReflectionMethod
->method('getName')
->willReturn('foo');

$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$betterReflectionClass
->method('getMethods')
->willReturn([$betterReflectionMethod]);
$betterReflectionClass
->method('hasMethod')
->with('foo')
->willReturn(true);

$reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

self::assertTrue($reflectionClassAdapter->hasMethod('foo'));
self::assertTrue($reflectionClassAdapter->hasMethod('FOO'));
}

public function testGetMethodIsCaseInsensitive() : void
{
$betterReflectionMethod = $this->createMock(BetterReflectionMethod::class);
$betterReflectionMethod
->method('getName')
->willReturn('foo');

$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$betterReflectionClass
->method('getMethods')
->willReturn([$betterReflectionMethod]);
$betterReflectionClass
->method('getMethod')
->with('foo')
->willReturn($betterReflectionMethod);

$reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

self::assertSame('foo', $reflectionClassAdapter->getMethod('foo')->getName());
self::assertSame('foo', $reflectionClassAdapter->getMethod('FOO')->getName());
}

public function testGetMethodsFilter() : void
{
$publicBetterReflectionMethod = $this->createMock(BetterReflectionMethod::class);
Expand Down
32 changes: 32 additions & 0 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use Roave\BetterReflectionTest\Fixture;
use Roave\BetterReflectionTest\Fixture\AbstractClass;
use Roave\BetterReflectionTest\Fixture\ClassForHinting;
use Roave\BetterReflectionTest\Fixture\ClassWithCaseInsensitiveMethods;
use Roave\BetterReflectionTest\Fixture\ClassWithMissingParent;
use Roave\BetterReflectionTest\Fixture\ExampleClass;
use Roave\BetterReflectionTest\Fixture\ExampleClassWhereConstructorIsNotFirstMethod;
Expand Down Expand Up @@ -208,6 +209,16 @@ public function testGetMethodsWithFilter(int $filter, int $count) : void
self::assertCount($count, $classInfo->getImmediateMethods($filter));
}

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

self::assertCount(1, $classInfo->getMethods());
}

public function testGetMethodsReturnsInheritedMethods() : void
{
$classInfo = (new ClassReflector(new SingleFileSourceLocator(
Expand Down Expand Up @@ -674,6 +685,27 @@ public function testHasMethod() : void
self::assertTrue($classInfo->hasMethod('someMethod'));
}

public function testHasMethodIsCaseInsensitive() : void
{
$reflector = new ClassReflector($this->getComposerLocator());
$classInfo = $reflector->reflect(ExampleClass::class);

self::assertTrue($classInfo->hasMethod('someMethod'));
self::assertTrue($classInfo->hasMethod('SOMEMETHOD'));
self::assertTrue($classInfo->hasMethod('somemethod'));
}

public function testGetMethodIsCaseInsensitive() : void
{
$reflector = new ClassReflector($this->getComposerLocator());
$classInfo = $reflector->reflect(ExampleClass::class);

$method1 = $classInfo->getMethod('someMethod');
$method2 = $classInfo->getMethod('SOMEMETHOD');

self::assertSame($method1, $method2);
}

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