Skip to content

Commit fc0886d

Browse files
committed
chore: Handle blacklist and blacklist_files options.
Since the phpunit/php-code-coverage isn't able to exclude any folder from version 11, we must handle that logic differently. We now compute a list of files and folder to be excluded and we give it to the filter.
1 parent 61b4936 commit fc0886d

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

spec/Listener/CodeCoverageListenerSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function let(ConsoleIO $io, Driver $driver)
3535
public function it_can_process_all_directory_filtering_options(SuiteEvent $event)
3636
{
3737
$this->setOptions([
38-
'blacklist' => [
38+
'whitelist' => [
3939
'src',
4040
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
4141
['directory' => 'src', 'suffix' => 'Test.php'],

src/Listener/CodeCoverageListener.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)