From 1505153525ec010466b4cbd9dcbae79682c43e31 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 25 Oct 2021 17:01:55 +0200 Subject: [PATCH] faster `FileHelper::normalizePath()` --- src/File/FileHelper.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/File/FileHelper.php b/src/File/FileHelper.php index eac8c5accc..890d8309fe 100644 --- a/src/File/FileHelper.php +++ b/src/File/FileHelper.php @@ -2,8 +2,6 @@ namespace PHPStan\File; -use Nette\Utils\Strings; - class FileHelper { @@ -41,7 +39,13 @@ public function absolutizePath(string $path): string /** @api */ public function normalizePath(string $originalPath, string $directorySeparator = DIRECTORY_SEPARATOR): string { - $matches = \Nette\Utils\Strings::match($originalPath, '~^([a-z]+)\\:\\/\\/(.+)~'); + $isLocalPath = $originalPath !== '' && $originalPath[0] === '/'; + + $matches = null; + if (!$isLocalPath) { + $matches = \Nette\Utils\Strings::match($originalPath, '~^([a-z]+)\\:\\/\\/(.+)~'); + } + if ($matches !== null) { [, $scheme, $path] = $matches; } else { @@ -49,8 +53,7 @@ public function normalizePath(string $originalPath, string $directorySeparator = $path = $originalPath; } - $path = str_replace('\\', '/', $path); - $path = Strings::replace($path, '~/{2,}~', '/'); + $path = str_replace(['\\', '//', '///', '////'], '/', $path); $pathRoot = strpos($path, '/') === 0 ? $directorySeparator : ''; $pathParts = explode('/', trim($path, '/'));