Passed
Pull Request — master (#58)
by Stéphane
10:08 queued 08:18
created

CodeCoverageExtension::load()   D

Complexity

Conditions 27
Paths 2

Size

Total Lines 145
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 246.5508

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 84
c 5
b 0
f 0
dl 0
loc 145
ccs 30
cts 91
cp 0.3297
rs 4.1666
cc 27
nc 2
nop 2
crap 246.5508

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
/**
4
 * This file is part of the friends-of-phpspec/phpspec-code-coverage package.
5
 *
6
 * @author ek9 <[email protected]>
7
 * @license MIT
8
 *
9
 * For the full copyright and license information, please see the LICENSE file
10
 * that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage;
16
17
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Exception\NoCoverageDriverAvailableException;
18
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener\CodeCoverageListener;
19
use PhpSpec\Console\ConsoleIO;
20
use PhpSpec\Extension;
21
use PhpSpec\ServiceContainer;
22
use RuntimeException;
23
use SebastianBergmann\CodeCoverage\CodeCoverage;
24
use SebastianBergmann\CodeCoverage\Driver\Selector;
25
use SebastianBergmann\CodeCoverage\Filter;
26
use SebastianBergmann\CodeCoverage\Report;
27
use SebastianBergmann\CodeCoverage\Report\Thresholds;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCoverage\Report\Thresholds 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...
28
use SebastianBergmann\CodeCoverage\Version;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Input\InputOption;
31
32
use function count;
33
use function is_array;
34
35
/**
36
 * Injects Code Coverage Event Subscriber into the EventDispatcher.
37
 * The Subscriber will add Code Coverage information before each example.
38
 *
39
 * @author Henrik Bjornskov
40
 */
41
class CodeCoverageExtension implements Extension
42
{
43
    /**
44
     * @param array<string, string> $params
45
     */
46 5
    public function load(ServiceContainer $container, array $params = []): void
47
    {
48 5
        foreach ($container->getByTag('console.commands') as $command) {
49
            $command->addOption('no-coverage', null, InputOption::VALUE_NONE, 'Skip code coverage generation');
50
        }
51
52 5
        $container->define('code_coverage.filter', static function () {
53
            return new Filter();
54 5
        });
55
56 5
        $container->define('code_coverage', static function (ServiceContainer $container) {
57
            /** @var InputInterface $input */
58
            $input = $container->get('console.input');
59
            $skipCoverage = $input->hasOption('no-coverage') && $input->getOption('no-coverage');
60
61
            /** @var Filter $filter */
62
            $filter = $container->get('code_coverage.filter');
63
64
            try {
65
                return new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
66
            } catch (RuntimeException $error) {
67
                if ($skipCoverage === true) {
68
                    return;
69
                }
70
71
                throw new NoCoverageDriverAvailableException(
72
                    'There is no available coverage driver to be used.',
73
                    0,
74
                    $error
75
                );
76
            }
77 5
        });
78
79 5
        $container->define('code_coverage.options', static function (ServiceContainer $container) use ($params) {
80 5
            $options = !empty($params) ? $params : $container->getParam('code_coverage');
81
82 5
            if (!isset($options['format'])) {
83 3
                $options['format'] = ['html'];
84 2
            } elseif (!is_array($options['format'])) {
85 2
                $options['format'] = (array) $options['format'];
86
            }
87
88 5
            if (isset($options['output'])) {
89 1
                if (!is_array($options['output']) && 1 === count($options['format'])) {
90 1
                    $format = $options['format'][0];
91 1
                    $options['output'] = [$format => $options['output']];
92
                }
93
            }
94
95 5
            if (!isset($options['show_uncovered_files'])) {
96 5
                $options['show_uncovered_files'] = true;
97
            }
98
99 5
            if (!isset($options['lower_upper_bound'])) {
100 5
                $options['lower_upper_bound'] = 35;
101
            }
102
103 5
            if (!isset($options['high_lower_bound'])) {
104 5
                $options['high_lower_bound'] = 70;
105
            }
106
107 5
            if (!isset($options['show_only_summary'])) {
108 4
                $options['show_only_summary'] = false;
109
            }
110
111 5
            return $options;
112 5
        });
113
114 5
        $container->define('code_coverage.reports', static function (ServiceContainer $container) {
115
            /** @var array<string, mixed> $options */
116
            $options = $container->get('code_coverage.options');
117
118
            $reports = [];
119
120
            foreach ($options['format'] as $format) {
121
                switch ($format) {
122
                    case 'clover':
123
                        $reports['clover'] = new Report\Clover();
124
125
                        break;
126
                    case 'php':
127
                        $reports['php'] = new Report\PHP();
128
129
                        break;
130
                    case 'text':
131
                        $reports['text'] = version_compare(Version::id(), '10.0.0', '>=') && class_exists(Thresholds::class)
132
                            ? new Report\Text(
133
                                Thresholds::from($options['lower_upper_bound'], $options['high_lower_bound']),
134
                                $options['show_uncovered_files'],
135
                                $options['show_only_summary']
136
                            )
137
                            : new Report\Text(
138
                                $options['lower_upper_bound'],
139
                                $options['high_lower_bound'],
140
                                $options['show_uncovered_files'],
141
                                $options['show_only_summary']
142
                            );
143
144
                        break;
145
                    case 'xml':
146
                        $reports['xml'] = new Report\Xml\Facade(Version::id());
147
148
                        break;
149
                    case 'crap4j':
150
                        $reports['crap4j'] = new Report\Crap4j();
151
152
                        break;
153
                    case 'html':
154
                        $reports['html'] = new Report\Html\Facade();
155
156
                        break;
157
                    case 'cobertura':
158
                        $reports['cobertura'] = new Report\Cobertura();
159
160
                        break;
161
                }
162
            }
163
164
            $container->setParam('code_coverage', $options);
165
166
            return $reports;
167 5
        });
168
169 5
        $container->define('event_dispatcher.listeners.code_coverage', static function (ServiceContainer $container) {
170
            /** @var InputInterface $input */
171
            $input = $container->get('console.input');
172
            $skipCoverage = $input->hasOption('no-coverage') && $input->getOption('no-coverage');
173
174
            /** @var ConsoleIO $consoleIO */
175
            $consoleIO = $container->get('console.io');
176
177
            /** @var ?CodeCoverage $codeCoverage */
178
            $codeCoverage = $container->get('code_coverage');
179
            if ($codeCoverage === null) {
180
                return;
181
            }
182
183
            /** @var array<string, object> $codeCoverageReports */
184
            $codeCoverageReports = $container->get('code_coverage.reports');
185
186
            $listener = new CodeCoverageListener($consoleIO, $codeCoverage, $codeCoverageReports, $skipCoverage);
187
            $listener->setOptions($container->getParam('code_coverage', []));
188
189
            return $listener;
190 5
        }, ['event_dispatcher.listeners']);
191
    }
192
}
193