| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of phpunit/php-code-coverage.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace SebastianBergmann\CodeCoverage\Report;
|
|
|
11 |
|
|
|
12 |
use function count;
|
|
|
13 |
use function dirname;
|
|
|
14 |
use function file_put_contents;
|
|
|
15 |
use function is_string;
|
|
|
16 |
use function ksort;
|
|
|
17 |
use function max;
|
|
|
18 |
use function range;
|
|
|
19 |
use function time;
|
|
|
20 |
use DOMDocument;
|
|
|
21 |
use SebastianBergmann\CodeCoverage\CodeCoverage;
|
|
|
22 |
use SebastianBergmann\CodeCoverage\Driver\WriteOperationFailedException;
|
|
|
23 |
use SebastianBergmann\CodeCoverage\Node\File;
|
|
|
24 |
use SebastianBergmann\CodeCoverage\Util\Filesystem;
|
|
|
25 |
|
|
|
26 |
final class Clover
|
|
|
27 |
{
|
|
|
28 |
/**
|
|
|
29 |
* @throws WriteOperationFailedException
|
|
|
30 |
*/
|
|
|
31 |
public function process(CodeCoverage $coverage, ?string $target = null, ?string $name = null): string
|
|
|
32 |
{
|
|
|
33 |
$time = (string) time();
|
|
|
34 |
|
|
|
35 |
$xmlDocument = new DOMDocument('1.0', 'UTF-8');
|
|
|
36 |
$xmlDocument->formatOutput = true;
|
|
|
37 |
|
|
|
38 |
$xmlCoverage = $xmlDocument->createElement('coverage');
|
|
|
39 |
$xmlCoverage->setAttribute('generated', $time);
|
|
|
40 |
$xmlDocument->appendChild($xmlCoverage);
|
|
|
41 |
|
|
|
42 |
$xmlProject = $xmlDocument->createElement('project');
|
|
|
43 |
$xmlProject->setAttribute('timestamp', $time);
|
|
|
44 |
|
|
|
45 |
if (is_string($name)) {
|
|
|
46 |
$xmlProject->setAttribute('name', $name);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$xmlCoverage->appendChild($xmlProject);
|
|
|
50 |
|
|
|
51 |
$packages = [];
|
|
|
52 |
$report = $coverage->getReport();
|
|
|
53 |
|
|
|
54 |
foreach ($report as $item) {
|
|
|
55 |
if (!$item instanceof File) {
|
|
|
56 |
continue;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/* @var File $item */
|
|
|
60 |
|
|
|
61 |
$xmlFile = $xmlDocument->createElement('file');
|
|
|
62 |
$xmlFile->setAttribute('name', $item->pathAsString());
|
|
|
63 |
|
|
|
64 |
$classes = $item->classesAndTraits();
|
|
|
65 |
$coverageData = $item->lineCoverageData();
|
|
|
66 |
$lines = [];
|
|
|
67 |
$namespace = 'global';
|
|
|
68 |
|
|
|
69 |
foreach ($classes as $className => $class) {
|
|
|
70 |
$classStatements = 0;
|
|
|
71 |
$coveredClassStatements = 0;
|
|
|
72 |
$coveredMethods = 0;
|
|
|
73 |
$classMethods = 0;
|
|
|
74 |
|
|
|
75 |
foreach ($class['methods'] as $methodName => $method) {
|
|
|
76 |
if ($method['executableLines'] == 0) {
|
|
|
77 |
continue;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$classMethods++;
|
|
|
81 |
$classStatements += $method['executableLines'];
|
|
|
82 |
$coveredClassStatements += $method['executedLines'];
|
|
|
83 |
|
|
|
84 |
if ($method['coverage'] == 100) {
|
|
|
85 |
$coveredMethods++;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$methodCount = 0;
|
|
|
89 |
|
|
|
90 |
foreach (range($method['startLine'], $method['endLine']) as $line) {
|
|
|
91 |
if (isset($coverageData[$line]) && ($coverageData[$line] !== null)) {
|
|
|
92 |
$methodCount = max($methodCount, count($coverageData[$line]));
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$lines[$method['startLine']] = [
|
|
|
97 |
'ccn' => $method['ccn'],
|
|
|
98 |
'count' => $methodCount,
|
|
|
99 |
'crap' => $method['crap'],
|
|
|
100 |
'type' => 'method',
|
|
|
101 |
'visibility' => $method['visibility'],
|
|
|
102 |
'name' => $methodName,
|
|
|
103 |
];
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
if (!empty($class['package']['namespace'])) {
|
|
|
107 |
$namespace = $class['package']['namespace'];
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
$xmlClass = $xmlDocument->createElement('class');
|
|
|
111 |
$xmlClass->setAttribute('name', $className);
|
|
|
112 |
$xmlClass->setAttribute('namespace', $namespace);
|
|
|
113 |
|
|
|
114 |
if (!empty($class['package']['fullPackage'])) {
|
|
|
115 |
$xmlClass->setAttribute(
|
|
|
116 |
'fullPackage',
|
|
|
117 |
$class['package']['fullPackage']
|
|
|
118 |
);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
if (!empty($class['package']['category'])) {
|
|
|
122 |
$xmlClass->setAttribute(
|
|
|
123 |
'category',
|
|
|
124 |
$class['package']['category']
|
|
|
125 |
);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
if (!empty($class['package']['package'])) {
|
|
|
129 |
$xmlClass->setAttribute(
|
|
|
130 |
'package',
|
|
|
131 |
$class['package']['package']
|
|
|
132 |
);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
if (!empty($class['package']['subpackage'])) {
|
|
|
136 |
$xmlClass->setAttribute(
|
|
|
137 |
'subpackage',
|
|
|
138 |
$class['package']['subpackage']
|
|
|
139 |
);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
$xmlFile->appendChild($xmlClass);
|
|
|
143 |
|
|
|
144 |
$xmlMetrics = $xmlDocument->createElement('metrics');
|
|
|
145 |
$xmlMetrics->setAttribute('complexity', (string) $class['ccn']);
|
|
|
146 |
$xmlMetrics->setAttribute('methods', (string) $classMethods);
|
|
|
147 |
$xmlMetrics->setAttribute('coveredmethods', (string) $coveredMethods);
|
|
|
148 |
$xmlMetrics->setAttribute('conditionals', (string) $class['executableBranches']);
|
|
|
149 |
$xmlMetrics->setAttribute('coveredconditionals', (string) $class['executedBranches']);
|
|
|
150 |
$xmlMetrics->setAttribute('statements', (string) $classStatements);
|
|
|
151 |
$xmlMetrics->setAttribute('coveredstatements', (string) $coveredClassStatements);
|
|
|
152 |
$xmlMetrics->setAttribute('elements', (string) ($classMethods + $classStatements + $class['executableBranches']));
|
|
|
153 |
$xmlMetrics->setAttribute('coveredelements', (string) ($coveredMethods + $coveredClassStatements + $class['executedBranches']));
|
|
|
154 |
$xmlClass->appendChild($xmlMetrics);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
foreach ($coverageData as $line => $data) {
|
|
|
158 |
if ($data === null || isset($lines[$line])) {
|
|
|
159 |
continue;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
$lines[$line] = [
|
|
|
163 |
'count' => count($data), 'type' => 'stmt',
|
|
|
164 |
];
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
ksort($lines);
|
|
|
168 |
|
|
|
169 |
foreach ($lines as $line => $data) {
|
|
|
170 |
$xmlLine = $xmlDocument->createElement('line');
|
|
|
171 |
$xmlLine->setAttribute('num', (string) $line);
|
|
|
172 |
$xmlLine->setAttribute('type', $data['type']);
|
|
|
173 |
|
|
|
174 |
if (isset($data['name'])) {
|
|
|
175 |
$xmlLine->setAttribute('name', $data['name']);
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
if (isset($data['visibility'])) {
|
|
|
179 |
$xmlLine->setAttribute('visibility', $data['visibility']);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
if (isset($data['ccn'])) {
|
|
|
183 |
$xmlLine->setAttribute('complexity', (string) $data['ccn']);
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
if (isset($data['crap'])) {
|
|
|
187 |
$xmlLine->setAttribute('crap', (string) $data['crap']);
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
$xmlLine->setAttribute('count', (string) $data['count']);
|
|
|
191 |
$xmlFile->appendChild($xmlLine);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
$linesOfCode = $item->linesOfCode();
|
|
|
195 |
|
|
|
196 |
$xmlMetrics = $xmlDocument->createElement('metrics');
|
|
|
197 |
$xmlMetrics->setAttribute('loc', (string) $linesOfCode['linesOfCode']);
|
|
|
198 |
$xmlMetrics->setAttribute('ncloc', (string) $linesOfCode['nonCommentLinesOfCode']);
|
|
|
199 |
$xmlMetrics->setAttribute('classes', (string) $item->numberOfClassesAndTraits());
|
|
|
200 |
$xmlMetrics->setAttribute('methods', (string) $item->numberOfMethods());
|
|
|
201 |
$xmlMetrics->setAttribute('coveredmethods', (string) $item->numberOfTestedMethods());
|
|
|
202 |
$xmlMetrics->setAttribute('conditionals', (string) $item->numberOfExecutableBranches());
|
|
|
203 |
$xmlMetrics->setAttribute('coveredconditionals', (string) $item->numberOfExecutedBranches());
|
|
|
204 |
$xmlMetrics->setAttribute('statements', (string) $item->numberOfExecutableLines());
|
|
|
205 |
$xmlMetrics->setAttribute('coveredstatements', (string) $item->numberOfExecutedLines());
|
|
|
206 |
$xmlMetrics->setAttribute('elements', (string) ($item->numberOfMethods() + $item->numberOfExecutableLines() + $item->numberOfExecutableBranches()));
|
|
|
207 |
$xmlMetrics->setAttribute('coveredelements', (string) ($item->numberOfTestedMethods() + $item->numberOfExecutedLines() + $item->numberOfExecutedBranches()));
|
|
|
208 |
$xmlFile->appendChild($xmlMetrics);
|
|
|
209 |
|
|
|
210 |
if ($namespace === 'global') {
|
|
|
211 |
$xmlProject->appendChild($xmlFile);
|
|
|
212 |
} else {
|
|
|
213 |
if (!isset($packages[$namespace])) {
|
|
|
214 |
$packages[$namespace] = $xmlDocument->createElement(
|
|
|
215 |
'package'
|
|
|
216 |
);
|
|
|
217 |
|
|
|
218 |
$packages[$namespace]->setAttribute('name', $namespace);
|
|
|
219 |
$xmlProject->appendChild($packages[$namespace]);
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
$packages[$namespace]->appendChild($xmlFile);
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
$linesOfCode = $report->linesOfCode();
|
|
|
227 |
|
|
|
228 |
$xmlMetrics = $xmlDocument->createElement('metrics');
|
|
|
229 |
$xmlMetrics->setAttribute('files', (string) count($report));
|
|
|
230 |
$xmlMetrics->setAttribute('loc', (string) $linesOfCode['linesOfCode']);
|
|
|
231 |
$xmlMetrics->setAttribute('ncloc', (string) $linesOfCode['nonCommentLinesOfCode']);
|
|
|
232 |
$xmlMetrics->setAttribute('classes', (string) $report->numberOfClassesAndTraits());
|
|
|
233 |
$xmlMetrics->setAttribute('methods', (string) $report->numberOfMethods());
|
|
|
234 |
$xmlMetrics->setAttribute('coveredmethods', (string) $report->numberOfTestedMethods());
|
|
|
235 |
$xmlMetrics->setAttribute('conditionals', (string) $report->numberOfExecutableBranches());
|
|
|
236 |
$xmlMetrics->setAttribute('coveredconditionals', (string) $report->numberOfExecutedBranches());
|
|
|
237 |
$xmlMetrics->setAttribute('statements', (string) $report->numberOfExecutableLines());
|
|
|
238 |
$xmlMetrics->setAttribute('coveredstatements', (string) $report->numberOfExecutedLines());
|
|
|
239 |
$xmlMetrics->setAttribute('elements', (string) ($report->numberOfMethods() + $report->numberOfExecutableLines() + $report->numberOfExecutableBranches()));
|
|
|
240 |
$xmlMetrics->setAttribute('coveredelements', (string) ($report->numberOfTestedMethods() + $report->numberOfExecutedLines() + $report->numberOfExecutedBranches()));
|
|
|
241 |
$xmlProject->appendChild($xmlMetrics);
|
|
|
242 |
|
|
|
243 |
$buffer = $xmlDocument->saveXML();
|
|
|
244 |
|
|
|
245 |
if ($target !== null) {
|
|
|
246 |
Filesystem::createDirectory(dirname($target));
|
|
|
247 |
|
|
|
248 |
if (@file_put_contents($target, $buffer) === false) {
|
|
|
249 |
throw new WriteOperationFailedException($target);
|
|
|
250 |
}
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
return $buffer;
|
|
|
254 |
}
|
|
|
255 |
}
|