Passed
Pull Request — master (#10)
by Pol
02:23
created

CodeCoverageExtension   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 32.39%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 72
c 4
b 2
f 0
dl 0
loc 113
ccs 23
cts 71
cp 0.3239
rs 10
wmc 21

1 Method

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