|
1
|
|
|
<?php |
|
2
|
|
|
namespace exussum12\CoverageChecker\Loaders; |
|
3
|
|
|
|
|
4
|
|
|
use exussum12\CoverageChecker\FileChecker; |
|
5
|
|
|
use XMLReader; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class XMLReport |
|
9
|
|
|
* Used for reading in a phpunit clover XML file |
|
10
|
|
|
* @package exussum12\CoverageChecker |
|
11
|
|
|
*/ |
|
12
|
|
|
class Clover implements FileChecker |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $file; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $coveredLines; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* XMLReport constructor. |
|
25
|
|
|
* @param string $file the path the to phpunit clover file |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($file) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->file = $file; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function parseLines(): array |
|
36
|
|
|
{ |
|
37
|
|
|
$this->coveredLines = []; |
|
38
|
|
|
$reader = new XMLReader; |
|
39
|
|
|
$reader->open($this->file); |
|
40
|
|
|
$currentFile = ''; |
|
41
|
|
|
while ($reader->read()) { |
|
42
|
|
|
$currentFile = $this->checkForNewFiles($reader, $currentFile); |
|
43
|
|
|
|
|
44
|
|
|
$this->handleStatement($reader, $currentFile); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return array_keys($this->coveredLines); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getErrorsOnLine(string $file, int $line) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!isset($this->coveredLines[$file][$line])) { |
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
return $this->coveredLines[$file][$line] > 0 ? |
|
59
|
|
|
[]: |
|
60
|
|
|
['No unit test covering this line'] |
|
61
|
|
|
; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function handleNotFoundFile() |
|
68
|
|
|
{ |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function getDescription(): string |
|
76
|
|
|
{ |
|
77
|
|
|
return 'Parses text output in clover (xml) format'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function checkForNewFiles(XMLReader $reader, string $currentFile) |
|
81
|
|
|
{ |
|
82
|
|
|
if (( |
|
83
|
|
|
$reader->name === "file" && |
|
84
|
|
|
$reader->nodeType == XMLReader::ELEMENT |
|
85
|
|
|
)) { |
|
86
|
|
|
$currentFile = $reader->getAttribute('name'); |
|
87
|
|
|
$this->coveredLines[$currentFile] = []; |
|
88
|
|
|
} |
|
89
|
|
|
return $currentFile; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function addLine(XMLReader $reader, string $currentFile) |
|
93
|
|
|
{ |
|
94
|
|
|
$covered = $reader->getAttribute('count') > 0; |
|
95
|
|
|
|
|
96
|
|
|
$this->coveredLines |
|
97
|
|
|
[$currentFile] |
|
98
|
|
|
[$reader->getAttribute('num')] |
|
99
|
|
|
= $covered ?: "No test coverage"; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function handleStatement(XMLReader $reader, string $currentFile) |
|
103
|
|
|
{ |
|
104
|
|
|
if (( |
|
105
|
|
|
$reader->name === "line" && |
|
106
|
|
|
$reader->getAttribute("type") == "stmt" |
|
107
|
|
|
)) { |
|
108
|
|
|
$this->addLine($reader, $currentFile); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|