Passed
Push — master ( b8ff0c...321aea )
by Doug
02:22
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 5
    public function load(ContainerBuilder $container, array $config): void
50
    {
51 5
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/Resources/config'));
52
53 5
        $servicesFile = 'services.xml';
54 5
        $loader->load($servicesFile);
55
56 5
        $container->setParameter('behat.code_coverage.config.filter', $config['filter']);
57 5
        $container->setParameter('behat.code_coverage.config.branchAndPathCoverage', $config['branchAndPathCoverage']);
58 5
        $container->setParameter('behat.code_coverage.config.reports', $config['reports'] ?? []);
59 5
        $container->setParameter('behat.code_coverage.config.cache', $config['cache']);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 10
    public function configure(ArrayNodeDefinition $builder): void
66
    {
67 6
        $builder
68 10
            ->children()
69 10
                ->scalarNode('cache')
70 10
                    ->defaultValue(sys_get_temp_dir() . '/behat-code-coverage-cache')
71 10
                ->end()
72 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

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