Completed
Push — master ( b6b833...ae163c )
by Stéphane
15s queued 12s
created

CodeCoverageExtension   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 37.84%

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 123
ccs 28
cts 74
cp 0.3784
rs 10
c 0
b 0
f 0
wmc 21

1 Method

Rating   Name   Duplication   Size   Complexity  
D load() 0 118 21
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\Extension;
20
use PhpSpec\ServiceContainer;
21
use RuntimeException;
22
use SebastianBergmann\CodeCoverage\CodeCoverage;
23
use SebastianBergmann\CodeCoverage\Filter;
24
use SebastianBergmann\CodeCoverage\Report;
25
use SebastianBergmann\CodeCoverage\Version;
26
use Symfony\Component\Console\Input\InputOption;
27
28
use function count;
29
use function is_array;
30
31
/**
32
 * Injects Code Coverage Event Subscriber into the EventDispatcher.
33
 * The Subscriber will add Code Coverage information before each example.
34
 *
35
 * @author Henrik Bjornskov
36
 */
37
class CodeCoverageExtension implements Extension
38
{
39
    /**
40
     * {@inheritdoc}
41
     */
42 3
    public function load(ServiceContainer $container, array $params = []): void
43
    {
44 3
        foreach ($container->getByTag('console.commands') as $command) {
45
            $command->addOption('no-coverage', null, InputOption::VALUE_NONE, 'Skip code coverage generation');
46
        }
47
48 3
        $container->define('code_coverage.filter', static function () {
49
            return new Filter();
50 3
        });
51
52 3
        $container->define('code_coverage', static function ($container) {
53
            try {
54
                $coverage = new CodeCoverage(null, $container->get('code_coverage.filter'));
55
            } catch (RuntimeException $error) {
56
                throw new NoCoverageDriverAvailableException(
57
                    'There is no available coverage driver to be used.',
58
                    0,
59
                    $error
60
                );
61
            }
62
63
            return $coverage;
64 3
        });
65
66 3
        $container->define('code_coverage.options', static function ($container) use ($params) {
67 3
            $options = !empty($params) ? $params : $container->getParam('code_coverage');
68
69 3
            if (!isset($options['format'])) {
70 1
                $options['format'] = ['html'];
71 2
            } elseif (!is_array($options['format'])) {
72 2
                $options['format'] = (array) $options['format'];
73
            }
74
75 3
            if (isset($options['output'])) {
76 1
                if (!is_array($options['output']) && 1 === count($options['format'])) {
77 1
                    $format = $options['format'][0];
78 1
                    $options['output'] = [$format => $options['output']];
79
                }
80
            }
81
82 3
            if (!isset($options['show_uncovered_files'])) {
83 3
                $options['show_uncovered_files'] = true;
84
            }
85
86 3
            if (!isset($options['lower_upper_bound'])) {
87 3
                $options['lower_upper_bound'] = 35;
88
            }
89
90 3
            if (!isset($options['high_lower_bound'])) {
91 3
                $options['high_lower_bound'] = 70;
92
            }
93
94 3
            return $options;
95 3
        });
96
97 3
        $container->define('code_coverage.reports', static function ($container) {
98
            $options = $container->get('code_coverage.options');
99
100
            $reports = [];
101
102
            foreach ($options['format'] as $format) {
103
                switch ($format) {
104
                    case 'clover':
105
                        $reports['clover'] = new Report\Clover();
106
107
                        break;
108
                    case 'php':
109
                        $reports['php'] = new Report\PHP();
110
111
                        break;
112
                    case 'text':
113
                        $reports['text'] = new Report\Text(
114
                            $options['lower_upper_bound'],
115
                            $options['high_lower_bound'],
116
                            $options['show_uncovered_files'],
117
                            /* $showOnlySummary */
118
                            false
119
                        );
120
121
                        break;
122
                    case 'xml':
123
                        $reports['xml'] = new Report\Xml\Facade(Version::id());
124
125
                        break;
126
                    case 'crap4j':
127
                        $reports['crap4j'] = new Report\Crap4j();
128
129
                        break;
130
                    case 'html':
131
                        $reports['html'] = new Report\Html\Facade();
132
133
                        break;
134
                }
135
            }
136
137
            $container->setParam('code_coverage', $options);
138
139
            return $reports;
140 3
        });
141
142 3
        $container->define('event_dispatcher.listeners.code_coverage', static function ($container) {
143
            $skipCoverage = false;
144
            $input = $container->get('console.input');
145
146
            if ($input->hasOption('no-coverage') && $input->getOption('no-coverage')) {
147
                $skipCoverage = true;
148
            }
149
150
            $listener = new CodeCoverageListener(
151
                $container->get('console.io'),
152
                $container->get('code_coverage'),
153
                $container->get('code_coverage.reports'),
154
                $skipCoverage
155
            );
156
            $listener->setOptions($container->getParam('code_coverage', []));
157
158
            return $listener;
159 3
        }, ['event_dispatcher.listeners']);
160 3
    }
161
}
162