Skip to content

Commit

Permalink
Fix path with scheme absolutize/normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 4, 2023
1 parent bed4e30 commit 5f53f24
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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

0 comments on commit 5f53f24

Please sign in to comment.