1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
use XMLReader; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class PhpMdLoader |
8
|
|
|
* Used for parsing phpmd xml output |
9
|
|
|
* @package exussum12\CoverageChecker |
10
|
|
|
*/ |
11
|
|
|
class PhpMdLoader implements FileChecker |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $file; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $errors = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $errorRanges = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* PhpMdLoader constructor. |
30
|
|
|
* @param string $file the path to the phpmd xml file |
31
|
|
|
*/ |
32
|
|
|
public function __construct($file) |
33
|
|
|
{ |
34
|
|
|
$this->file = $file; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function getLines() |
41
|
|
|
{ |
42
|
|
|
$this->errors = []; |
43
|
|
|
$this->errorRanges = []; |
44
|
|
|
$reader = new XMLReader; |
45
|
|
|
$reader->open($this->file); |
46
|
|
|
$currentFile = ""; |
47
|
|
|
while ($reader->read()) { |
48
|
|
|
$currentFile = $this->checkForNewFile($reader, $currentFile); |
49
|
|
|
$this->checkForViolation($reader, $currentFile); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->errors; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function isValidLine($file, $lineNumber) |
59
|
|
|
{ |
60
|
|
|
$valid = true; |
61
|
|
|
foreach ($this->errorRanges[$file] as $number => $errors) { |
62
|
|
|
if (( |
63
|
|
|
$errors['start'] <= $lineNumber && |
64
|
|
|
$errors['end'] >= $lineNumber |
65
|
|
|
)) { |
66
|
|
|
//unset this error |
67
|
|
|
unset($this->errorRanges[$file][$number]); |
68
|
|
|
$valid = false; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $valid; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param XMLReader $reader |
77
|
|
|
* @param string $currentFile |
78
|
|
|
*/ |
79
|
|
|
protected function checkForViolation(XMLReader $reader, $currentFile) |
80
|
|
|
{ |
81
|
|
|
if (( |
82
|
|
|
$reader->name === 'violation' && |
83
|
|
|
$reader->nodeType == XMLReader::ELEMENT |
84
|
|
|
)) { |
85
|
|
|
$error = trim($reader->readString()); |
86
|
|
|
$start = $reader->getAttribute('beginline'); |
87
|
|
|
$end = $reader->getAttribute('endline'); |
88
|
|
|
$this->errorRanges[$currentFile][] = [ |
89
|
|
|
'start' => $start, |
90
|
|
|
'end' => $end, |
91
|
|
|
'error' => $error, |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
$this->addForAllLines($currentFile, $start, $end, $error); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param XMLReader $reader |
100
|
|
|
* @param string $currentFile |
101
|
|
|
* @return string the currentFileName |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
protected function checkForNewFile(XMLReader $reader, $currentFile) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
if (( |
106
|
|
|
$reader->name === 'file' && |
107
|
|
|
$reader->nodeType == XMLReader::ELEMENT |
108
|
|
|
) |
109
|
|
|
) { |
110
|
|
|
$currentFile = $reader->getAttribute('name'); |
111
|
|
|
$this->errors[$currentFile] = []; |
112
|
|
|
return $currentFile; |
113
|
|
|
} |
114
|
|
|
return $currentFile; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function handleNotFoundFile() |
121
|
|
|
{ |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public static function getDescription() |
129
|
|
|
{ |
130
|
|
|
return 'Parses the xml report format of phpmd, this mode ' . |
131
|
|
|
'reports multi line violations once per diff, instead ' . |
132
|
|
|
'of on each line the violation occurs'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function addForAllLines($currentFile, $start, $end, $error) |
136
|
|
|
{ |
137
|
|
|
for ($i = $start; $i <= $end; $i++) { |
138
|
|
|
if (( |
139
|
|
|
!isset($this->errors[$currentFile][$i]) || |
140
|
|
|
!in_array($error, $this->errors[$currentFile][$i]) |
141
|
|
|
)) { |
142
|
|
|
$this->errors[$currentFile][$i][] = $error; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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.