88% of 1121OPs |
92% of 179Lines |
65% of 92Branches |
17% of 143Paths |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum\reports\asynchronous; |
4 |
|
5 |
use |
6 |
mageekguy\atoum, |
7 |
mageekguy\atoum\exceptions, |
8 |
mageekguy\atoum\score |
9 |
; |
10 |
|
11 |
class clover extends atoum\reports\asynchronous |
12 |
{ |
13 |
const defaultTitle = 'atoum code coverage'; |
14 |
const defaultPackage = 'atoumCodeCoverage'; |
15 |
const lineTypeMethod = 'method'; |
16 |
const lineTypeStatement = 'stmt'; |
17 |
const lineTypeConditional = 'cond'; |
18 |
|
19 |
protected $score = null; |
20 |
protected $loc = 0; |
21 |
protected $coveredLoc = 0; |
22 |
protected $methods = 0; |
23 |
protected $coveredMethods = 0; |
24 |
protected $branches = 0; |
25 |
protected $coveredBranches = 0; |
26 |
protected $paths = 0; |
27 |
protected $classes = 0; |
28 |
protected $package = ''; |
29 |
|
30 |
public function __construct(atoum\adapter $adapter = null)100% |
31 |
{ |
32 |
parent::__construct(); |
33 |
|
34 |
$this->setAdapter($adapter); |
35 |
|
36 |
if ($this->adapter->extension_loaded('libxml') === false) |
37 |
{ |
38 |
throw new exceptions\runtime('libxml PHP extension is mandatory for clover report'); |
39 |
} |
40 |
} |
41 |
|
42 |
public function getTitle()100% |
43 |
{ |
44 |
return ($this->title ?: self::defaultTitle); |
45 |
} |
46 |
|
47 |
public function getPackage()100% |
48 |
{ |
49 |
return ($this->package ?: self::defaultPackage); |
50 |
} |
51 |
|
52 |
public function setPackage($package)100% |
53 |
{ |
54 |
$this->package = (string) $package; |
55 |
|
56 |
return $this; |
57 |
} |
58 |
|
59 |
public function handleEvent($event, atoum\observable $observable)100% |
60 |
{ |
61 |
$this->score = ($event !== atoum\runner::runStop ? null : $observable->getScore()); |
62 |
|
63 |
return parent::handleEvent($event, $observable); |
64 |
} |
65 |
|
66 |
public function build($event)100% |
67 |
{ |
68 |
if ($event === atoum\runner::runStop) |
69 |
{ |
70 |
$document = new \DOMDocument('1.0', 'UTF-8'); |
71 |
|
72 |
$document->formatOutput = true; |
73 |
$document->appendChild($this->makeRootElement($document, $this->score->getCoverage())); |
74 |
|
75 |
$this->string = $document->saveXML(); |
76 |
} |
77 |
|
78 |
return $this; |
79 |
} |
80 |
|
81 |
protected function makeRootElement(\DOMDocument $document, score\coverage $coverage)100% |
82 |
{ |
83 |
$root = $document->createElement('coverage'); |
84 |
|
85 |
$root->setAttribute('generated', $this->getAdapter()->time()); |
86 |
$root->setAttribute('clover', $this->getAdapter()->uniqid()); |
87 |
|
88 |
$root->appendChild($this->makeProjectElement($document, $coverage)); |
89 |
|
90 |
return $root; |
91 |
} |
92 |
|
93 |
protected function makeProjectElement(\DOMDocument $document, score\coverage $coverage)100% |
94 |
{ |
95 |
$project = $document->createElement('project'); |
96 |
|
97 |
$project->setAttribute('timestamp', $this->getAdapter()->time()); |
98 |
$project->setAttribute('name', $this->getTitle()); |
99 |
|
100 |
$project->appendChild($this->makePackageElement($document, $coverage)); |
101 |
$project->appendChild($this->makeProjectMetricsElement($document, sizeof($coverage->getClasses()))); |
102 |
|
103 |
return $project; |
104 |
} |
105 |
|
106 |
protected function makeProjectMetricsElement(\DOMDocument $document, $files)100% |
107 |
{ |
108 |
$metrics = $this->makePackageMetricsElement($document, $files); |
109 |
|
110 |
$metrics->setAttribute('packages', 1); |
111 |
|
112 |
return $metrics; |
113 |
} |
114 |
|
115 |
protected function makePackageElement(\DOMDocument $document, score\coverage $coverage)100% |
116 |
{ |
117 |
$package = $document->createElement('package'); |
118 |
|
119 |
$package->setAttribute('name', $this->getPackage()); |
120 |
|
121 |
foreach ($coverage->getClasses() as $class => $file) |
122 |
{ |
123 |
$package->appendChild($this->makeFileElement($document, $file, $class, $coverage->getCoverageForClass($class), $coverage->getBranchesCoverageForClass($class), $coverage->getPathsCoverageForClass($class))); |
124 |
} |
125 |
|
126 |
$package->appendChild($this->makePackageMetricsElement($document, sizeof($coverage->getClasses()))); |
127 |
|
128 |
return $package; |
129 |
} |
130 |
|
131 |
protected function makePackageMetricsElement(\DOMDocument $document, $files)100% |
132 |
{ |
133 |
$metrics = $this->makeFileMetricsElement($document, $this->loc, $this->coveredLoc, $this->methods, $this->coveredMethods, $this->classes, $this->branches, $this->coveredBranches, $this->paths); |
134 |
|
135 |
$metrics->setAttribute('files', $files); |
136 | |
137 |
return $metrics; |
138 |
} |
139 |
|
140 |
protected function makeFileElement(\DOMDocument $document, $filename, $class, array $coverage, array $branches, array $paths)87% |
141 |
{ |
142 |
$file = $document->createElement('file'); |
143 |
|
144 |
$file->setAttribute('name', basename($filename)); |
145 |
$file->setAttribute('path', $filename); |
146 |
|
147 |
$methods = sizeof($coverage); |
148 |
$coveredMethods = 0; |
149 |
$totalLines = $coveredLines = 0; |
150 |
$totalBranches = $coveredBranches = 0; |
151 |
$totalPaths = 0; |
152 |
|
153 |
foreach ($coverage as $method => $lines) |
154 |
{ |
155 |
$totalMethodLines = $coveredMethodLines = 0; |
156 |
|
157 |
if (isset($branches[$method])) |
158 |
{ |
159 |
$totalBranches += sizeof($branches[$method]); |
160 |
$coveredBranches += sizeof(array_filter($branches[$method], function(array $branch) { return $branch['hit'] === 1; })); |
161 |
} |
162 |
|
163 |
if (isset($paths[$method])) |
164 |
{ |
165 |
$totalPaths += sizeof($paths[$method]); |
166 |
} |
167 |
|
168 |
foreach ($lines as $lineNumber => $cover) |
169 |
{ |
170 |
if ($cover >= -1) |
171 |
{ |
172 |
$totalMethodLines++; |
173 |
} |
174 |
|
175 |
if ($cover === 1) |
176 |
{ |
177 |
$coveredMethodLines++; |
178 |
$file->appendChild($this->makeLineElement($document, $lineNumber)); |
179 |
} |
180 |
else |
181 |
{ |
182 |
if ($cover !== -2) |
183 |
{ |
184 |
$file->appendChild($this->makeLineElement($document, $lineNumber, 0)); |
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
if ($coveredMethodLines === $totalMethodLines) |
190 |
{ |
191 |
++$coveredMethods; |
192 |
} |
193 |
|
194 |
$totalLines += $totalMethodLines; |
195 |
$coveredLines += $coveredMethodLines; |
196 |
} |
197 |
|
198 |
$this |
199 |
->addLoc($totalLines) |
200 |
->addCoveredLoc($coveredLines) |
201 |
->addClasses(1) |
202 |
->addMethod($methods) |
203 |
->addCoveredMethod($coveredMethods) |
204 |
->addBranches($totalBranches) |
205 |
->addCoveredBranches($coveredBranches) |
206 |
->addPaths($totalPaths) |
207 |
; |
208 |
|
209 |
$file->appendChild($this->makeClassElement($document, $class, $coverage, $branches, $paths)); |
210 |
$file->appendChild($this->makeFileMetricsElement($document, $totalLines, $coveredLines, $methods, $coveredMethods, 1, $totalBranches, $coveredBranches, $totalPaths)); |
211 |
|
212 |
return $file; |
213 |
} |
214 |
|
215 |
protected function makeFileMetricsElement(\DOMDocument $document, $loc, $cloc, $methods, $coveredMethods, $classes, $branches = 0, $coveredBranches = 0, $complexity = 0)100% |
216 |
{ |
217 |
$metrics = $this->makeClassMetricsElement($document, $loc, $cloc, $methods, $coveredMethods, $branches, $coveredBranches, $complexity); |
218 |
|
219 |
$metrics->setAttribute('classes', $classes); |
220 |
$metrics->setAttribute('loc', $loc); |
221 |
$metrics->setAttribute('ncloc', $loc); |
222 |
|
223 |
return $metrics; |
224 |
} |
225 |
|
226 |
protected function makeClassElement(\DOMDocument $document, $classname, array $coverage, array $branches, array $paths)79% |
227 |
{ |
228 |
$class = $document->createElement('class'); |
229 |
|
230 |
$class->setAttribute('name', basename(str_replace('\\', DIRECTORY_SEPARATOR, $classname))); |
231 |
|
232 |
$methods = sizeof($coverage); |
233 |
$coveredMethods = 0; |
234 |
$totalLines = $coveredLines = 0; |
235 |
$totalBranches = $coveredBranches = 0; |
236 |
$totalPaths = 0; |
237 |
|
238 |
foreach ($coverage as $method => $lines) |
239 |
{ |
240 |
if (isset($branches[$method])) |
241 |
{ |
242 |
$totalBranches += sizeof($branches[$method]); |
243 |
$coveredBranches += sizeof(array_filter($branches[$method], function(array $branch) { return $branch['hit'] === 1; })); |
244 |
} |
245 |
|
246 |
if (isset($paths[$method])) |
247 |
{ |
248 |
$totalPaths += sizeof($paths[$method]); |
249 |
} |
250 |
|
251 |
foreach ($lines as $cover) |
252 |
{ |
253 |
if ($cover >= -1) |
254 |
{ |
255 |
$totalLines++; |
256 |
} |
257 |
|
258 |
if ($cover === 1) |
259 |
{ |
260 |
$coveredLines++; |
261 |
} |
262 |
} |
263 |
|
264 |
if ($totalLines === $coveredLines) |
265 |
{ |
266 |
++$coveredMethods; |
267 |
} |
268 |
} |
269 |
|
270 |
$class->appendChild($this->makeClassMetricsElement($document, $totalLines, $coveredLines, $methods, $coveredMethods, $totalBranches, $coveredBranches, $totalPaths)); |
271 |
|
272 |
return $class; |
273 |
} |
274 |
|
275 |
protected function makeClassMetricsElement(\DOMDocument $document, $loc, $coveredLines, $methods, $coveredMethods, $branches = 0, $coveredBranches = 0, $complexity = 0)100% |
276 |
{ |
277 |
$metrics = $document->createElement('metrics'); |
278 |
|
279 |
$metrics->setAttribute('complexity', $complexity); |
280 |
$metrics->setAttribute('elements', $loc + $methods + $branches); |
281 |
$metrics->setAttribute('coveredelements', $coveredLines + $coveredMethods + $coveredBranches); |
282 |
$metrics->setAttribute('conditionals', $branches); |
283 |
$metrics->setAttribute('coveredconditionals', $coveredBranches); |
284 |
$metrics->setAttribute('statements', $loc); |
285 |
$metrics->setAttribute('coveredstatements', $coveredLines); |
286 |
$metrics->setAttribute('methods', $methods); |
287 |
$metrics->setAttribute('coveredmethods', $coveredMethods); |
288 |
$metrics->setAttribute('testduration', 0); |
289 |
$metrics->setAttribute('testfailures', 0); |
290 |
$metrics->setAttribute('testpasses', 0); |
291 |
$metrics->setAttribute('testruns', 0); |
292 |
|
293 |
return $metrics; |
294 |
} |
295 |
|
296 |
protected function makeLineElement(\DOMDocument $document, $linenum, $count = 1)100% |
297 |
{ |
298 |
$line = $document->createElement('line'); |
299 |
|
300 |
$line->setAttribute('num', $linenum); |
301 |
$line->setAttribute('type', self::lineTypeStatement); |
302 |
$line->setAttribute('complexity', 0); |
303 |
$line->setAttribute('count', $count); |
304 |
$line->setAttribute('falsecount', 0); |
305 |
$line->setAttribute('truecount', 0); |
306 |
$line->setAttribute('signature', ''); |
307 |
$line->setAttribute('testduration', 0); |
308 |
$line->setAttribute('testsuccess', 0); |
309 |
|
310 |
return $line; |
311 |
} |
312 |
|
313 |
protected function addLoc($count)100% |
314 |
{ |
315 |
$this->loc += $count; |
316 |
|
317 |
return $this; |
318 |
} |
319 |
|
320 |
protected function addCoveredLoc($count)100% |
321 |
{ |
322 |
$this->coveredLoc += $count; |
323 |
|
324 |
return $this; |
325 |
} |
326 |
|
327 |
protected function addMethod($count)100% |
328 |
{ |
329 |
$this->methods += $count; |
330 |
|
331 |
return $this; |
332 |
} |
333 |
|
334 |
protected function addCoveredMethod($count)100% |
335 |
{ |
336 |
$this->coveredMethods += $count; |
337 |
|
338 |
return $this; |
339 |
} |
340 |
|
341 |
protected function addBranches($count)100% |
342 |
{ |
343 |
$this->branches += $count; |
344 |
|
345 |
return $this; |
346 |
} |
347 |
|
348 |
protected function addCoveredBranches($count)100% |
349 |
{ |
350 |
$this->coveredBranches += $count; |
351 |
|
352 |
return $this; |
353 |
} |
354 |
|
355 |
protected function addPaths($count)100% |
356 |
{ |
357 |
$this->paths += $count; |
358 |
|
359 |
return $this; |
360 |
} |
361 |
|
362 |
protected function addClasses($count)100% |
363 |
{ |
364 |
$this->classes += $count; |
365 |
|
366 |
return $this; |
367 |
} |
368 |
} |