Passed
Pull Request — master (#15)
by Pierre
01:45
created

CodeCoverageExtension   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 31.43%

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 121
ccs 22
cts 70
cp 0.3143
rs 10
c 0
b 0
f 0
wmc 21

1 Method

Rating   Name   Duplication   Size   Complexity  
D load() 0 116 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
        $container->define('code_coverage.filter', static function () {
49
            return new Filter();
50 3
        });
51
52
        $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
        $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']) && !is_array($options['output']) && 1 === count($options['format'])) {
76 1
                $format = $options['format'][0];
77 1
                $options['output'] = [$format => $options['output']];
78
            }
79
80 3
            if (!isset($options['show_uncovered_files'])) {
81 3
                $options['show_uncovered_files'] = true;
82
            }
83
84 3
            if (!isset($options['lower_upper_bound'])) {
85 3
                $options['lower_upper_bound'] = 35;
86
            }
87
88 3
            if (!isset($options['high_lower_bound'])) {
89 3
                $options['high_lower_bound'] = 70;
90
            }
91
92 3
            return $options;
93 3
        });
94
95
        $container->define('code_coverage.reports', static function ($container) {
96
            $options = $container->get('code_coverage.options');
97
98
            $reports = [];
99
100
            foreach ($options['format'] as $format) {
101
                switch ($format) {
102
                    case 'clover':
103
                        $reports['clover'] = new Report\Clover();
104
105
                        break;
106
                    case 'php':
107
                        $reports['php'] = new Report\PHP();
108
109
                        break;
110
                    case 'text':
111
                        $reports['text'] = new Report\Text(
112
                            $options['lower_upper_bound'],
113
                            $options['high_lower_bound'],
114
                            $options['show_uncovered_files'],
115
                            /* $showOnlySummary */
116
                            false
117
                        );
118
119
                        break;
120
                    case 'xml':
121
                        $reports['xml'] = new Report\Xml\Facade(Version::id());
122
123
                        break;
124
                    case 'crap4j':
125
                        $reports['crap4j'] = new Report\Crap4j();
126
127
                        break;
128
                    case 'html':
129
                        $reports['html'] = new Report\Html\Facade();
130
131
                        break;
132
                }
133
            }
134
135
            $container->setParam('code_coverage', $options);
136
137
            return $reports;
138 3
        });
139
140
        $container->define('event_dispatcher.listeners.code_coverage', static function ($container) {
141
            $skipCoverage = false;
142
            $input = $container->get('console.input');
143
144
            if ($input->hasOption('no-coverage') && $input->getOption('no-coverage')) {
145
                $skipCoverage = true;
146
            }
147
148
            $listener = new CodeCoverageListener(
149
                $container->get('console.io'),
150
                $container->get('code_coverage'),
151
                $container->get('code_coverage.reports'),
152
                $skipCoverage
153
            );
154
            $listener->setOptions($container->getParam('code_coverage', []));
155
156
            return $listener;
157 3
        }, ['event_dispatcher.listeners']);
158 3
    }
159
}
160