-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FileExcluder: Improve performance of isExcludedFromAnalysing
- Loading branch information
Showing
1 changed file
with
24 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,15 @@ class FileExcluder | |
* | ||
* @var string[] | ||
*/ | ||
private array $analyseExcludes; | ||
private array $literalAnalyseExcludes = []; | ||
|
||
/** | ||
* fnmatch() patterns to use for excluding files and directories from analysing | ||
* @var string[] | ||
*/ | ||
private array $fnmatchAnalyseExcludes = []; | ||
|
||
private int $fnmatchFlags; | ||
|
||
/** | ||
* @param FileHelper $fileHelper | ||
|
@@ -23,7 +31,7 @@ public function __construct( | |
array $stubFiles | ||
) | ||
{ | ||
$this->analyseExcludes = array_map(function (string $exclude) use ($fileHelper): string { | ||
foreach (array_merge($analyseExcludes, $stubFiles) as $exclude) { | ||
$len = strlen($exclude); | ||
$trailingDirSeparator = ($len > 0 && in_array($exclude[$len - 1], ['\\', '/'], true)); | ||
|
||
|
@@ -34,28 +42,29 @@ public function __construct( | |
} | ||
|
||
if ($this->isFnmatchPattern($normalized)) { | ||
return $normalized; | ||
$this->fnmatchAnalyseExcludes[] = $normalized; | ||
} else { | ||
$this->literalAnalyseExcludes[] = $fileHelper->absolutizePath($normalized); | ||
} | ||
} | ||
|
||
return $fileHelper->absolutizePath($normalized); | ||
}, array_merge($analyseExcludes, $stubFiles)); | ||
$isWindows = DIRECTORY_SEPARATOR === '\\'; | ||
if ($isWindows) { | ||
$this->fnmatchFlags = FNM_NOESCAPE | FNM_CASEFOLD; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dktapps
Author
Contributor
|
||
} else { | ||
$this->fnmatchFlags = 0; | ||
} | ||
} | ||
|
||
public function isExcludedFromAnalysing(string $file): bool | ||
{ | ||
foreach ($this->analyseExcludes as $exclude) { | ||
foreach ($this->literalAnalyseExcludes as $exclude) { | ||
if (strpos($file, $exclude) === 0) { | ||
return true; | ||
} | ||
|
||
$isWindows = DIRECTORY_SEPARATOR === '\\'; | ||
if ($isWindows) { | ||
$fnmatchFlags = FNM_NOESCAPE | FNM_CASEFOLD; | ||
} else { | ||
$fnmatchFlags = 0; | ||
} | ||
|
||
if ($this->isFnmatchPattern($exclude) && fnmatch($exclude, $file, $fnmatchFlags)) { | ||
} | ||
foreach ($this->fnmatchAnalyseExcludes as $exclude) { | ||
if (fnmatch($exclude, $file, $this->fnmatchFlags)) { | ||
return true; | ||
} | ||
} | ||
|
Why do we need the new property? Could this still be a local variable in isExcludedFromAnalysing?