Skip to content

Commit 8359339

Browse files
SanderMullerclaude
authored andcommitted
Check the literal text before the directory form of an exclude pattern
A file under a directory that the pattern matches contains the literal text the pattern ends with, followed by a separator. str_contains() on that text rules out most paths before the second fnmatch() call. The check is off on Windows, which matches case-insensitively. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a1ce7f0 commit 8359339

2 files changed

Lines changed: 48 additions & 13 deletions

File tree

‎src/File/FileExcluder.php‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use function is_dir;
99
use function is_file;
1010
use function preg_match;
11+
use function str_contains;
1112
use function str_ends_with;
1213
use function str_starts_with;
1314
use function strlen;
@@ -35,8 +36,10 @@ final class FileExcluder
3536
private array $literalAnalyseFilesExcludes = [];
3637

3738
/**
38-
* fnmatch() patterns to use for excluding files and directories from analysing
39-
* @var string[]
39+
* fnmatch() patterns to use for excluding files and directories from analysing, each with text
40+
* a matching path has to contain. Checking that text is cheaper than fnmatch().
41+
*
42+
* @var list<array{string, string}>
4043
*/
4144
private array $fnmatchAnalyseExcludes = [];
4245

@@ -50,6 +53,13 @@ public function __construct(
5053
array $analyseExcludes,
5154
)
5255
{
56+
$isWindows = DIRECTORY_SEPARATOR === '\\';
57+
if ($isWindows) {
58+
$this->fnmatchFlags = FNM_NOESCAPE | FNM_CASEFOLD;
59+
} else {
60+
$this->fnmatchFlags = 0;
61+
}
62+
5363
foreach ($analyseExcludes as $exclude) {
5464
$len = strlen($exclude);
5565
$trailingDirSeparator = ($len > 0 && in_array($exclude[$len - 1], ['\\', '/'], true));
@@ -63,12 +73,19 @@ public function __construct(
6373
if (self::isFnmatchPattern($normalized)) {
6474
// a path that ends with a separator names a directory, and no file matches it
6575
if (!$trailingDirSeparator) {
66-
$this->fnmatchAnalyseExcludes[] = $normalized;
76+
$this->fnmatchAnalyseExcludes[] = [$normalized, ''];
6777
}
6878

6979
// a directory the pattern matches excludes the files in it, as a literal directory does
7080
if (!str_ends_with($normalized, '*')) {
71-
$this->fnmatchAnalyseExcludes[] = ($trailingDirSeparator ? $normalized : $normalized . DIRECTORY_SEPARATOR) . '*';
81+
$directory = $trailingDirSeparator ? $normalized : $normalized . DIRECTORY_SEPARATOR;
82+
83+
// the path of a file in such a directory contains the literal text the pattern ends
84+
// with, and the separator after it - not provable on Windows, which matches case-insensitively
85+
$this->fnmatchAnalyseExcludes[] = [
86+
$directory . '*',
87+
!$isWindows && preg_match('~[^*?[\]]*$~', $directory, $matches) === 1 ? $matches[0] : '',
88+
];
7289
}
7390
} else {
7491
if (is_file($normalized)) {
@@ -82,13 +99,6 @@ public function __construct(
8299
}
83100
}
84101
}
85-
86-
$isWindows = DIRECTORY_SEPARATOR === '\\';
87-
if ($isWindows) {
88-
$this->fnmatchFlags = FNM_NOESCAPE | FNM_CASEFOLD;
89-
} else {
90-
$this->fnmatchFlags = 0;
91-
}
92102
}
93103

94104
public function isExcludedFromAnalysing(string $file): bool
@@ -103,8 +113,8 @@ public function isExcludedFromAnalysing(string $file): bool
103113
return true;
104114
}
105115
}
106-
foreach ($this->fnmatchAnalyseExcludes as $exclude) {
107-
if (fnmatch($exclude, $file, $this->fnmatchFlags)) {
116+
foreach ($this->fnmatchAnalyseExcludes as [$pattern, $mustContain]) {
117+
if (str_contains($file, $mustContain) && fnmatch($pattern, $file, $this->fnmatchFlags)) {
108118
return true;
109119
}
110120
}

‎tests/PHPStan/File/FileExcluderTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,31 @@ public static function dataExcludeOnUnix(): array
256256
['/project/src/*Test'],
257257
true,
258258
],
259+
[
260+
'/project/src/FooTest',
261+
['/project/src/*Test'],
262+
true,
263+
],
264+
[
265+
'/project/src/FooTests/Bar.php',
266+
['/project/src/*Test'],
267+
false,
268+
],
269+
[
270+
'/project/src/Foo1/Bar.php',
271+
['/project/src/Foo?'],
272+
true,
273+
],
274+
[
275+
'/project/src/FooA/Bar.php',
276+
['/project/src/Foo[AB]'],
277+
true,
278+
],
279+
[
280+
'/project/src/FooC/Bar.php',
281+
['/project/src/Foo[AB]'],
282+
false,
283+
],
259284
];
260285
}
261286

0 commit comments

Comments
 (0)