Passed
Pull Request — master (#64)
by Csaba
03:03
created

CodeCoverageExtension::load()   D

Complexity

Conditions 24
Paths 2

Size

Total Lines 136
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 200.2823

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 24
eloc 84
c 5
b 0
f 0
nc 2
nop 2
dl 0
loc 136
ccs 30
cts 92
cp 0.3261
crap 200.2823
rs 4.1666

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 Filter $filter */
58
            $filter = $container->get('code_coverage.filter');
59
60
            try {
61
                return new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
62
            } catch (RuntimeException $error) {
63
                throw new NoCoverageDriverAvailableException(
64
                    'There is no available coverage driver to be used.',
65
                    0,
66
                    $error
67
                );
68
            }
69 5
        });
70
71 5
        $container->define('code_coverage.options', static function (ServiceContainer $container) use ($params) {
72 5
            $options = !empty($params) ? $params : $container->getParam('code_coverage');
73
74 5
            if (!isset($options['format'])) {
75 3
                $options['format'] = ['html'];
76 2
            } elseif (!is_array($options['format'])) {
77 2
                $options['format'] = (array) $options['format'];
78
            }
79
80 5
            if (isset($options['output'])) {
81 1
                if (!is_array($options['output']) && 1 === count($options['format'])) {
82 1
                    $format = $options['format'][0];
83 1
                    $options['output'] = [$format => $options['output']];
84
                }
85
            }
86
87 5
            if (!isset($options['show_uncovered_files'])) {
88 5
                $options['show_uncovered_files'] = true;
89
            }
90
91 5
            if (!isset($options['lower_upper_bound'])) {
92 5
                $options['lower_upper_bound'] = 35;
93
            }
94
95 5
            if (!isset($options['high_lower_bound'])) {
96 5
                $options['high_lower_bound'] = 70;
97
            }
98
99 5
            if (!isset($options['show_only_summary'])) {
100 4
                $options['show_only_summary'] = false;
101
            }
102
103 5
            return new CodeCoverageOptions($options);
104 5
        });
105
106 5
        $container->define('code_coverage.reports', static function (ServiceContainer $container) {
107
            /** @var CodeCoverageOptions $optionsWrapper */
108
            $optionsWrapper = $container->get('code_coverage.options');
109
            $options = $optionsWrapper->getOptions();
110
111
            $reports = [];
112
113
            foreach ($optionsWrapper->getFormats() as $format) {
114
                switch ($format) {
115
                    case 'clover':
116
                        $reports['clover'] = new Report\Clover();
117
                        break;
118
                    case 'php':
119
                        $reports['php'] = new Report\PHP();
120
                        break;
121
                    case 'text':
122
                        $reports['text'] = version_compare(Version::id(), '10.0.0', '>=') && class_exists(Thresholds::class)
123
                            ? new Report\Text(
124
                                Thresholds::from($optionsWrapper->getLowerUpperBound(), $optionsWrapper->getHighLowerBound()),
125
                                $optionsWrapper->showUncoveredFiles(), // @phpstan-ignore-line Version 10.0.0+ uses Thresholds
0 ignored issues
show
Bug introduced by
$optionsWrapper->showUncoveredFiles() of type boolean is incompatible with the type integer expected by parameter $highLowerBound of SebastianBergmann\CodeCo...ort\Text::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
                                /** @scrutinizer ignore-type */ $optionsWrapper->showUncoveredFiles(), // @phpstan-ignore-line Version 10.0.0+ uses Thresholds
Loading history...
126
                                $optionsWrapper->showOnlySummary()
127
                            )
128
                            : new Report\Text(
129
                                $optionsWrapper->getLowerUpperBound(),
130
                                $optionsWrapper->getHighLowerBound(),
131
                                $optionsWrapper->showUncoveredFiles(),
132
                                $optionsWrapper->showOnlySummary()
133
                            );
134
                        break;
135
                    case 'xml':
136
                        $reports['xml'] = new Report\Xml\Facade(Version::id());
137
                        break;
138
                    case 'crap4j':
139
                        $reports['crap4j'] = new Report\Crap4j();
140
                        break;
141
                    case 'html':
142
                        $reports['html'] = new Report\Html\Facade();
143
                        break;
144
                    case 'cobertura':
145
                        $reports['cobertura'] = new Report\Cobertura();
146
                        break;
147
                }
148
            }
149
150
            $container->setParam('code_coverage', $options);
151
152
            return new CodeCoverageReports($reports);
153 5
        });
154
155 5
        $container->define('event_dispatcher.listeners.code_coverage', static function (ServiceContainer $container) {
156
            /** @var InputInterface $input */
157
            $input = $container->get('console.input');
158
            $skipCoverage = $input->hasOption('no-coverage') && $input->getOption('no-coverage');
159
160
            /** @var ConsoleIO $consoleIO */
161
            $consoleIO = $container->get('console.io');
162
163
            /** @var CodeCoverage $codeCoverage */
164
            $codeCoverage = $container->get('code_coverage');
165
166
            /** @var CodeCoverageReports $codeCoverageReportsWrapper */
167
            $codeCoverageReportsWrapper = $container->get('code_coverage.reports');
168
169
            /** @var CodeCoverageOptions $optionsWrapper */
170
            $optionsWrapper = $container->get('code_coverage.options');
171
172
            $listener = new CodeCoverageListener(
173
                $consoleIO,
174
                $codeCoverage,
175
                $codeCoverageReportsWrapper->getReports(),
176
                $skipCoverage
177
            );
178
            $listener->setOptions($optionsWrapper->getOptions());
179
180
            return $listener;
181 5
        }, ['event_dispatcher.listeners']);
182
    }
183
}
184