Passed
Push — master ( 462a2e...65d36c )
by Doug
02:19
created

Extension::initCodeCoverage()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7.0368

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 32
c 3
b 0
f 0
nc 24
nop 5
dl 0
loc 52
ccs 30
cts 33
cp 0.9091
crap 7.0368
rs 8.4746

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Behat Code Coverage
4
 */
5
declare(strict_types=1);
6
7
namespace DVDoug\Behat\CodeCoverage;
8
9
use function array_walk;
10
use Behat\Testwork\Cli\ServiceContainer\CliExtension;
11
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
12
use Behat\Testwork\ServiceContainer\ExtensionManager;
13
use Composer\InstalledVersions;
14
use Composer\Semver\VersionParser;
15
use DVDoug\Behat\CodeCoverage\Subscriber\EventSubscriber;
16
use SebastianBergmann\CodeCoverage\CodeCoverage;
17
use SebastianBergmann\CodeCoverage\Driver\Driver;
18
use SebastianBergmann\CodeCoverage\Driver\Selector;
19
use SebastianBergmann\CodeCoverage\Driver\Xdebug2NotEnabledException;
20
use SebastianBergmann\CodeCoverage\Driver\Xdebug3NotEnabledException;
21
use SebastianBergmann\CodeCoverage\Driver\XdebugNotAvailableException;
22
use SebastianBergmann\CodeCoverage\Driver\XdebugNotEnabledException;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCo...ebugNotEnabledException 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 SebastianBergmann\CodeCoverage\Filter;
24
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverAvailableException;
25
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverWithPathCoverageSupportAvailableException;
26
use function sprintf;
27
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
28
use Symfony\Component\Config\FileLocator;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Output\OutputInterface;
31
use Symfony\Component\DependencyInjection\ContainerBuilder;
32
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
33
use Symfony\Component\DependencyInjection\Reference;
34
use function sys_get_temp_dir;
35
36
class Extension implements ExtensionInterface
37
{
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function initialize(ExtensionManager $extensionManager): void
42
    {
43 1
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 5
    public function load(ContainerBuilder $container, array $config): void
49
    {
50 5
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/Resources/config'));
51
52 5
        $servicesFile = 'services.xml';
53 5
        $loader->load($servicesFile);
54
55 5
        $container->setParameter('behat.code_coverage.config.filter', $config['filter']);
56 5
        $container->setParameter('behat.code_coverage.config.branchAndPathCoverage', $config['branchAndPathCoverage']);
57 5
        $container->setParameter('behat.code_coverage.config.reports', $config['reports'] ?? []);
58 5
        $container->setParameter('behat.code_coverage.config.cache', $config['cache']);
59 1
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 10
    public function configure(ArrayNodeDefinition $builder): void
65
    {
66
        $builder
67 10
            ->children()
68 10
                ->scalarNode('cache')
69 10
                    ->defaultValue(sys_get_temp_dir() . '/behat-code-coverage-cache')
70 10
                ->end()
71 10
                ->booleanNode('branchAndPathCoverage')
0 ignored issues
show
Bug introduced by
The method booleanNode() 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

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