|
14 | 14 |
|
15 | 15 | namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener; |
16 | 16 |
|
| 17 | +use InvalidArgumentException; |
17 | 18 | use PhpSpec\Console\ConsoleIO; |
18 | 19 | use PhpSpec\Event\ExampleEvent; |
19 | 20 | use PhpSpec\Event\SuiteEvent; |
20 | 21 | use SebastianBergmann\CodeCoverage\CodeCoverage; |
21 | 22 | use SebastianBergmann\CodeCoverage\Report; |
22 | 23 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 24 | +use TypeError; |
23 | 25 |
|
| 26 | +use function gettype; |
| 27 | +use function is_array; |
24 | 28 | use function is_string; |
25 | 29 |
|
26 | 30 | /** |
@@ -148,19 +152,15 @@ public function beforeSuite(SuiteEvent $event): void |
148 | 152 | $filter = $this->coverage->filter(); |
149 | 153 |
|
150 | 154 | foreach ($this->options['whitelist'] as $option) { |
151 | | - if (is_string($option)) { |
152 | | - $option = ['directory' => $option]; |
153 | | - } |
154 | | - $option = $option + ['suffix' => '.php', 'prefix' => '']; |
155 | | - $filter->includeDirectory($option['directory'], $option['suffix'], $option['prefix']); |
| 155 | + $settings = $this->filterDirectoryParams($option); |
| 156 | + |
| 157 | + $filter->includeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']); |
156 | 158 | } |
157 | 159 |
|
158 | 160 | foreach ($this->options['blacklist'] as $option) { |
159 | | - if (is_string($option)) { |
160 | | - $option = ['directory' => $option]; |
161 | | - } |
162 | | - $option = $option + ['suffix' => '.php', 'prefix' => '']; |
163 | | - $filter->excludeDirectory($option['directory'], $option['suffix'], $option['prefix']); |
| 161 | + $settings = $this->filterDirectoryParams($option); |
| 162 | + |
| 163 | + $filter->excludeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']); |
164 | 164 | } |
165 | 165 |
|
166 | 166 | $filter->includeFiles($this->options['whitelist_files']); |
@@ -190,4 +190,33 @@ public function setOptions(array $options): void |
190 | 190 | { |
191 | 191 | $this->options = $options + $this->options; |
192 | 192 | } |
| 193 | + |
| 194 | + /** |
| 195 | + * @param array<string, string>|string $option |
| 196 | + * |
| 197 | + * @return array{directory:string, prefix:string, suffix:string} |
| 198 | + */ |
| 199 | + protected function filterDirectoryParams($option): array |
| 200 | + { |
| 201 | + if (is_string($option)) { |
| 202 | + $option = ['directory' => $option]; |
| 203 | + } |
| 204 | + |
| 205 | + if (!is_array($option)) { |
| 206 | + throw new TypeError(sprintf( |
| 207 | + 'Directory filtering options must be a string or an associated array, %s given instead.', |
| 208 | + gettype($option) |
| 209 | + )); |
| 210 | + } |
| 211 | + |
| 212 | + if (!isset($option['directory'])) { |
| 213 | + throw new InvalidArgumentException('Missing required directory path.'); |
| 214 | + } |
| 215 | + |
| 216 | + return [ |
| 217 | + 'directory' => $option['directory'], |
| 218 | + 'suffix' => $option['suffix'] ?? '.php', |
| 219 | + 'prefix' => $option['prefix'] ?? '', |
| 220 | + ]; |
| 221 | + } |
193 | 222 | } |
0 commit comments