1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CloverReporter; |
4
|
|
|
|
5
|
|
|
use CloverReporter\Console\Style; |
6
|
|
|
|
7
|
|
|
class Render |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $infoList; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $options; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Style |
21
|
|
|
*/ |
22
|
|
|
protected $style; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Render constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $options |
28
|
|
|
* @param array $infoList |
29
|
|
|
* @param Style $style |
30
|
|
|
*/ |
31
|
6 |
|
public function __construct(array $options, array $infoList, Style $style) |
32
|
|
|
{ |
33
|
6 |
|
unset($infoList['files']['package']); |
34
|
|
|
|
35
|
6 |
|
$this->options = $options; |
36
|
6 |
|
$this->infoList = $infoList; |
37
|
6 |
|
$this->style = $style; |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws \InvalidArgumentException |
42
|
|
|
*/ |
43
|
2 |
|
public function displayCoverage() |
44
|
|
|
{ |
45
|
2 |
|
$this->allFiles(); |
46
|
|
|
|
47
|
2 |
|
foreach ($this->infoList['files'] as $fileData) { |
48
|
2 |
|
$this->style->formatCoverage( |
49
|
2 |
|
$fileData['percent'], |
50
|
2 |
|
$fileData['package'] . '\\' . $fileData['namespace'] |
51
|
2 |
|
); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
2 |
|
$this->style->newLine(); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
2 |
|
public function shortReport() |
62
|
|
|
{ |
63
|
|
|
$this->allFiles()->fileProcessor(function (array $fileData, array $lines) { |
64
|
1 |
|
$newLine = false; |
65
|
|
|
|
66
|
1 |
|
foreach ($lines as $number => $line) { |
67
|
1 |
|
if (isset($fileData['info'][$number + 1])) { |
68
|
1 |
|
$lineCoverage = $fileData['info'][$number + 1]; |
69
|
|
|
|
70
|
1 |
|
if ($lineCoverage !== '0') { |
71
|
1 |
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
$newLine = true; |
75
|
1 |
|
$this->style->formatUncoveredLine($number +1, $line); |
76
|
1 |
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
if ($newLine) { |
80
|
1 |
|
$this->style->newLine(); |
81
|
1 |
|
} |
82
|
2 |
|
}); |
83
|
|
|
|
84
|
2 |
|
$this->style->newLine(); |
85
|
|
|
|
86
|
2 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @throws \InvalidArgumentException |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
4 |
|
public function allFiles() |
94
|
|
|
{ |
95
|
4 |
|
$files = count($this->infoList['files']); |
96
|
4 |
|
$this->style->writeln("Found <info>$files</info> source files:"); |
97
|
|
|
|
98
|
4 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
* @throws \InvalidArgumentException |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function fullReport() |
107
|
|
|
{ |
108
|
1 |
|
$this->fileProcessor(function (array $fileData, array $lines) { |
109
|
1 |
|
foreach ($lines as $number => $line) { |
110
|
1 |
|
$lineCoverage = '-'; |
111
|
|
|
|
112
|
1 |
|
if (isset($fileData['info'][$number + 1])) { |
113
|
1 |
|
$lineCoverage = $fileData['info'][$number + 1]; |
114
|
1 |
|
} |
115
|
|
|
|
116
|
1 |
|
if ($lineCoverage === '0') { |
117
|
1 |
|
$this->style->formatUncoveredLine($number +1, $line, 0); |
118
|
1 |
|
} else { |
119
|
1 |
|
$this->style->formatCoveredLine($number, $lineCoverage, $line); |
120
|
|
|
} |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
$this->style->newLine(); |
124
|
1 |
|
}); |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// public function htmlReport() |
|
|
|
|
130
|
|
|
// { |
131
|
|
|
// |
132
|
|
|
// } |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param \Closure $lineProcessor |
136
|
|
|
* @throws \InvalidArgumentException |
137
|
|
|
*/ |
138
|
3 |
|
protected function fileProcessor(\Closure $lineProcessor) |
139
|
|
|
{ |
140
|
3 |
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem; |
141
|
|
|
|
142
|
3 |
|
foreach ($this->infoList['files'] as $fileData) { |
143
|
3 |
|
$this->style->formatCoverage( |
144
|
3 |
|
$fileData['percent'], |
145
|
3 |
|
$fileData['package'] . '\\' . $fileData['namespace'] |
146
|
3 |
|
); |
147
|
|
|
|
148
|
3 |
|
$path = $fileData['path']; |
149
|
|
|
|
150
|
3 |
|
if (!$filesystem->exists($path)) { |
151
|
1 |
|
$this->style->errorMessage("File don't exists: <comment>$path</comment>"); |
152
|
1 |
|
continue; |
153
|
|
|
} |
154
|
|
|
|
155
|
2 |
|
$content = file_get_contents($path); |
156
|
|
|
|
157
|
2 |
|
$lines = explode("\n", $content); |
158
|
|
|
|
159
|
2 |
|
$lineProcessor($fileData, $lines); |
160
|
3 |
|
} |
161
|
3 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param int $startTime |
165
|
|
|
* @throws \InvalidArgumentException |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
6 |
|
public function summary($startTime) |
169
|
|
|
{ |
170
|
|
|
//@todo count warnings, errors & ok |
171
|
6 |
|
$sum = 0; |
172
|
6 |
|
$count = 0; |
173
|
6 |
|
$coverVal = 0; |
174
|
6 |
|
$beer = ''; |
175
|
|
|
|
176
|
6 |
|
foreach ($this->infoList['files'] as $fileData) { |
177
|
6 |
|
$sum += $fileData['percent']; |
178
|
6 |
|
$count++; |
179
|
6 |
|
} |
180
|
|
|
|
181
|
6 |
|
if ($count > 0) { |
182
|
6 |
|
$coverVal = round($sum / $count, 3); |
183
|
6 |
|
} |
184
|
|
|
|
185
|
6 |
|
$coverage = $this->style->formatCoveragePercent($coverVal); |
186
|
|
|
|
187
|
6 |
|
if ($coverage === '<info>100</info>') { |
188
|
1 |
|
$beer = "\xF0\x9F\x8D\xBA"; |
189
|
1 |
|
} |
190
|
|
|
|
191
|
6 |
|
$this->style->writeln("Total coverage: $coverage% $beer$beer$beer"); |
192
|
|
|
|
193
|
6 |
|
$endTime = microtime(true); |
194
|
6 |
|
$diff = round($endTime - $startTime, 5); |
195
|
6 |
|
$this->style->formatSection('Execution time', $diff . ' sec'); |
196
|
6 |
|
$this->style->formatSection( |
197
|
6 |
|
'Memory used', |
198
|
6 |
|
$this->bytes(memory_get_usage(true)) |
199
|
6 |
|
); |
200
|
|
|
|
201
|
6 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param int $bytes |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
6 |
|
public function bytes($bytes) |
209
|
|
|
{ |
210
|
6 |
|
$format = '%01.2f %s'; |
211
|
6 |
|
$units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB']; |
212
|
6 |
|
$mod = 1000; |
213
|
6 |
|
$power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0; |
214
|
|
|
|
215
|
6 |
|
return sprintf($format, $bytes / ($mod ** $power), $units[$power]); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.