1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
use XMLReader; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class JacocoReport |
8
|
|
|
* Used for reading in a Jacoco coverage report |
9
|
|
|
* @package exussum12\CoverageChecker |
10
|
|
|
*/ |
11
|
|
|
class JacocoReport extends CloverLoader |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
public function parseLines() |
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() |
36
|
|
|
{ |
37
|
|
|
return 'Parses xml coverage report produced by Jacoco'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $reader |
42
|
|
|
* @param $currentFile |
43
|
|
|
*/ |
44
|
|
|
protected function addLine($reader, $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
|
|
View Code Duplication |
protected function findFile($reader, $currentNamespace, $currentFile) |
|
|
|
|
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($reader, $currentNamespace) |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.