Passed
Push — master ( f819c0...dd8ea4 )
by Doug
09:19
created

Extension   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Test Coverage

Coverage 87.62%

Importance

Changes 12
Bugs 0 Features 0
Metric Value
eloc 171
dl 0
loc 257
ccs 177
cts 202
cp 0.8762
rs 10
c 12
b 0
f 0
wmc 18

8 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 2 1
A getConfigKey() 0 3 1
B configure() 0 98 1
A process() 0 26 4
A load() 0 10 1
A initCodeCoverageV678() 0 30 2
A initCodeCoverage() 0 8 2
B initCodeCoverageV9() 0 48 6
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Code Coverage Extension for Behat.
6
 *
7
 * @copyright 2013 Anthon Pang
8
 *
9
 * @license BSD-2-Clause
10
 */
11
12
namespace DVDoug\Behat\CodeCoverage;
13
14
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
15
use Behat\Testwork\ServiceContainer\ExtensionManager;
16
use DVDoug\Behat\CodeCoverage\Subscriber\EventSubscriber;
17
use SebastianBergmann\CodeCoverage\CodeCoverage;
18
use SebastianBergmann\CodeCoverage\Driver\Driver;
19
use SebastianBergmann\CodeCoverage\Filter;
20
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverAvailableException;
21
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverWithPathCoverageSupportAvailableException;
22
use SebastianBergmann\CodeCoverage\RuntimeException;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCoverage\RuntimeException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
24
use Symfony\Component\Config\FileLocator;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
29
use Symfony\Component\DependencyInjection\Reference;
30
31
/**
32
 * Code coverage extension.
33
 *
34
 * @author Anthon Pang <[email protected]>
35
 */
