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