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

Fix path with scheme normalization #2506

Merged
merged 1 commit into from
Jul 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use function explode;
use function implode;
use function ltrim;
use function preg_match;
use function rtrim;
use function str_replace;
use function str_starts_with;
use function strlen;
use function strpos;
use function strtolower;
use function substr;
use function trim;
use const DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function absolutizePath(string $path): string
return $path;
}
}
if (str_starts_with($path, 'phar://')) {
if (preg_match('~^[a-z0-9+\-.]+://~i', $path) === 1) {
return $path;
}

Expand All @@ -64,11 +65,12 @@ public function normalizePath(string $originalPath, string $directorySeparator =

$matches = null;
if (!$isLocalPath) {
$matches = Strings::match($originalPath, '~^([a-z]+)\\:\\/\\/(.+)~');
$matches = Strings::match($originalPath, '~^([a-z0-9+\-.]+)://(.+)$~is');
}

if ($matches !== null) {
[, $scheme, $path] = $matches;
$scheme = strtolower($scheme);
} else {
$scheme = null;
$path = $originalPath;
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/File/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function dataAbsolutizePathOnWindows(): array
['users', 'C:\abcd\users'],
['../lib', 'C:\abcd\../lib'],
['./lib', 'C:\abcd\./lib'],
['vFs-v1.0://a\b', 'vFs-v1.0://a\b'],
['./x://a\b', 'C:\abcd\./x://a\b'],
];
}

Expand Down Expand Up @@ -47,6 +49,8 @@ public function dataAbsolutizePathOnLinuxOrMac(): array
['../lib', '/abcd/../lib'],
['./lib', '/abcd/./lib'],
['phar:///home/users/', 'phar:///home/users/'],
['vFs-v1.0://a/b', 'vFs-v1.0://a/b'],
['./x://a/b', '/abcd/./x://a/b'],
];
}

Expand All @@ -73,6 +77,7 @@ public function dataNormalizePathOnWindows(): array
['/home/users/./phpstan', '\home\users\phpstan'],
['/home/users/../../phpstan/', '\phpstan'],
['./phpstan/', 'phpstan'],
['vFs-v1.0://a/b', 'vfs-v1.0://a\b'],
];
}

Expand All @@ -98,6 +103,7 @@ public function dataNormalizePathOnLinuxOrMac(): array
['/home/users/./phpstan', '/home/users/phpstan'],
['/home/users/../../phpstan/', '/phpstan'],
['./phpstan/', 'phpstan'],
['vFs-v1.0://a/b', 'vfs-v1.0://a/b'],
['phar:///usr/local/bin/phpstan.phar/tmp/cache/../..', 'phar:///usr/local/bin/phpstan.phar'],
['phar:///usr/local/bin/phpstan.phar/tmp/cache/../../..', '/usr/local/bin'],
];
Expand Down