2020use PhpSpec \Event \SuiteEvent ;
2121use SebastianBergmann \CodeCoverage \CodeCoverage ;
2222use SebastianBergmann \CodeCoverage \Report ;
23+ use SebastianBergmann \FileIterator \Facade as FileIteratorFacade ;
2324use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
2425
2526use function gettype ;
@@ -138,10 +139,6 @@ public function beforeExample(ExampleEvent $event): void
138139 $ this ->coverage ->start ($ name );
139140 }
140141
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- */
145142 public function beforeSuite (SuiteEvent $ event ): void
146143 {
147144 if ($ this ->skipCoverage ) {
@@ -150,22 +147,36 @@ public function beforeSuite(SuiteEvent $event): void
150147
151148 $ filter = $ this ->coverage ->filter ();
152149
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 ' ];
159154 foreach ($ this ->options ['blacklist ' ] as $ option ) {
160155 $ 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+ }
163165 }
164166
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+ }
169180 }
170181 }
171182
@@ -193,7 +204,7 @@ public function setOptions(array $options): void
193204 /**
194205 * @param array<string, string>|string $option
195206 *
196- * @return array{directory:string, prefix:string, suffix:string}
207+ * @return array{directory:non-empty- string, prefix:string, suffix:string}
197208 */
198209 protected function filterDirectoryParams ($ option ): array
199210 {
@@ -208,7 +219,7 @@ protected function filterDirectoryParams($option): array
208219 ));
209220 }
210221
211- if (! isset ($ option ['directory ' ])) {
222+ if (empty ($ option ['directory ' ])) {
212223 throw new ConfigurationException ('Missing required directory path. ' );
213224 }
214225
0 commit comments