1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker\Loaders; |
3
|
|
|
|
4
|
|
|
use XMLReader; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Jacoco |
8
|
|
|
* Used for reading in a Jacoco coverage report |
9
|
|
|
* @package exussum12\CoverageChecker |
10
|
|
|
*/ |
11
|
|
|
class Jacoco extends Clover |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
public function parseLines(): array |
17
|
|
|
{ |
18
|
|
|
$this->coveredLines = []; |
19
|
|
|
$reader = new XMLReader; |
20
|
|
|
$reader->open($this->file); |
21
|
|
|
$currentNamespace = ''; |
22
|
|
|
$currentFile = ''; |
23
|
|
|
|
24
|
|
|
while ($reader->read()) { |
25
|
|
|
$currentNamespace = $this->findNamespace($reader, $currentNamespace); |
26
|
|
|
|
27
|
|
|
$currentFile = $this->findFile($reader, $currentNamespace, $currentFile); |
28
|
|
|
|
29
|
|
|
$this->addLine($reader, $currentFile); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return array_keys($this->coveredLines); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function getDescription(): string |
36
|
|
|
{ |
37
|
|
|
return 'Parses xml coverage report produced by Jacoco'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param XMLReader $reader |
42
|
|
|
* @param string $currentFile |
43
|
|
|
*/ |
44
|
|
|
protected function addLine(XMLReader $reader, string $currentFile) |
45
|
|
|
{ |
46
|
|
|
if (( |
47
|
|
|
$reader->name === "line" |
48
|
|
|
)) { |
49
|
|
|
$this->coveredLines |
50
|
|
|
[$currentFile] |
51
|
|
|
[$reader->getAttribute('nr')] |
52
|
|
|
= $reader->getAttribute("mi") == 0; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function findFile(XMLReader $reader, string $currentNamespace, string $currentFile): string |
57
|
|
|
{ |
58
|
|
|
if (( |
59
|
|
|
$reader->name === "sourcefile" && |
60
|
|
|
$reader->nodeType == XMLReader::ELEMENT |
61
|
|
|
)) { |
62
|
|
|
$currentFile = $currentNamespace . '/' . $reader->getAttribute('name'); |
63
|
|
|
$this->coveredLines[$currentFile] = []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $currentFile; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function findNamespace(XMLReader $reader, string $currentNamespace): string |
70
|
|
|
{ |
71
|
|
|
if (( |
72
|
|
|
$reader->name === "package" && |
73
|
|
|
$reader->nodeType == XMLReader::ELEMENT |
74
|
|
|
)) { |
75
|
|
|
$currentNamespace = $reader->getAttribute('name'); |
76
|
|
|
} |
77
|
|
|
return $currentNamespace; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|