Passed
Pull Request — master (#20)
by Stéphane
14:19
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 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
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
    private $coverage;
29
30
    private $io;
31
32
    private $options;
33
34
    private $reports;
35
36
    private $skipCoverage;
37
38
    public function __construct(ConsoleIO $io, CodeCoverage $coverage, array $reports, bool $skipCoverage = false)
39
    {
40
        $this->io = $io;
41
        $this->coverage = $coverage;
42
        $this->reports = $reports;
43
        $this->options = [
44
            'whitelist' => ['src', 'lib'],
45
            'blacklist' => ['test', 'vendor', 'spec'],
46
            'whitelist_files' => [],
47
            'blacklist_files' => [],
48
            'output' => ['html' => 'coverage'],
49
            'format' => ['html'],
50
        ];
51
52
        $this->skipCoverage = $skipCoverage;
53
    }
54
55
    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

55
    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...
56 5
    {
57
        if ($this->skipCoverage) {
58 5
            if ($this->io->isVerbose()) {
59
                $this->io->writeln('Skipping code coverage generation');
60
            }
61
62 5
            return;
63
        }
64
65
        if ($this->io->isVerbose()) {
66
            $this->io->writeln();
67
        }
68
69
        foreach ($this->reports as $format => $report) {
70
            if ($this->io->isVerbose()) {
71
                $this->io->writeln(sprintf('Generating code coverage report in %s format ...', $format));
72
            }
73
74
            if ($report instanceof Report\Text) {
75
                $this->io->writeln(
76
                    $report->process($this->coverage, $this->io->isDecorated())
77
                );
78
            } else {
79
                $report->process($this->coverage, $this->options['output'][$format]);
80
            }
81
        }
82
    }
83
84
    /**
85
     * Note: We use array_map() instead of array_walk() because the latter expects
86
     * the callback to take the value as the first and the index as the seconds parameter.
87
     *
88
     * @param SuiteEvent $event
89
     */
90
    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

90
    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...
91
    {
92
        if ($this->skipCoverage) {
93
            return;
94 5
        }
95
96
        $filter = $this->coverage->filter();
97
98
        foreach ($this->options['whitelist'] as $option) {
99
            $filter->addDirectoryToWhitelist($option);
100
        }
101
102
        foreach ($this->options['blacklist'] as $option) {
103
            $filter->removeDirectoryFromWhitelist($option);
104
        }
105
106
        foreach ($this->options['whitelist_files'] as $option) {
107
            $filter->addFilesToWhitelist($option);
108
        }
109
110
        foreach ($this->options['blacklist_files'] as $option) {
111
            $filter->removeFileFromWhitelist($option);
112
        }
113
    }
114 5
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public static function getSubscribedEvents(): array
119
    {
120
        return [
121
            'beforeSuite' => ['beforeSuite', -10],
122
            'afterSuite' => ['afterSuite', -10],
123
        ];
124
    }
125
126
    public function setOptions(array $options): void
127
    {
128
        $this->options = $options + $this->options;
129
    }
130
}
131