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

FileHelper - fix normalizing paths with protocol:// on Windows #632

Merged
merged 2 commits into from
Jun 10, 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
18 changes: 13 additions & 5 deletions src/Util/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Roave\BetterReflection\Util;

use function preg_match;
use function str_replace;
use const DIRECTORY_SEPARATOR;

Expand All @@ -14,12 +15,19 @@ public static function normalizeWindowsPath(string $path) : string
return str_replace('\\', '/', $path);
}

public static function normalizeSystemPath(string $path) : string
public static function normalizeSystemPath(string $originalPath) : string
{
$path = self::normalizeWindowsPath($path);
$path = self::normalizeWindowsPath($originalPath);
preg_match('~^([a-z]+)\\:\\/\\/(.+)~', $path, $matches);
$scheme = null;
if ($matches !== []) {
[, $scheme, $path] = $matches;
}

return DIRECTORY_SEPARATOR === '\\'
? str_replace('/', '\\', $path)
: $path;
return ($scheme !== null ? $scheme . '://' : '') . (
DIRECTORY_SEPARATOR === '\\'
? str_replace('/', '\\', $path)
: $path
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @covers \Roave\BetterReflection\Util\FileHelper
*/
class FindHelperTest extends TestCase
class FileHelperTest extends TestCase
{
public function testNormalizeWindowsPath() : void
{
Expand All @@ -26,4 +26,17 @@ public function testSystemWindowsPath() : void

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

public function testSystemWindowsPathWithProtocol() : void
{
if (DIRECTORY_SEPARATOR !== '\\') {
$this->markTestSkipped('Test runs only on Windows');
}

$path = 'phar://C:/Users/ondrej/phpstan.phar/src/TrinaryLogic.php';
self::assertSame(
'phar://C:\Users\ondrej\phpstan.phar\src\TrinaryLogic.php',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that phars still have borked file paths 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it - 'phar://C:\Users\ondrej\phpstan.phar\src\TrinaryLogic.php' is what we want here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but effectively this is no longer a filesystem path.

Anyway, all good with this patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

FileHelper::normalizeSystemPath($path),
);
}
}