Passed
Pull Request — master (#20)
by Stéphane
11:07
created

CodeCoverageListener::afterExample()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
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\Listener;
16
17
use PhpSpec\Console\ConsoleIO;
18
use PhpSpec\Event\SuiteEvent;
19
use SebastianBergmann\CodeCoverage\CodeCoverage;
20
use SebastianBergmann\CodeCoverage\Report;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
23
/**
24
 * @author Henrik Bjornskov
25
 */
26
class CodeCoverageListener implements EventSubscriberInterface
27
{
28
    /**
29
     * @var CodeCoverage
30
     */
31
    private $coverage;
32
33
    /**
34
     * @var ConsoleIO
35
     */
36
    private $io;
37
38
    /**
39
     * @var array
40
     */
41
    private $options;
42
43
    /**
44
     * @var array
45
     */
46
    private $reports;
47
48
    /**
49
     * @var bool
50
     */
51
    private $skipCoverage;
52
53
    public function __construct(ConsoleIO $io, CodeCoverage $coverage, array $reports, bool $skipCoverage = false)
54 1
    {
55
        $this->io = $io;
56 1
        $this->coverage = $coverage;
57 1
        $this->reports = $reports;
58 1
        $this->options = [
59 1
            'whitelist' => ['src', 'lib'],
60
            'blacklist' => ['test', 'vendor', 'spec'],
61
            'whitelist_files' => [],
62
            'blacklist_files' => [],
63
            'output' => ['html' => 'coverage'],
64
            'format' => ['html'],
65
        ];
66
67
        $this->skipCoverage = $skipCoverage;
68 1
    }
69 1
70
    public function afterSuite(SuiteEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
    public function afterSuite(/** @scrutinizer ignore-unused */ SuiteEvent $event): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71 6
    {
72
        if ($this->skipCoverage) {
73 6
            if ($this->io->isVerbose()) {
74
                $this->io->writeln('Skipping code coverage generation');
75
            }
76
77 6
            return;
78
        }
79
80
        if ($this->io->isVerbose()) {
81
            $this->io->writeln();
82
        }
83
84
        foreach ($this->reports as $format => $report) {
85
            if ($this->io->isVerbose()) {
86
                $this->io->writeln(sprintf('Generating code coverage report in %s format ...', $format));
87
            }
88
89
            if ($report instanceof Report\Text) {
90
                $this->io->writeln(
91
                    $report->process($this->coverage, $this->io->isDecorated())
92
                );
93
            } else {
94
                $report->process($this->coverage, $this->options['output'][$format]);
95
            }
96
        }
97
    }
98
99
    /**
100
     * Note: We use array_map() instead of array_walk() because the latter expects
101
     * the callback to take the value as the first and the index as the seconds parameter.
102
     *
103
     * @param SuiteEvent $event
104
     */
105
    public function beforeSuite(SuiteEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
    public function beforeSuite(/** @scrutinizer ignore-unused */ SuiteEvent $event): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        if ($this->skipCoverage) {
108
            return;
109 6
        }
110
111
        $filter = $this->coverage->filter();
112
113
        foreach ($this->options['whitelist'] as $option) {
114
            $filter->addDirectoryToWhitelist($option);
115
        }
116
117
        foreach ($this->options['blacklist'] as $option) {
118
            $filter->removeDirectoryFromWhitelist($option);
119
        }
120
121
        foreach ($this->options['whitelist_files'] as $option) {
122
            $filter->addFilesToWhitelist($option);
123
        }
124
125
        foreach ($this->options['blacklist_files'] as $option) {
126
            $filter->removeFileFromWhitelist($option);
127
        }
128
    }
129 6
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public static function getSubscribedEvents(): array
134
    {
135
        return [
136
            'beforeSuite' => ['beforeSuite', -10],
137
            'afterSuite' => ['afterSuite', -10],
138
        ];
139
    }
140
141
    public function setOptions(array $options): void
142
    {
143
        $this->options = $options + $this->options;
144
    }
145
}
146