1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the PHP_CodeCoverage package. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Bergmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Generates a Clover XML logfile from an PHP_CodeCoverage object. |
13
|
|
|
* |
14
|
|
|
* @since Class available since Release 1.0.0 |
15
|
|
|
*/ |
16
|
|
|
class PHP_CodeCoverage_Report_Clover |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param PHP_CodeCoverage $coverage |
20
|
|
|
* @param string $target |
21
|
|
|
* @param string $name |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function process(PHP_CodeCoverage $coverage, $target = null, $name = null) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$xmlDocument = new DOMDocument('1.0', 'UTF-8'); |
27
|
|
|
$xmlDocument->formatOutput = true; |
28
|
|
|
|
29
|
|
|
$xmlCoverage = $xmlDocument->createElement('coverage'); |
30
|
|
|
$xmlCoverage->setAttribute('generated', (int) $_SERVER['REQUEST_TIME']); |
31
|
|
|
$xmlDocument->appendChild($xmlCoverage); |
32
|
|
|
|
33
|
|
|
$xmlProject = $xmlDocument->createElement('project'); |
34
|
|
|
$xmlProject->setAttribute('timestamp', (int) $_SERVER['REQUEST_TIME']); |
35
|
|
|
|
36
|
|
|
if (is_string($name)) { |
37
|
|
|
$xmlProject->setAttribute('name', $name); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$xmlCoverage->appendChild($xmlProject); |
41
|
|
|
|
42
|
|
|
$packages = array(); |
43
|
|
|
$report = $coverage->getReport(); |
44
|
|
|
unset($coverage); |
45
|
|
|
|
46
|
|
|
foreach ($report as $item) { |
47
|
|
|
$namespace = 'global'; |
48
|
|
|
|
49
|
|
|
if (!$item instanceof PHP_CodeCoverage_Report_Node_File) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$xmlFile = $xmlDocument->createElement('file'); |
54
|
|
|
$xmlFile->setAttribute('name', $item->getPath()); |
55
|
|
|
|
56
|
|
|
$classes = $item->getClassesAndTraits(); |
57
|
|
|
$coverage = $item->getCoverageData(); |
58
|
|
|
$lines = array(); |
59
|
|
|
|
60
|
|
|
foreach ($classes as $className => $class) { |
61
|
|
|
$classStatements = 0; |
62
|
|
|
$coveredClassStatements = 0; |
63
|
|
|
$coveredMethods = 0; |
64
|
|
|
$classMethods = 0; |
65
|
|
|
|
66
|
|
|
foreach ($class['methods'] as $methodName => $method) { |
67
|
|
|
if ($method['executableLines'] == 0) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$classMethods++; |
72
|
|
|
$classStatements += $method['executableLines']; |
73
|
|
|
$coveredClassStatements += $method['executedLines']; |
74
|
|
|
if ($method['coverage'] == 100) { |
75
|
|
|
$coveredMethods++; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$methodCount = 0; |
79
|
|
|
for ($i = $method['startLine']; |
80
|
|
|
$i <= $method['endLine']; |
81
|
|
|
$i++) { |
82
|
|
|
if (isset($coverage[$i]) && ($coverage[$i] !== null)) { |
83
|
|
|
$methodCount = max($methodCount, count($coverage[$i])); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$lines[$method['startLine']] = array( |
88
|
|
|
'count' => $methodCount, |
89
|
|
|
'crap' => $method['crap'], |
90
|
|
|
'type' => 'method', |
91
|
|
|
'name' => $methodName |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!empty($class['package']['namespace'])) { |
96
|
|
|
$namespace = $class['package']['namespace']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$xmlClass = $xmlDocument->createElement('class'); |
100
|
|
|
$xmlClass->setAttribute('name', $className); |
101
|
|
|
$xmlClass->setAttribute('namespace', $namespace); |
102
|
|
|
|
103
|
|
View Code Duplication |
if (!empty($class['package']['fullPackage'])) { |
|
|
|
|
104
|
|
|
$xmlClass->setAttribute( |
105
|
|
|
'fullPackage', |
106
|
|
|
$class['package']['fullPackage'] |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
if (!empty($class['package']['category'])) { |
|
|
|
|
111
|
|
|
$xmlClass->setAttribute( |
112
|
|
|
'category', |
113
|
|
|
$class['package']['category'] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
if (!empty($class['package']['package'])) { |
|
|
|
|
118
|
|
|
$xmlClass->setAttribute( |
119
|
|
|
'package', |
120
|
|
|
$class['package']['package'] |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
if (!empty($class['package']['subpackage'])) { |
|
|
|
|
125
|
|
|
$xmlClass->setAttribute( |
126
|
|
|
'subpackage', |
127
|
|
|
$class['package']['subpackage'] |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$xmlFile->appendChild($xmlClass); |
132
|
|
|
|
133
|
|
|
$xmlMetrics = $xmlDocument->createElement('metrics'); |
134
|
|
|
$xmlMetrics->setAttribute('methods', $classMethods); |
135
|
|
|
$xmlMetrics->setAttribute('coveredmethods', $coveredMethods); |
136
|
|
|
$xmlMetrics->setAttribute('conditionals', 0); |
137
|
|
|
$xmlMetrics->setAttribute('coveredconditionals', 0); |
138
|
|
|
$xmlMetrics->setAttribute('statements', $classStatements); |
139
|
|
|
$xmlMetrics->setAttribute( |
140
|
|
|
'coveredstatements', |
141
|
|
|
$coveredClassStatements |
142
|
|
|
); |
143
|
|
|
$xmlMetrics->setAttribute( |
144
|
|
|
'elements', |
145
|
|
|
$classMethods + |
146
|
|
|
$classStatements |
147
|
|
|
/* + conditionals */ |
148
|
|
|
); |
149
|
|
|
$xmlMetrics->setAttribute( |
150
|
|
|
'coveredelements', |
151
|
|
|
$coveredMethods + |
152
|
|
|
$coveredClassStatements |
153
|
|
|
/* + coveredconditionals */ |
154
|
|
|
); |
155
|
|
|
$xmlClass->appendChild($xmlMetrics); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach ($coverage as $line => $data) { |
159
|
|
|
if ($data === null || isset($lines[$line])) { |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$lines[$line] = array( |
164
|
|
|
'count' => count($data), 'type' => 'stmt' |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
ksort($lines); |
169
|
|
|
|
170
|
|
|
foreach ($lines as $line => $data) { |
171
|
|
|
$xmlLine = $xmlDocument->createElement('line'); |
172
|
|
|
$xmlLine->setAttribute('num', $line); |
173
|
|
|
$xmlLine->setAttribute('type', $data['type']); |
174
|
|
|
|
175
|
|
|
if (isset($data['name'])) { |
176
|
|
|
$xmlLine->setAttribute('name', $data['name']); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (isset($data['crap'])) { |
180
|
|
|
$xmlLine->setAttribute('crap', $data['crap']); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$xmlLine->setAttribute('count', $data['count']); |
184
|
|
|
$xmlFile->appendChild($xmlLine); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$linesOfCode = $item->getLinesOfCode(); |
188
|
|
|
|
189
|
|
|
$xmlMetrics = $xmlDocument->createElement('metrics'); |
190
|
|
|
$xmlMetrics->setAttribute('loc', $linesOfCode['loc']); |
191
|
|
|
$xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']); |
192
|
|
|
$xmlMetrics->setAttribute('classes', $item->getNumClassesAndTraits()); |
193
|
|
|
$xmlMetrics->setAttribute('methods', $item->getNumMethods()); |
194
|
|
|
$xmlMetrics->setAttribute( |
195
|
|
|
'coveredmethods', |
196
|
|
|
$item->getNumTestedMethods() |
197
|
|
|
); |
198
|
|
|
$xmlMetrics->setAttribute('conditionals', 0); |
199
|
|
|
$xmlMetrics->setAttribute('coveredconditionals', 0); |
200
|
|
|
$xmlMetrics->setAttribute( |
201
|
|
|
'statements', |
202
|
|
|
$item->getNumExecutableLines() |
203
|
|
|
); |
204
|
|
|
$xmlMetrics->setAttribute( |
205
|
|
|
'coveredstatements', |
206
|
|
|
$item->getNumExecutedLines() |
207
|
|
|
); |
208
|
|
|
$xmlMetrics->setAttribute( |
209
|
|
|
'elements', |
210
|
|
|
$item->getNumMethods() + $item->getNumExecutableLines() |
211
|
|
|
/* + conditionals */ |
212
|
|
|
); |
213
|
|
|
$xmlMetrics->setAttribute( |
214
|
|
|
'coveredelements', |
215
|
|
|
$item->getNumTestedMethods() + $item->getNumExecutedLines() |
216
|
|
|
/* + coveredconditionals */ |
217
|
|
|
); |
218
|
|
|
$xmlFile->appendChild($xmlMetrics); |
219
|
|
|
|
220
|
|
|
if ($namespace == 'global') { |
221
|
|
|
$xmlProject->appendChild($xmlFile); |
222
|
|
|
} else { |
223
|
|
|
if (!isset($packages[$namespace])) { |
224
|
|
|
$packages[$namespace] = $xmlDocument->createElement( |
225
|
|
|
'package' |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
$packages[$namespace]->setAttribute('name', $namespace); |
229
|
|
|
$xmlProject->appendChild($packages[$namespace]); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$packages[$namespace]->appendChild($xmlFile); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$linesOfCode = $report->getLinesOfCode(); |
237
|
|
|
|
238
|
|
|
$xmlMetrics = $xmlDocument->createElement('metrics'); |
239
|
|
|
$xmlMetrics->setAttribute('files', count($report)); |
240
|
|
|
$xmlMetrics->setAttribute('loc', $linesOfCode['loc']); |
241
|
|
|
$xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']); |
242
|
|
|
$xmlMetrics->setAttribute( |
243
|
|
|
'classes', |
244
|
|
|
$report->getNumClassesAndTraits() |
245
|
|
|
); |
246
|
|
|
$xmlMetrics->setAttribute('methods', $report->getNumMethods()); |
247
|
|
|
$xmlMetrics->setAttribute( |
248
|
|
|
'coveredmethods', |
249
|
|
|
$report->getNumTestedMethods() |
250
|
|
|
); |
251
|
|
|
$xmlMetrics->setAttribute('conditionals', 0); |
252
|
|
|
$xmlMetrics->setAttribute('coveredconditionals', 0); |
253
|
|
|
$xmlMetrics->setAttribute( |
254
|
|
|
'statements', |
255
|
|
|
$report->getNumExecutableLines() |
256
|
|
|
); |
257
|
|
|
$xmlMetrics->setAttribute( |
258
|
|
|
'coveredstatements', |
259
|
|
|
$report->getNumExecutedLines() |
260
|
|
|
); |
261
|
|
|
$xmlMetrics->setAttribute( |
262
|
|
|
'elements', |
263
|
|
|
$report->getNumMethods() + $report->getNumExecutableLines() |
264
|
|
|
/* + conditionals */ |
265
|
|
|
); |
266
|
|
|
$xmlMetrics->setAttribute( |
267
|
|
|
'coveredelements', |
268
|
|
|
$report->getNumTestedMethods() + $report->getNumExecutedLines() |
269
|
|
|
/* + coveredconditionals */ |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
$xmlProject->appendChild($xmlMetrics); |
273
|
|
|
|
274
|
|
View Code Duplication |
if ($target !== null) { |
|
|
|
|
275
|
|
|
if (!is_dir(dirname($target))) { |
276
|
|
|
mkdir(dirname($target), 0777, true); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return $xmlDocument->save($target); |
280
|
|
|
} else { |
281
|
|
|
return $xmlDocument->saveXML(); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.