88use function is_dir ;
99use function is_file ;
1010use function preg_match ;
11+ use function str_contains ;
12+ use function str_ends_with ;
1113use function str_starts_with ;
1214use function strlen ;
1315use 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 }
0 commit comments