Skip to content

Commit

Permalink
Fix problems with leading '\' in FQCNs (#89)
Browse files Browse the repository at this point in the history
* Make some Reflection* classes independed of a leading '\' in FQCN

* Make locators independed of a leading '\' in FQCN
  • Loading branch information
VolCh authored and aik099 committed Mar 19, 2018
1 parent 9e8dffc commit d9c1dcc
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Locator/CallableLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public function __construct(callable $callable)
*/
public function locateClass($className)
{
return call_user_func($this->callable, $className);
return call_user_func($this->callable, ltrim($className, '\\'));
}
}
2 changes: 1 addition & 1 deletion src/Locator/ComposerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(ClassLoader $loader = null)
*/
public function locateClass($className)
{
$filePath = $this->loader->findFile($className);
$filePath = $this->loader->findFile(ltrim($className, '\\'));
if (!empty($filePath)) {
$filePath = PathResolver::realpath($filePath);
}
Expand Down
2 changes: 1 addition & 1 deletion src/LocatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface LocatorInterface
/**
* Returns a path to the file for given class name
*
* @param string $className Name of the class
* @param string $className Name of the class (with or without leading '\' FQCN)
*
* @return string|false Path to the file with given class or false if not found
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ReflectionClass extends InternalReflectionClass
*/
public function __construct($argument, ClassLike $classLikeNode = null)
{
$fullClassName = is_object($argument) ? get_class($argument) : $argument;
$fullClassName = is_object($argument) ? get_class($argument) : ltrim($argument, '\\');
$namespaceParts = explode('\\', $fullClassName);
$this->className = array_pop($namespaceParts);
// Let's unset original read-only property to have a control over it via __get
Expand Down
2 changes: 1 addition & 1 deletion src/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(
ReflectionClass $declaringClass = null
) {
//for some reason, ReflectionMethod->getNamespaceName in php always returns '', so we shouldn't use it too
$this->className = $className;
$this->className = ltrim($className, '\\');
$this->declaringClass = $declaringClass;
$this->functionLikeNode = $classMethodNode ?: ReflectionEngine::parseClassMethod($className, $methodName);

Expand Down
2 changes: 1 addition & 1 deletion src/ReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(
Property $propertyType = null,
PropertyProperty $propertyNode = null
) {
$this->className = $className;
$this->className = ltrim($className, '\\');
if (!$propertyType || !$propertyNode) {
list ($propertyType, $propertyNode) = ReflectionEngine::parseClassProperty($className, $propertyName);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Locator/CallableLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ class CallableLocatorTest extends \PHPUnit_Framework_TestCase
public function testLocateClass()
{
$callable = function ($class) {
return $class . '.php';
return ltrim($class, '\\') . '.php';
};

$locator = new CallableLocator($callable);

$this->assertSame('class.php', $locator->locateClass('class'));
$this->assertSame('class.php', $locator->locateClass('\class'));
}
}
4 changes: 4 additions & 0 deletions tests/Locator/ComposerLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public function testLocateClass()
$reflectionClass->getFileName(),
$locator->locateClass(ReflectionClass::class)
);
$this->assertSame(
$reflectionClass->getFileName(),
$locator->locateClass('\\' . ReflectionClass::class)
);
}
}
2 changes: 1 addition & 1 deletion tests/Stub/Issue44/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Locator implements LocatorInterface
*/
public function locateClass($className)
{
if ($className === '\\Stub\\Issue44\\ClassWithNamespace') {
if (ltrim($className, '\\') === ClassWithNamespace::class) {
return __DIR__ . '/ClassWithNamespace.php';
}

Expand Down

0 comments on commit d9c1dcc

Please sign in to comment.