Skip to content

Commit 69bcc77

Browse files
SanderMullerclaude
andcommitted
Exclude the files under a directory that an exclude pattern matches
A literal exclude path that names a directory excludes the files under it. A pattern had to match the whole file path, so themes/*/node_modules/ and themes/*/node_modules excluded nothing under those directories. Each pattern that does not end with * now also gets the form <pattern>/*. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 07d57b7 commit 69bcc77

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎src/File/FileExcluder.php‎

Lines changed: 10 additions & 1 deletion
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_ends_with;
1112
use function str_starts_with;
1213
use function strlen;
1314
use function substr;
@@ -60,7 +61,15 @@ public function __construct(
6061
}
6162

6263
if (self::isFnmatchPattern($normalized)) {
63-
$this->fnmatchAnalyseExcludes[] = $normalized;
64+
// a path that ends with a separator names a directory, and no file matches it
65+
if (!$trailingDirSeparator) {
66+
$this->fnmatchAnalyseExcludes[] = $normalized;
67+
}
68+
69+
// a directory the pattern matches excludes the files in it, as a literal directory does
70+
if (!str_ends_with($normalized, '*')) {
71+
$this->fnmatchAnalyseExcludes[] = ($trailingDirSeparator ? $normalized : $normalized . DIRECTORY_SEPARATOR) . '*';
72+
}
6473
} else {
6574
if (is_file($normalized)) {
6675
$this->literalAnalyseFilesExcludes[$normalized] = true;

‎tests/PHPStan/File/FileExcluderTest.php‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ public static function dataExcludeOnWindows(): array
113113
['c:\etc\phpstan*'],
114114
true,
115115
],
116+
[
117+
'C:\Project\themes\foo\node_modules\baz.php',
118+
['C:/Project/themes/*/node_modules/'],
119+
true,
120+
],
121+
[
122+
'C:\Project\themes\foo\node_modules\baz.php',
123+
['C:/Project/themes/*/node_modules'],
124+
true,
125+
],
126+
[
127+
'C:\Project\themes\foo\node_modules-old\baz.php',
128+
['C:/Project/themes/*/node_modules'],
129+
false,
130+
],
116131
];
117132
}
118133

@@ -201,6 +216,46 @@ public static function dataExcludeOnUnix(): array
201216
['/etc/phpstan*'],
202217
true,
203218
],
219+
[
220+
'/project/themes/foo/node_modules/bar/baz.php',
221+
['/project/themes/*/node_modules/'],
222+
true,
223+
],
224+
[
225+
'/project/themes/foo/node_modules/bar/baz.php',
226+
['/project/themes/*/node_modules'],
227+
true,
228+
],
229+
[
230+
'/project/themes/foo/node_modules-old/baz.php',
231+
['/project/themes/*/node_modules'],
232+
false,
233+
],
234+
[
235+
'/project/themes/foo/node_modules-old/baz.php',
236+
['/project/themes/*/node_modules/'],
237+
false,
238+
],
239+
[
240+
'/project/themes/foo/src/baz.php',
241+
['/project/themes/*/node_modules'],
242+
false,
243+
],
244+
[
245+
'/project/src/FooTest.php',
246+
['/project/src/*Test.php'],
247+
true,
248+
],
249+
[
250+
'/project/src/Foo.php',
251+
['/project/src/*Test.php'],
252+
false,
253+
],
254+
[
255+
'/project/src/FooTest/Bar.php',
256+
['/project/src/*Test'],
257+
true,
258+
],
204259
];
205260
}
206261

0 commit comments

Comments
 (0)