Passed
Pull Request — master (#24)
by Kacper
12:16
created

Extension::initCodeCoverage()   F

Complexity

Conditions 14
Paths 1728

Size

Total Lines 62
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 14.8225

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 14
eloc 33
c 4
b 0
f 0
nc 1728
nop 5
dl 0
loc 62
ccs 26
cts 31
cp 0.8387
crap 14.8225
rs 2.1

How to fix   Long Method    Complexity   

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\Controller;
10
use Behat\Testwork\Cli\ServiceContainer\CliExtension;
11
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
12
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
13
use Behat\Testwork\ServiceContainer\ExtensionManager;
14
use Composer\InstalledVersions;
15
use Composer\Semver\VersionParser;
16
use DVDoug\Behat\CodeCoverage\Subscriber\EventSubscriber;
17
use SebastianBergmann\CodeCoverage\CodeCoverage;
18
use SebastianBergmann\CodeCoverage\Driver\Selector;
19
use SebastianBergmann\CodeCoverage\Driver\Xdebug2NotEnabledException;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCo...bug2NotEnabledException 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...
20
use SebastianBergmann\CodeCoverage\Driver\Xdebug3NotEnabledException;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCo...bug3NotEnabledException 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...
21
use SebastianBergmann\CodeCoverage\Driver\XdebugNotAvailableException;
22
use SebastianBergmann\CodeCoverage\Driver\XdebugNotEnabledException;
23
use SebastianBergmann\CodeCoverage\Filter;
24
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverAvailableException;
25
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverWithPathCoverageSupportAvailableException;
26
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
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\PhpFileLoader;
33
use Symfony\Component\DependencyInjection\Reference;
34
35
use function sprintf;
36
use function sys_get_temp_dir;
37
38
class Extension implements ExtensionInterface
39
{
40 36
    public function initialize(ExtensionManager $extensionManager): void
41
    {
42 36
    }
43
44 36
    public function load(ContainerBuilder $container, array $config): void
45
    {
46 36
        $container->registerForAutoconfiguration(Controller::class)->addTag(CliExtension::CONTROLLER_TAG);
47 36
        $container->registerForAutoconfiguration(EventSubscriber::class)->addTag(EventDispatcherExtension::SUBSCRIBER_TAG);
48
49 36
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../config'));
50 36
        $loader->load('services.php');
51
52 36
        $container->setParameter('behat.code_coverage.config.filter', $config['filter']);
53 36
        $container->setParameter('behat.code_coverage.config.branchAndPathCoverage', $config['branchAndPathCoverage']);
54 36
        $container->setParameter('behat.code_coverage.config.reports', $config['reports'] ?? []);
55 36
        $container->setParameter('behat.code_coverage.config.cache', $config['cache']);
56
    }
57
58 72
    public function configure(ArrayNodeDefinition $builder): void
59
    {
60 72
        $builder
61 72
            ->children()
62 72
                ->scalarNode('cache')
63 72
                    ->defaultValue(sys_get_temp_dir() . '/behat-code-coverage-cache')
64 72
                ->end()
65 72
                ->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

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

269
                $codeCoverage->/** @scrutinizer ignore-call */ 
270
                               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...
270
            } else {
271
                $codeCoverage->doNotProcessUncoveredFiles();
0 ignored issues
show
Bug introduced by
The method doNotProcessUncoveredFiles() 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

271
                $codeCoverage->/** @scrutinizer ignore-call */ 
272
                               doNotProcessUncoveredFiles();

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
            }
273
        }
274
275 108
        return $codeCoverage;
276
    }
277
}
278