CodeCoverageExtension   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 20

1 Method

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