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

Return system directory separator in adapters #591

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Util\FileHelper;
use function array_combine;
use function array_map;
use function array_values;
Expand Down Expand Up @@ -121,7 +122,9 @@ public function isCloneable()
*/
public function getFileName()
{
return $this->betterReflectionClass->getFileName() ?? false;
$fileName = $this->betterReflectionClass->getFileName();

return $fileName !== null ? FileHelper::normalizeSystemPath($fileName) : false;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Reflection/Adapter/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ReflectionFunction as CoreReflectionFunction;
use Roave\BetterReflection\Reflection\Adapter\Exception\NotImplemented;
use Roave\BetterReflection\Reflection\ReflectionFunction as BetterReflectionFunction;
use Roave\BetterReflection\Util\FileHelper;
use Throwable;
use function func_get_args;

Expand Down Expand Up @@ -133,7 +134,9 @@ public function getExtensionName()
*/
public function getFileName()
{
return $this->betterReflectionFunction->getFileName() ?? false;
$fileName = $this->betterReflectionFunction->getFileName();

return $fileName !== null ? FileHelper::normalizeSystemPath($fileName) : false;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Reflection/Adapter/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Roave\BetterReflection\Reflection\Exception\NoObjectProvided;
use Roave\BetterReflection\Reflection\Exception\NotAnObject;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Util\FileHelper;
use Throwable;
use function func_get_args;

Expand Down Expand Up @@ -138,7 +139,9 @@ public function getExtensionName()
*/
public function getFileName()
{
return $this->betterReflectionMethod->getFileName() ?? false;
$fileName = $this->betterReflectionMethod->getFileName();

return $fileName !== null ? FileHelper::normalizeSystemPath($fileName) : false;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Reflection/Adapter/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Util\FileHelper;
use function array_combine;
use function array_map;
use function array_values;
Expand Down Expand Up @@ -100,7 +101,9 @@ public function isCloneable()
*/
public function getFileName()
{
return $this->betterReflectionObject->getFileName() ?? false;
$fileName = $this->betterReflectionObject->getFileName();

return $fileName !== null ? FileHelper::normalizeSystemPath($fileName) : false;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Util/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
namespace Roave\BetterReflection\Util;

use function str_replace;
use const DIRECTORY_SEPARATOR;

class FileHelper
{
public static function normalizeWindowsPath(string $path) : string
{
return str_replace('\\', '/', $path);
}

public static function normalizeSystemPath(string $path) : string
{
$path = self::normalizeWindowsPath($path);

return DIRECTORY_SEPARATOR === '\\'
? str_replace('/', '\\', $path)
: $path;
}
}
15 changes: 15 additions & 0 deletions test/unit/Reflection/Adapter/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Roave\BetterReflection\Reflection\ReflectionClassConstant as BetterReflectionClassConstant;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Util\FileHelper;
use stdClass;
use function array_combine;
use function array_map;
Expand Down Expand Up @@ -149,6 +150,20 @@ public function testGetFileNameReturnsFalseWhenNoFileName() : void
self::assertFalse($reflectionClassAdapter->getFileName());
}

public function testGetFileNameReturnsPathWithSystemDirectorySeparator() : void
{
$fileName = 'foo/bar\\foo/bar.php';

$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
$betterReflectionClass
->method('getFileName')
->willReturn($fileName);

$reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

self::assertSame(FileHelper::normalizeSystemPath($fileName), $reflectionClassAdapter->getFileName());
}

public function testGetDocCommentReturnsFalseWhenNoDocComment() : void
{
$betterReflectionClass = $this->createMock(BetterReflectionClass::class);
Expand Down
15 changes: 15 additions & 0 deletions test/unit/Reflection/Adapter/ReflectionFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Roave\BetterReflection\Reflection\ReflectionFunction as BetterReflectionFunction;
use Roave\BetterReflection\Reflection\ReflectionParameter as BetterReflectionParameter;
use Roave\BetterReflection\Reflection\ReflectionType as BetterReflectionType;
use Roave\BetterReflection\Util\FileHelper;
use Throwable;
use function array_combine;
use function array_map;
Expand Down Expand Up @@ -131,6 +132,20 @@ public function testGetFileNameReturnsFalseWhenNoFileName() : void
self::assertFalse($betterReflectionFunction->getFileName());
}

public function testGetFileNameReturnsPathWithSystemDirectorySeparator() : void
{
$fileName = 'foo/bar\\foo/bar.php';

$betterReflectionFunction = $this->createMock(BetterReflectionFunction::class);
$betterReflectionFunction
->method('getFileName')
->willReturn($fileName);

$betterReflectionFunction = new ReflectionFunctionAdapter($betterReflectionFunction);

self::assertSame(FileHelper::normalizeSystemPath($fileName), $betterReflectionFunction->getFileName());
}

public function testGetDocCommentReturnsFalseWhenNoDocComment() : void
{
$betterReflectionFunction = $this->createMock(BetterReflectionFunction::class);
Expand Down
15 changes: 15 additions & 0 deletions test/unit/Reflection/Adapter/ReflectionMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionParameter as BetterReflectionParameter;
use Roave\BetterReflection\Reflection\ReflectionType as BetterReflectionType;
use Roave\BetterReflection\Util\FileHelper;
use stdClass;
use Throwable;
use function array_combine;
Expand Down Expand Up @@ -164,6 +165,20 @@ public function testGetFileNameReturnsFalseWhenNoFileName() : void
self::assertFalse($betterReflectionMethod->getFileName());
}

public function testGetFileNameReturnsPathWithSystemDirectorySeparator() : void
{
$fileName = 'foo/bar\\foo/bar.php';

$betterReflectionMethod = $this->createMock(BetterReflectionMethod::class);
$betterReflectionMethod
->method('getFileName')
->willReturn($fileName);

$betterReflectionMethod = new ReflectionMethodAdapter($betterReflectionMethod);

self::assertSame(FileHelper::normalizeSystemPath($fileName), $betterReflectionMethod->getFileName());
}

public function testGetDocCommentReturnsFalseWhenNoDocComment() : void
{
$betterReflectionMethod = $this->createMock(BetterReflectionMethod::class);
Expand Down
15 changes: 15 additions & 0 deletions test/unit/Reflection/Adapter/ReflectionObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Util\FileHelper;
use stdClass;
use function array_combine;
use function array_map;
Expand Down Expand Up @@ -144,6 +145,20 @@ public function testGetFileNameReturnsFalseWhenNoFileName() : void
self::assertFalse($betterReflectionObject->getFileName());
}

public function testGetFileNameReturnsPathWithSystemDirectorySeparator() : void
{
$fileName = 'foo/bar\\foo/bar.php';

$betterReflectionObject = $this->createMock(BetterReflectionObject::class);
$betterReflectionObject
->method('getFileName')
->willReturn($fileName);

$betterReflectionObject = new ReflectionObjectAdapter($betterReflectionObject);

self::assertSame(FileHelper::normalizeSystemPath($fileName), $betterReflectionObject->getFileName());
}

public function testGetDocCommentReturnsFalseWhenNoDocComment() : void
{
$betterReflectionObject = $this->createMock(BetterReflectionObject::class);
Expand Down
9 changes: 9 additions & 0 deletions test/unit/Util/FindHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use PHPUnit\Framework\TestCase;
use Roave\BetterReflection\Util\FileHelper;
use function strtr;
use const DIRECTORY_SEPARATOR;

/**
* @covers \Roave\BetterReflection\Util\FileHelper
Expand All @@ -17,4 +19,11 @@ public function testNormalizeWindowsPath() : void
self::assertSame('directory/foo/boo/file.php', FileHelper::normalizeWindowsPath('directory\\foo/boo\\file.php'));
self::assertSame('directory/foo/boo/file.php', FileHelper::normalizeWindowsPath('directory/foo/boo/file.php'));
}

public function testSystemWindowsPath() : void
{
$path = 'directory\\foo/boo\\foo/file.php';

self::assertSame(strtr($path, '\\/', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR), FileHelper::normalizeSystemPath($path));
}
}