Skip to content

Commit bb705f0

Browse files
authored
Exclude the files under a directory that an exclude pattern matches (#7175)
1 parent 6dba7d2 commit bb705f0

2 files changed

Lines changed: 111 additions & 12 deletions

File tree

‎src/File/FileExcluder.php‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use function is_dir;
99
use function is_file;
1010
use function preg_match;
11+
use function str_contains;
12+
use function str_ends_with;
1113
use function str_starts_with;
1214
use function strlen;
1315
use function substr;
@@ -34,8 +36,10 @@ final class FileExcluder
3436
private array $literalAnalyseFilesExcludes = [];
3537

3638
/**
37-
* fnmatch() patterns to use for excluding files and directories from analysing
38-
* @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}>
3943
*/
4044
private array $fnmatchAnalyseExcludes = [];
4145

@@ -49,6 +53,13 @@ public function __construct(
4953
array $analyseExcludes,
5054
)
5155
{
56+
$isWindows = DIRECTORY_SEPARATOR === '\\';
57+
if ($isWindows) {
58+
$this->fnmatchFlags = FNM_NOESCAPE | FNM_CASEFOLD;
59+
} else {
60+
$this->fnmatchFlags = 0;
61+
}
62+
5263
foreach ($analyseExcludes as $exclude) {
5364
$len = strlen($exclude);
5465
$trailingDirSeparator = ($len > 0 && in_array($exclude[$len - 1], ['\\', '/'], true));
@@ -60,7 +71,22 @@ public function __construct(
6071
}
6172

6273
if (self::isFnmatchPattern($normalized)) {
63-
$this->fnmatchAnalyseExcludes[] = $normalized;
74+
// a path that ends with a separator names a directory, and no file matches it
75+
if (!$trailingDirSeparator) {
76+
$this->fnmatchAnalyseExcludes[] = [$normalized, ''];
77+
}
78+
79+
// a directory the pattern matches excludes the files in it, as a literal directory does
80+
if (!str_ends_with($normalized, '*')) {
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+
];
89+
}
6490
} else {
6591
if (is_file($normalized)) {
6692
$this->literalAnalyseFilesExcludes[$normalized] = true;
@@ -73,13 +99,6 @@ public function __construct(
7399
}
74100
}
75101
}
76-
77-
$isWindows = DIRECTORY_SEPARATOR === '\\';
78-
if ($isWindows) {
79-
$this->fnmatchFlags = FNM_NOESCAPE | FNM_CASEFOLD;
80-
} else {
81-
$this->fnmatchFlags = 0;
82-
}
83102
}
84103

85104
public function isExcludedFromAnalysing(string $file): bool
@@ -94,8 +113,8 @@ public function isExcludedFromAnalysing(string $file): bool
94113
return true;
95114
}
96115
}
97-
foreach ($this->fnmatchAnalyseExcludes as $exclude) {
98-
if (fnmatch($exclude, $file, $this->fnmatchFlags)) {
116+
foreach ($this->fnmatchAnalyseExcludes as [$pattern, $mustContain]) {
117+
if (str_contains($file, $mustContain) && fnmatch($pattern, $file, $this->fnmatchFlags)) {
99118
return true;
100119
}
101120
}

‎tests/PHPStan/File/FileExcluderTest.php‎

Lines changed: 80 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,71 @@ 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+
],
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+
],
204284
];
205285
}
206286

0 commit comments

Comments
 (0)