Completed
Push — master ( dab6cf...e9b641 )
by Stéphane
13s queued 11s
created

CodeCoverageExtension::load()   D

Complexity

Conditions 21
Paths 2

Size

Total Lines 118
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 154.4696

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 21
eloc 71
c 4
b 2
f 0
nc 2
nop 2
dl 0
loc 118
ccs 23
cts 70
cp 0.3286
crap 154.4696
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
declare(strict_types=1);
4
5
/**
6
 * This file is part of the friends-of-phpspec/phpspec-code-coverage package.
7
 *
8
 * @author ek9 <[email protected]>
9
 * @license MIT
10
 *
11
 * For the full copyright and license information, please see the LICENSE file
12
 * that was distributed with this source code.
13
 */
14
15
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage;
16
17
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Exception\NoCoverageDriverAvailableException;
18
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener\CodeCoverageListener;
19
use PhpSpec\Extension;
20
use PhpSpec\ServiceContainer;
21
use SebastianBergmann\CodeCoverage\CodeCoverage;
22
use SebastianBergmann\CodeCoverage\Filter;
23
use SebastianBergmann\CodeCoverage\Report;
24
use SebastianBergmann\CodeCoverage\Version;
25
use Symfony\Component\Console\Input\InputOption;
26
27
/**
28
 * Injects Code Coverage Event Subscriber into the EventDispatcher.
29
 * The Subscriber will add Code Coverage information before each example.
30
 *
31
 * @author Henrik Bjornskov
32
 */
33
class CodeCoverageExtension implements Extension
34
{
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function load(ServiceContainer $container, array $params = []): void
39
    {
40 3
        foreach ($container->getByTag('console.commands') as $command) {
41
            $command->addOption('no-coverage', null, InputOption::VALUE_NONE, 'Skip code coverage generation');
42
        }
43
44
        $container->define('code_coverage.filter', static function () {
45
            return new Filter();
46 3
        });
47
48
        $container->define('code_coverage', static function ($container) {
49
            try {
50
                $coverage = new CodeCoverage(null, $container->get('code_coverage.filter'));
51
            } catch (\RuntimeException $error) {
52
                throw new NoCoverageDriverAvailableException(
53
                    'There is no available coverage driver to be used.',
54
                    0,
55
                    $error
56
                );
57
            }
58
59
            return $coverage;
60 3
        });
61
62
        $container->define('code_coverage.options', static function ($container) use ($params) {
63 3
            $options = !empty($params) ? $params : $container->getParam('code_coverage');
64
65 3
            if (!isset($options['format'])) {
66 1
                $options['format'] = ['html'];
67 2
            } elseif (!\is_array($options['format'])) {
68 2
                $options['format'] = (array) $options['format'];
69
            }
70
71 3
            if (isset($options['output'])) {
72 1
                if (!\is_array($options['output']) && 1 === \count($options['format'])) {
73 1
                    $format = $options['format'][0];
74 1
                    $options['output'] = [$format => $options['output']];
75
                }
76
            }
77
78 3
            if (!isset($options['show_uncovered_files'])) {
79 3
                $options['show_uncovered_files'] = true;
80
            }
81
82 3
            if (!isset($options['lower_upper_bound'])) {
83 3
                $options['lower_upper_bound'] = 35;
84
            }
85
86 3
            if (!isset($options['high_lower_bound'])) {
87 3
                $options['high_lower_bound'] = 70;
88
            }
89
90 3
            return $options;
91 3
        });
92
93
        $container->define('code_coverage.reports', static function ($container) {
94
            $options = $container->get('code_coverage.options');
95
96
            $reports = [];
97
98
            foreach ($options['format'] as $format) {
99
                switch ($format) {
100
                    case 'clover':
101
                        $reports['clover'] = new Report\Clover();
102
103
                        break;
104
                    case 'php':
105
                        $reports['php'] = new Report\PHP();
106
107
                        break;
108
                    case 'text':
109
                        $reports['text'] = new Report\Text(
110
                            $options['lower_upper_bound'],
111
                            $options['high_lower_bound'],
112
                            $options['show_uncovered_files'],
113
                            /* $showOnlySummary */
114
                            false
115
                        );
116
117
                        break;
118
                    case 'xml':
119
                        $reports['xml'] = new Report\Xml\Facade(Version::id());
120
121
                        break;
122
                    case 'crap4j':
123
                        $reports['crap4j'] = new Report\Crap4j();
124
125
                        break;
126
                    case 'html':
127
                        $reports['html'] = new Report\Html\Facade();
128
129
                        break;
130
                }
131
            }
132
133
            $container->setParam('code_coverage', $options);
134
135
            return $reports;
136 3
        });
137
138
        $container->define('event_dispatcher.listeners.code_coverage', static function ($container) {
139
            $skipCoverage = false;
140
            $input = $container->get('console.input');
141
142
            if ($input->hasOption('no-coverage') && $input->getOption('no-coverage')) {
143
                $skipCoverage = true;
144
            }
145
146
            $listener = new CodeCoverageListener(
147
                $container->get('console.io'),
148
                $container->get('code_coverage'),
149
                $container->get('code_coverage.reports'),
150
                $skipCoverage
151
            );
152
            $listener->setOptions($container->getParam('code_coverage', []));
153
154
            return $listener;
155 3
        }, ['event_dispatcher.listeners']);
156 3
    }
157
}
158