Skip to content

Commit d9c1dcc

Browse files
VolChaik099
authored andcommitted
Fix problems with leading '\' in FQCNs (#89)
* Make some Reflection* classes independed of a leading '\' in FQCN * Make locators independed of a leading '\' in FQCN
1 parent 9e8dffc commit d9c1dcc

9 files changed

+13
-8
lines changed

src/Locator/CallableLocator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public function __construct(callable $callable)
3636
*/
3737
public function locateClass($className)
3838
{
39-
return call_user_func($this->callable, $className);
39+
return call_user_func($this->callable, ltrim($className, '\\'));
4040
}
4141
}

src/Locator/ComposerLocator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(ClassLoader $loader = null)
5151
*/
5252
public function locateClass($className)
5353
{
54-
$filePath = $this->loader->findFile($className);
54+
$filePath = $this->loader->findFile(ltrim($className, '\\'));
5555
if (!empty($filePath)) {
5656
$filePath = PathResolver::realpath($filePath);
5757
}

src/LocatorInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface LocatorInterface
1919
/**
2020
* Returns a path to the file for given class name
2121
*
22-
* @param string $className Name of the class
22+
* @param string $className Name of the class (with or without leading '\' FQCN)
2323
*
2424
* @return string|false Path to the file with given class or false if not found
2525
*/

src/ReflectionClass.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ReflectionClass extends InternalReflectionClass
3333
*/
3434
public function __construct($argument, ClassLike $classLikeNode = null)
3535
{
36-
$fullClassName = is_object($argument) ? get_class($argument) : $argument;
36+
$fullClassName = is_object($argument) ? get_class($argument) : ltrim($argument, '\\');
3737
$namespaceParts = explode('\\', $fullClassName);
3838
$this->className = array_pop($namespaceParts);
3939
// Let's unset original read-only property to have a control over it via __get

src/ReflectionMethod.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(
5252
ReflectionClass $declaringClass = null
5353
) {
5454
//for some reason, ReflectionMethod->getNamespaceName in php always returns '', so we shouldn't use it too
55-
$this->className = $className;
55+
$this->className = ltrim($className, '\\');
5656
$this->declaringClass = $declaringClass;
5757
$this->functionLikeNode = $classMethodNode ?: ReflectionEngine::parseClassMethod($className, $methodName);
5858

src/ReflectionProperty.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct(
6060
Property $propertyType = null,
6161
PropertyProperty $propertyNode = null
6262
) {
63-
$this->className = $className;
63+
$this->className = ltrim($className, '\\');
6464
if (!$propertyType || !$propertyNode) {
6565
list ($propertyType, $propertyNode) = ReflectionEngine::parseClassProperty($className, $propertyName);
6666
}

tests/Locator/CallableLocatorTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ class CallableLocatorTest extends \PHPUnit_Framework_TestCase
66
public function testLocateClass()
77
{
88
$callable = function ($class) {
9-
return $class . '.php';
9+
return ltrim($class, '\\') . '.php';
1010
};
1111

1212
$locator = new CallableLocator($callable);
1313

1414
$this->assertSame('class.php', $locator->locateClass('class'));
15+
$this->assertSame('class.php', $locator->locateClass('\class'));
1516
}
1617
}

tests/Locator/ComposerLocatorTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ public function testLocateClass()
1313
$reflectionClass->getFileName(),
1414
$locator->locateClass(ReflectionClass::class)
1515
);
16+
$this->assertSame(
17+
$reflectionClass->getFileName(),
18+
$locator->locateClass('\\' . ReflectionClass::class)
19+
);
1620
}
1721
}

tests/Stub/Issue44/Locator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Locator implements LocatorInterface
1111
*/
1212
public function locateClass($className)
1313
{
14-
if ($className === '\\Stub\\Issue44\\ClassWithNamespace') {
14+
if (ltrim($className, '\\') === ClassWithNamespace::class) {
1515
return __DIR__ . '/ClassWithNamespace.php';
1616
}
1717

0 commit comments

Comments
 (0)