Skip to content

Commit

Permalink
Skipping: Use plain string functions instead of regex matching (#4153)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 10, 2023
1 parent 0a62001 commit 21a233d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
20 changes: 0 additions & 20 deletions packages/Skipper/Enum/AsteriskMatch.php

This file was deleted.

10 changes: 2 additions & 8 deletions packages/Skipper/FileSystem/FnMatchPathNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ final class FnMatchPathNormalizer
{
public function normalizeForFnmatch(string $path): string
{
// ends with *
if (Strings::match($path, AsteriskMatch::ONLY_ENDS_WITH_ASTERISK_REGEX) !== null) {
return '*' . $path;
}

// starts with *
if (Strings::match($path, AsteriskMatch::ONLY_STARTS_WITH_ASTERISK_REGEX) !== null) {
return $path . '*';
if (str_ends_with($path, '*') || str_starts_with($path, '*')) {
return '*' . trim($path, '*') . '*';
}

if (\str_contains($path, '..')) {
Expand Down
21 changes: 10 additions & 11 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ private function addFilterWithExcludedPaths(Finder $finder): void
// make the path work accross different OSes
$excludePath = str_replace('\\', '/', $excludePath);

if (StringUtils::isMatch($realPath, '#' . preg_quote($excludePath, '#') . '#')) {
if (fnmatch($this->normalizeForFnmatch($excludePath), $realPath)) {
return false;
}

$excludePath = $this->normalizeForFnmatch($excludePath);
if (fnmatch($excludePath, $realPath)) {
if (str_contains($excludePath, '**')) {
// prevent matching a fnmatch pattern as a regex
// which is a waste of resources
continue;
}

if (StringUtils::isMatch($realPath, '#' . preg_quote($excludePath, '#') . '#')) {
return false;
}
}
Expand All @@ -133,14 +138,8 @@ private function addFilterWithExcludedPaths(Finder $finder): void
*/
private function normalizeForFnmatch(string $path): string
{
// ends with *
if (StringUtils::isMatch($path, AsteriskMatch::ONLY_ENDS_WITH_ASTERISK_REGEX)) {
return '*' . $path;
}

// starts with *
if (StringUtils::isMatch($path, AsteriskMatch::ONLY_STARTS_WITH_ASTERISK_REGEX)) {
return $path . '*';
if (str_ends_with($path, '*') || str_starts_with($path, '*')) {
return '*' . trim($path, '*') . '*';
}

return $path;
Expand Down

0 comments on commit 21a233d

Please sign in to comment.