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

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