Skip to content

Commit

Permalink
faster FileHelper::normalizePath()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Oct 25, 2021
1 parent 0e42081 commit 1505153
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace PHPStan\File;

use Nette\Utils\Strings;

class FileHelper
{

Expand Down Expand Up @@ -41,16 +39,21 @@ 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 {
$scheme = null;
$path = $originalPath;
}

$path = str_replace('\\', '/', $path);
$path = Strings::replace($path, '~/{2,}~', '/');
$path = str_replace(['\\', '//', '///', '////'], '/', $path);

$pathRoot = strpos($path, '/') === 0 ? $directorySeparator : '';
$pathParts = explode('/', trim($path, '/'));
Expand Down

0 comments on commit 1505153

Please sign in to comment.