20
20
use PhpSpec \Event \SuiteEvent ;
21
21
use SebastianBergmann \CodeCoverage \CodeCoverage ;
22
22
use SebastianBergmann \CodeCoverage \Report ;
23
+ use SebastianBergmann \FileIterator \Facade as FileIteratorFacade ;
23
24
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
24
25
25
26
use function gettype ;
@@ -138,10 +139,6 @@ public function beforeExample(ExampleEvent $event): void
138
139
$ this ->coverage ->start ($ name );
139
140
}
140
141
141
- /**
142
- * Note: We use array_map() instead of array_walk() because the latter expects
143
- * the callback to take the value as the first and the index as the seconds parameter.
144
- */
145
142
public function beforeSuite (SuiteEvent $ event ): void
146
143
{
147
144
if ($ this ->skipCoverage ) {
@@ -150,22 +147,36 @@ public function beforeSuite(SuiteEvent $event): void
150
147
151
148
$ filter = $ this ->coverage ->filter ();
152
149
153
- foreach ($ this ->options ['whitelist ' ] as $ option ) {
154
- $ settings = $ this ->filterDirectoryParams ($ option );
155
-
156
- $ filter ->includeDirectory ($ settings ['directory ' ], $ settings ['suffix ' ], $ settings ['prefix ' ]);
157
- }
158
-
150
+ // We compute the list of file / folder to be excluded
151
+ // If the blacklist contains suffixes and/or prefixes, we extract an
152
+ // exhaustive list of files that match to be added in the excluded list.
153
+ $ excludes = $ this ->options ['blacklist_files ' ];
159
154
foreach ($ this ->options ['blacklist ' ] as $ option ) {
160
155
$ settings = $ this ->filterDirectoryParams ($ option );
161
-
162
- $ filter ->excludeDirectory ($ settings ['directory ' ], $ settings ['suffix ' ], $ settings ['prefix ' ]);
156
+ if (!empty ($ settings ['suffix ' ]) || !empty ($ settings ['prefix ' ])) {
157
+ $ excludes = $ excludes + (new FileIteratorFacade ())->getFilesAsArray (
158
+ $ settings ['directory ' ],
159
+ $ settings ['suffix ' ],
160
+ $ settings ['prefix ' ]
161
+ );
162
+ } else {
163
+ $ excludes [] = $ settings ['directory ' ];
164
+ }
163
165
}
164
166
165
- $ filter ->includeFiles ($ this ->options ['whitelist_files ' ]);
166
-
167
- foreach ($ this ->options ['blacklist_files ' ] as $ option ) {
168
- $ filter ->excludeFile ($ option );
167
+ foreach ($ this ->options ['whitelist ' ] as $ option ) {
168
+ $ settings = $ this ->filterDirectoryParams ($ option );
169
+ $ fileIterator = (new FileIteratorFacade ())->getFilesAsArray (
170
+ [$ settings ['directory ' ]] + $ this ->options ['whitelist_files ' ],
171
+ $ settings ['suffix ' ],
172
+ $ settings ['prefix ' ],
173
+ // We exclude the files from the previously built list.
174
+ $ excludes
175
+ );
176
+
177
+ foreach ($ fileIterator as $ file ) {
178
+ $ filter ->includeFile ($ file );
179
+ }
169
180
}
170
181
}
171
182
@@ -193,7 +204,7 @@ public function setOptions(array $options): void
193
204
/**
194
205
* @param array<string, string>|string $option
195
206
*
196
- * @return array{directory:string, prefix:string, suffix:string}
207
+ * @return array{directory:non-empty- string, prefix:string, suffix:string}
197
208
*/
198
209
protected function filterDirectoryParams ($ option ): array
199
210
{
@@ -208,7 +219,7 @@ protected function filterDirectoryParams($option): array
208
219
));
209
220
}
210
221
211
- if (! isset ($ option ['directory ' ])) {
222
+ if (empty ($ option ['directory ' ])) {
212
223
throw new ConfigurationException ('Missing required directory path. ' );
213
224
}
214
225
0 commit comments