4
4
5
5
use function fnmatch ;
6
6
use function in_array ;
7
+ use function is_dir ;
8
+ use function is_file ;
7
9
use function preg_match ;
8
10
use function str_starts_with ;
9
11
use function strlen ;
@@ -15,12 +17,26 @@ class FileExcluder
15
17
{
16
18
17
19
/**
18
- * Directories to exclude from analysing
20
+ * Paths to exclude from analysing
19
21
*
20
22
* @var string[]
21
23
*/
22
24
private array $ literalAnalyseExcludes = [];
23
25
26
+ /**
27
+ * Directories to exclude from analysing
28
+ *
29
+ * @var string[]
30
+ */
31
+ private array $ literalAnalyseDirectoryExcludes = [];
32
+
33
+ /**
34
+ * Files to exclude from analysing
35
+ *
36
+ * @var string[]
37
+ */
38
+ private array $ literalAnalyseFilesExcludes = [];
39
+
24
40
/**
25
41
* fnmatch() patterns to use for excluding files and directories from analysing
26
42
* @var string[]
@@ -35,6 +51,7 @@ class FileExcluder
35
51
public function __construct (
36
52
FileHelper $ fileHelper ,
37
53
array $ analyseExcludes ,
54
+ private bool $ noImplicitWildcard ,
38
55
)
39
56
{
40
57
foreach ($ analyseExcludes as $ exclude ) {
@@ -47,10 +64,22 @@ public function __construct(
47
64
$ normalized .= DIRECTORY_SEPARATOR ;
48
65
}
49
66
50
- if ($ this -> isFnmatchPattern ($ normalized )) {
67
+ if (self :: isFnmatchPattern ($ normalized )) {
51
68
$ this ->fnmatchAnalyseExcludes [] = $ normalized ;
52
69
} else {
53
- $ this ->literalAnalyseExcludes [] = $ fileHelper ->absolutizePath ($ normalized );
70
+ if ($ this ->noImplicitWildcard ) {
71
+ if (is_file ($ normalized )) {
72
+ $ this ->literalAnalyseFilesExcludes [] = $ normalized ;
73
+ } elseif (is_dir ($ normalized )) {
74
+ if (!$ trailingDirSeparator ) {
75
+ $ normalized .= DIRECTORY_SEPARATOR ;
76
+ }
77
+
78
+ $ this ->literalAnalyseDirectoryExcludes [] = $ normalized ;
79
+ }
80
+ } else {
81
+ $ this ->literalAnalyseExcludes [] = $ fileHelper ->absolutizePath ($ normalized );
82
+ }
54
83
}
55
84
}
56
85
@@ -69,6 +98,18 @@ public function isExcludedFromAnalysing(string $file): bool
69
98
return true ;
70
99
}
71
100
}
101
+ if ($ this ->noImplicitWildcard ) {
102
+ foreach ($ this ->literalAnalyseDirectoryExcludes as $ exclude ) {
103
+ if (str_starts_with ($ file , $ exclude )) {
104
+ return true ;
105
+ }
106
+ }
107
+ foreach ($ this ->literalAnalyseFilesExcludes as $ exclude ) {
108
+ if ($ file === $ exclude ) {
109
+ return true ;
110
+ }
111
+ }
112
+ }
72
113
foreach ($ this ->fnmatchAnalyseExcludes as $ exclude ) {
73
114
if (fnmatch ($ exclude , $ file , $ this ->fnmatchFlags )) {
74
115
return true ;
@@ -78,7 +119,7 @@ public function isExcludedFromAnalysing(string $file): bool
78
119
return false ;
79
120
}
80
121
81
- private function isFnmatchPattern (string $ path ): bool
122
+ public static function isFnmatchPattern (string $ path ): bool
82
123
{
83
124
return preg_match ('~[*?[\]]~ ' , $ path ) > 0 ;
84
125
}
0 commit comments