2020use PhpSpec \Event \SuiteEvent ;
2121use SebastianBergmann \CodeCoverage \CodeCoverage ;
2222use SebastianBergmann \CodeCoverage \Report ;
23- use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
2423use SebastianBergmann \FileIterator \Facade as FileIteratorFacade ;
24+ use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
2525
2626use function gettype ;
2727use function is_array ;
@@ -139,10 +139,6 @@ public function beforeExample(ExampleEvent $event): void
139139 $ this ->coverage ->start ($ name );
140140 }
141141
142- /**
143- * Note: We use array_map() instead of array_walk() because the latter expects
144- * the callback to take the value as the first and the index as the seconds parameter.
145- */
146142 public function beforeSuite (SuiteEvent $ event ): void
147143 {
148144 if ($ this ->skipCoverage ) {
@@ -151,26 +147,36 @@ public function beforeSuite(SuiteEvent $event): void
151147
152148 $ filter = $ this ->coverage ->filter ();
153149
154- foreach ($ this ->options ['whitelist ' ] as $ option ) {
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 ' ];
154+ foreach ($ this ->options ['blacklist ' ] as $ option ) {
155155 $ settings = $ this ->filterDirectoryParams ($ option );
156-
157- foreach ((new FileIteratorFacade ())->getFilesAsArray ($ settings ['directory ' ], $ settings ['suffix ' ], $ settings ['prefix ' ]) as $ file ) {
158- $ filter ->includeFile ($ file );
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 ' ];
159164 }
160165 }
161166
162- foreach ($ this ->options ['blacklist ' ] as $ option ) {
167+ foreach ($ this ->options ['whitelist ' ] as $ option ) {
163168 $ settings = $ this ->filterDirectoryParams ($ option );
164- foreach ((new FileIteratorFacade )->getFilesAsArray ($ directory , $ suffix , $ prefix ) as $ file ) {
165- $ filter ->excludeFile ($ file );
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 );
166179 }
167- $ filter ->excludeDirectory ($ settings ['directory ' ], $ settings ['suffix ' ], $ settings ['prefix ' ]);
168- }
169-
170- $ filter ->includeFiles ($ this ->options ['whitelist_files ' ]);
171-
172- foreach ($ this ->options ['blacklist_files ' ] as $ option ) {
173- $ filter ->excludeFile ($ option );
174180 }
175181 }
176182
@@ -198,7 +204,7 @@ public function setOptions(array $options): void
198204 /**
199205 * @param array<string, string>|string $option
200206 *
201- * @return array{directory:string, prefix:string, suffix:string}
207+ * @return array{directory:non-empty- string, prefix:string, suffix:string}
202208 */
203209 protected function filterDirectoryParams ($ option ): array
204210 {
@@ -213,7 +219,7 @@ protected function filterDirectoryParams($option): array
213219 ));
214220 }
215221
216- if (! isset ($ option ['directory ' ])) {
222+ if (empty ($ option ['directory ' ])) {
217223 throw new ConfigurationException ('Missing required directory path. ' );
218224 }
219225
0 commit comments