36
class Extension implements ExtensionInterface
37
{
38
    /**
39
     * {@inheritdoc}
40
     */
41 28
    public function initialize(ExtensionManager $extensionManager): void
42
    {
43 28
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 28
    public function load(ContainerBuilder $container, array $config): void
49
    {
50 28
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/Resources/config'));
51
52 28
        $servicesFile = 'services.xml';
53 28
        $loader->load($servicesFile);
54
55 28
        $container->setParameter('behat.code_coverage.config.filter', $config['filter']);
56 28
        $container->setParameter('behat.code_coverage.config.branchAndPathCoverage', $config['branchAndPathCoverage']);
57 28
        $container->setParameter('behat.code_coverage.config.reports', $config['reports'] ?? []);
58 28
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 56
    public function configure(ArrayNodeDefinition $builder): void
64
    {
65
        $builder
66 56
            ->children()
67 56
                ->booleanNode('branchAndPathCoverage')
68 56
                  ->defaultNull() // use null to mean auto
69 56
                ->end()
70 56
                ->arrayNode('filter')
0 ignored issues
show
Bug introduced by
The method arrayNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                ->/** @scrutinizer ignore-call */ arrayNode('filter')
Loading history...
71 56
                    ->addDefaultsIfNotSet()
72 56
                    ->children()
73 56
                        ->scalarNode('includeUncoveredFiles')
74 56
                            ->defaultTrue()
75 56
                        ->end()
76 56
                        ->scalarNode('processUncoveredFiles')
77 56
                            ->defaultFalse()
78 56
                        ->end()
79 56
                        ->arrayNode('include')
80 56
                            ->addDefaultsIfNotSet()
81 56
                            ->children()
82 56
                                ->arrayNode('directories')
83 56
                                   ->useAttributeAsKey('name')
84 56
                                   ->normalizeKeys(false)
85 56
                                   ->prototype('array')
86 56
                                       ->children()
87 56
                                           ->scalarNode('prefix')->defaultValue('')->end()
88 56
                                           ->scalarNode('suffix')->defaultValue('.php')->end()
89 56
                                       ->end()
90 56
                                   ->end()
91 56
                                ->end()
92 56
                                ->arrayNode('files')
93 56
                                   ->prototype('scalar')->end()
94 56
                                ->end()
95 56
                            ->end()
96 56
                        ->end()
97 56
                        ->arrayNode('exclude')
98 56
                            ->addDefaultsIfNotSet()
99 56
                            ->children()
100 56
                                ->arrayNode('directories')
101 56
                                   ->useAttributeAsKey('name')
102 56
                                   ->normalizeKeys(false)
103 56
                                   ->prototype('array')
104 56
                                       ->children()
105 56
                                           ->scalarNode('prefix')->defaultValue('')->end()
106 56
                                           ->scalarNode('suffix')->defaultValue('.php')->end()
107 56
                                       ->end()
108 56
                                   ->end()
109 56
                                ->end()
110 56
                                ->arrayNode('files')
111 56
                                   ->prototype('scalar')->end()
112 56
                                ->end()
113 56
                            ->end()
114 56
                        ->end()
115 56
                    ->end()
116 56
                ->end()
117 56
                ->arrayNode('reports')
118 56
                    ->children()
119 56
                        ->arrayNode('clover')
120 56
                            ->children()
121 56
                                ->scalarNode('name')->defaultNull()->end()
122 56
                                ->scalarNode('target')->isRequired()->cannotBeEmpty()->end()
123 56
                            ->end()
124 56
                        ->end()
125 56
                        ->arrayNode('crap4j')
126 56
                            ->children()
127 56
                                ->scalarNode('name')->defaultNull()->end()
128 56
                                ->scalarNode('target')->isRequired()->cannotBeEmpty()->end()
129 56
                            ->end()
130 56
                        ->end()
131 56
                        ->arrayNode('html')
132 56
                            ->children()
133 56
                                ->scalarNode('target')->isRequired()->cannotBeEmpty()->end()
134 56
                                ->scalarNode('lowUpperBound')->defaultValue(50)->end()
135 56
                                ->scalarNode('highLowerBound')->defaultValue(90)->end()
136 56
                            ->end()
137 56
                        ->end()
138 56
                        ->arrayNode('php')
139 56
                            ->children()
140 56
                                ->scalarNode('target')->isRequired()->cannotBeEmpty()->end()
141 56
                            ->end()
142 56
                        ->end()
143 56
                        ->arrayNode('text')
144 56
                            ->children()
145 56
                                ->booleanNode('showColors')->defaultValue(false)->end()
146 56
                                ->scalarNode('lowUpperBound')->defaultValue(50)->end()
147 56
                                ->scalarNode('highLowerBound')->defaultValue(90)->end()
148 56
                                ->booleanNode('showOnlySummary')->defaultValue(false)->end()
149 56
                                ->booleanNode('showUncoveredFiles')->defaultValue(false)->end()
150 56
                            ->end()
151 56
                        ->end()
152 56
                        ->arrayNode('xml')
153 56
                            ->children()
154 56
                                ->scalarNode('target')->isRequired()->cannotBeEmpty()->end()
155 56
                            ->end()
156 56
                        ->end()
157 56
                    ->end()
158 56
                ->end()
159 56
            ->end()
160 56
        ->end();
161 56
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 56
    public function getConfigKey()
167
    {
168 56
        return 'code_coverage';
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 112
    public function process(ContainerBuilder $container): void
175
    {
176
        /** @var InputInterface $input */
177 112
        $input = $container->get('cli.input');
178
179
        /** @var OutputInterface $output */
180 112
        $output = $container->get('cli.output');
181
182 112
        $filterConfig = $container->getParameter('behat.code_coverage.config.filter');
183 112
        $branchPathConfig = $container->getParameter('behat.code_coverage.config.branchAndPathCoverage');
184
185 112
        $canCollectCodeCoverage = true;
186
        try {
187 112
            $this->initCodeCoverage(new Filter(), $filterConfig, null, $output);
188
189 78
            $codeCoverageDefinition = $container->getDefinition(CodeCoverage::class);
190 78
            $filterDefinition = $container->getDefinition(Filter::class);
191 78
            $codeCoverageDefinition->setFactory([new Reference(self::class), 'initCodeCoverage']);
192 84
            $codeCoverageDefinition->setArguments([$filterDefinition, $filterConfig, $branchPathConfig, $output]);
193 28
        } catch (NoCodeCoverageDriverAvailableException | RuntimeException $e) {
194 28
            $output->writeln('<comment>No code coverage driver is available</comment>');
195 28
            $canCollectCodeCoverage = false;
196
        }
197
198 112
        if (!$canCollectCodeCoverage || $input->hasParameterOption('--no-coverage')) {
199 52
            $container->getDefinition(EventSubscriber::class)->setArgument('$coverage', null);
200
        }
201 112
    }
202
203 84
    public function initCodeCoverage(Filter $filter, array $filterConfig, ?bool $branchPathConfig, OutputInterface $output): CodeCoverage
204
    {
205 78
        $driverClassReflection = new \ReflectionClass(Driver::class);
206 84
        if ($driverClassReflection->isInterface()) {
207 78
            return $this->initCodeCoverageV678($filter, $filterConfig, $branchPathConfig, $output);
208
        }
209
210 6
        return $this->initCodeCoverageV9($filter, $filterConfig, $branchPathConfig, $output);
211
    }
212
213 6
    public function initCodeCoverageV9(Filter $filter, array $filterConfig, ?bool $branchPathConfig, OutputInterface $output): CodeCoverage
214
    {
215
        // set up filter
216 6
        array_walk($filterConfig['include']['directories'], static function (array $dir, string $path, Filter $filter): void {
217 6
            $filter->includeDirectory($path, $dir['suffix'], $dir['prefix']);
218 6
        }, $filter);
219
220 6
        array_walk($filterConfig['include']['files'], static function (string $file, string $key, Filter $filter): void {
221 6
            $filter->includeFile($file);
222 6
        }, $filter);
223
224 6
        array_walk($filterConfig['exclude']['directories'], static function (array $dir, string $path, Filter $filter): void {
225 6
            $filter->excludeDirectory($path, $dir['suffix'], $dir['prefix']);
226 6
        }, $filter);
227
228 6
        array_walk($filterConfig['exclude']['files'], static function (string $file, string $key, Filter $filter): void {
229 6
            $filter->excludeFile($file);
230 6
        }, $filter);
231
232
        // see if we can get a driver
233 6
        $driver = Driver::forLineCoverage($filter);
234 6
        if ($branchPathConfig !== false) {
235
            try {
236 6
                $driver = Driver::forLineAndPathCoverage($filter);
237 3
            } catch (NoCodeCoverageDriverWithPathCoverageSupportAvailableException $e) {
238
                // fallback driver is already set
239 3
                if ($branchPathConfig === true) { //only warn if explicitly enabled
0 ignored issues
show
introduced by
The condition $branchPathConfig === true is always true.
Loading history...
240 1
                    $output->writeln(sprintf('<info>%s does not support collecting branch and path data</info>', $driver->nameAndVersion()));
241
                }
242
            }
243
        }
244
245
        // and init coverage
246 6
        $codeCoverage = new CodeCoverage($driver, $filter);
247
248 6
        if ($filterConfig['includeUncoveredFiles']) {
249 2
            $codeCoverage->includeUncoveredFiles();
250
        } else {
251 4
            $codeCoverage->excludeUncoveredFiles();
252
        }
253
254 6
        if ($filterConfig['processUncoveredFiles']) {
255 2
            $codeCoverage->processUncoveredFiles();
256
        } else {
257 4
            $codeCoverage->doNotProcessUncoveredFiles();
258
        }
259
260 6
        return $codeCoverage;
261
    }
262
263 78
    public function initCodeCoverageV678(Filter $filter, array $config, ?bool $branchPathConfig, OutputInterface $output): CodeCoverage
264
    {
265 78
        if ($branchPathConfig === true) { //only warn if explicitly enabled
266 26
            $output->writeln('<info>php-code-coverage v9+ is needed to support collecting branch and path data</info>');
267
        }
268
269
        // set up filter
270 24
        array_walk($config['include']['directories'], static function (array $dir, string $path, Filter $filter): void {
271 78
            $filter->addDirectoryToWhitelist($path, $dir['suffix'], $dir['prefix']);
0 ignored issues
show
Bug introduced by
The method addDirectoryToWhitelist() does not exist on SebastianBergmann\CodeCoverage\Filter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
            $filter->/** @scrutinizer ignore-call */ 
272
                     addDirectoryToWhitelist($path, $dir['suffix'], $dir['prefix']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
272 78
        }, $filter);
273
274 24
        array_walk($config['include']['files'], static function (string $file, string $key, Filter $filter): void {
275 78
            $filter->addFileToWhitelist($file);
0 ignored issues
show
Bug introduced by
The method addFileToWhitelist() does not exist on SebastianBergmann\CodeCoverage\Filter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

275
            $filter->/** @scrutinizer ignore-call */ 
276
                     addFileToWhitelist($file);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
276 78
        }, $filter);
277
278 24
        array_walk($config['exclude']['directories'], static function (array $dir, string $path, Filter $filter): void {
279 78
            $filter->removeDirectoryFromWhitelist($path, $dir['suffix'], $dir['prefix']);
0 ignored issues
show
Bug introduced by
The method removeDirectoryFromWhitelist() does not exist on SebastianBergmann\CodeCoverage\Filter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

279
            $filter->/** @scrutinizer ignore-call */ 
280
                     removeDirectoryFromWhitelist($path, $dir['suffix'], $dir['prefix']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
280 78
        }, $filter);
281
282 24
        array_walk($config['exclude']['files'], static function (string $file, string $key, Filter $filter): void {
283 78
            $filter->removeFileFromWhitelist($file);
0 ignored issues
show
Bug introduced by
The method removeFileFromWhitelist() does not exist on SebastianBergmann\CodeCoverage\Filter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

283
            $filter->/** @scrutinizer ignore-call */ 
284
                     removeFileFromWhitelist($file);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
284 78
        }, $filter);
285
286
        // and init coverage
287 78
        $codeCoverage = new CodeCoverage(null, $filter);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type SebastianBergmann\CodeCoverage\Driver\Driver expected by parameter $driver of SebastianBergmann\CodeCo...Coverage::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

287
        $codeCoverage = new CodeCoverage(/** @scrutinizer ignore-type */ null, $filter);
Loading history...
288
289 78
        $codeCoverage->setAddUncoveredFilesFromWhitelist($config['includeUncoveredFiles']);
0 ignored issues
show
Bug introduced by
The method setAddUncoveredFilesFromWhitelist() does not exist on SebastianBergmann\CodeCoverage\CodeCoverage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

289
        $codeCoverage->/** @scrutinizer ignore-call */ 
290
                       setAddUncoveredFilesFromWhitelist($config['includeUncoveredFiles']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
290 78
        $codeCoverage->setProcessUncoveredFilesFromWhitelist($config['processUncoveredFiles']);
0 ignored issues
show
Bug introduced by
The method setProcessUncoveredFilesFromWhitelist() does not exist on SebastianBergmann\CodeCoverage\CodeCoverage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

290
        $codeCoverage->/** @scrutinizer ignore-call */ 
291
                       setProcessUncoveredFilesFromWhitelist($config['processUncoveredFiles']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
291
292 78
        return $codeCoverage;
293
    }
294
}
295