AbstractCodeCoverageReporter::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Peridot\Reporter\CodeCoverage;
4
5
use Peridot\Core\TestInterface;
6
use Peridot\Reporter\AbstractBaseReporter;
7
use SebastianBergmann\CodeCoverage\CodeCoverage;
8
use SebastianBergmann\CodeCoverage\Filter;
9
10
/**
11
 * Class AbstractCodeCoverageReporter
12
 * @package Peridot\Reporter\CodeCoverage
13
 */
14
abstract class AbstractCodeCoverageReporter extends AbstractBaseReporter implements CodeCoverageReporter
15
{
16
    /**
17
     * @var CodeCoverage
18
     */
19
    protected $coverage;
20
21
    /**
22
     * @var mixed
23
     */
24
    protected $coverageReporter;
25
26
    /**
27
     * @var Filter
28
     */
29
    protected $filter;
30
31
    /**
32
     * @var string
33
     */
34
    protected $reportPath = 'coverage';
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function init()
40
    {
41
        $this->filter = new Filter();
42
        $this->coverage = new CodeCoverage(null, $this->filter);
43
44
        $this->eventEmitter->on('runner.start', [$this, 'onRunnerStart']);
45
        $this->eventEmitter->on('runner.end', [$this, 'onRunnerEnd']);
46
        $this->eventEmitter->on('test.start', [$this, 'onTestStart']);
47
        $this->eventEmitter->on('test.end', [$this, 'onTestEnd']);
48
        $this->eventEmitter->on('test.pending', [$this, 'onTestEnd']);
49
    }
50
51
    /**
52
     * Add a directory to the blacklist (recursively).
53
     *
54
     * @param string $directory
55
     * @param string $suffix
56
     * @param string $prefix
57
     * @return $this
58
     */
59
    public function addDirectoryToBlacklist($directory, $suffix = '.php', $prefix = '')
60
    {
61
        $this->filter->addDirectoryToBlacklist($directory, $suffix, $prefix);
0 ignored issues
show
Bug introduced by
The method addDirectoryToBlacklist() does not seem to exist on object<SebastianBergmann\CodeCoverage\Filter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
        return $this;
63
    }
64
65
    /**
66
     * Add a directory to the whitelist (recursively).
67
     *
68
     * @param string $directory
69
     * @param string $suffix
70
     * @param string $prefix
71
     * @return $this
72
     */
73
    public function addDirectoryToWhitelist($directory, $suffix = '.php', $prefix = '')
74
    {
75
        $this->filter->addDirectoryToWhitelist($directory, $suffix, $prefix);
76
        return $this;
77
    }
78
79
    /**
80
     * Add a file to the blacklist.
81
     *
82
     * @param string $filename
83
     * @return $this
84
     */
85
    public function addFileToBlacklist($filename)
86
    {
87
        $this->filter->addFileToBlacklist($filename);
0 ignored issues
show
Bug introduced by
The method addFileToBlacklist() does not seem to exist on object<SebastianBergmann\CodeCoverage\Filter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        return $this;
89
    }
90
91
    /**
92
     * Add a file to the whitelist.
93
     *
94
     * @param string $filename
95
     * @return $this
96
     */
97
    public function addFileToWhitelist($filename)
98
    {
99
        $this->filter->addFileToWhitelist($filename);
100
        return $this;
101
    }
102
103
    /**
104
     * Add files to the blacklist.
105
     *
106
     * @param array $files
107
     * @return $this
108
     */
109
    public function addFilesToBlacklist(array $files)
110
    {
111
        $this->filter->addFilesToBlacklist($files);
0 ignored issues
show
Bug introduced by
The method addFilesToBlacklist() does not seem to exist on object<SebastianBergmann\CodeCoverage\Filter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
        return $this;
113
    }
114
115
    /**
116
     * Add files to the whitelist.
117
     *
118
     * @param array $files
119
     * @return $this
120
     */
121
    public function addFilesToWhitelist(array $files)
122
    {
123
        $this->filter->addFilesToWhitelist($files);
124
        return $this;
125
    }
126
127
    /**
128
     * Return the code coverage reporter.
129
     *
130
     * @return mixed
131
     */
132
    public function getCoverageReporter()
133
    {
134
        if (!$this->coverageReporter) {
135
            $this->coverageReporter = $this->createCoverageReporter();
136
        }
137
138
        return $this->coverageReporter;
139
    }
140
141
    /**
142
     * Handle the runner.end event.
143
     */
144
    public function onRunnerEnd()
145
    {
146
        $this->output->write('Generating code coverage report... ');
147
148
        $this->getCoverageReporter()->process($this->coverage, $this->getReportPath());
149
        $this->eventEmitter->emit('code-coverage.end', [$this]);
150
151
        $this->output->writeln('Done!');
152
    }
153
154
    /**
155
     * Handle the runner.start event.
156
     */
157
    public function onRunnerStart()
158
    {
159
        $this->eventEmitter->emit('code-coverage.start', [$this]);
160
    }
161
162
    /**
163
     * Handle the test.start event.
164
     */
165
    public function onTestStart(TestInterface $test)
166
    {
167
        $this->coverage->start($test->getTitle());
168
    }
169
170
    /**
171
     * Handle the test.end event.
172
     */
173
    public function onTestEnd()
174
    {
175
        $this->coverage->stop();
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getReportPath()
182
    {
183
        return $this->reportPath;
184
    }
185
186
    /**
187
     * @param string $reportPath
188
     * @return $this
189
     */
190
    public function setReportPath($reportPath)
191
    {
192
        $this->reportPath = $reportPath;
193
        return $this;
194
    }
195
196
    /**
197
     * Create the desired code coverage reporter.
198
     *
199
     * @return mixed
200
     */
201
    abstract protected function createCoverageReporter();
202
}
203