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

faster FileHelper::normalizePath() #735

Merged
merged 5 commits into from
Oct 25, 2021
Merged
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
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);
Copy link
Contributor Author

@staabm staabm Oct 25, 2021

Choose a reason for hiding this comment

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

I assume that we won't see path with more then 4 slashes in a row.
the previous regex supported "any number of slashes" but I think thats more we actually need.


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