Skip to content

Commit b212556

Browse files
Keep Report\Clover as-is and introduce new Report\OpenClover for reporter that generates Clover XML that validates against OpenClover's XML schema definition
1 parent a873add commit b212556

12 files changed

+495
-120
lines changed

src/Report/Clover.php

Lines changed: 47 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

12-
use function assert;
13-
use function basename;
1412
use function count;
1513
use function dirname;
1614
use function file_put_contents;
@@ -19,14 +17,11 @@
1917
use function max;
2018
use function range;
2119
use function str_contains;
22-
use function str_replace;
2320
use function time;
2421
use DOMDocument;
25-
use DOMElement;
2622
use SebastianBergmann\CodeCoverage\CodeCoverage;
2723
use SebastianBergmann\CodeCoverage\Node\File;
2824
use SebastianBergmann\CodeCoverage\Util\Filesystem;
29-
use SebastianBergmann\CodeCoverage\Version;
3025
use SebastianBergmann\CodeCoverage\WriteOperationFailedException;
3126

3227
final class Clover
@@ -42,7 +37,6 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
4237
$xmlDocument->formatOutput = true;
4338

4439
$xmlCoverage = $xmlDocument->createElement('coverage');
45-
$xmlCoverage->setAttribute('clover', Version::id());
4640
$xmlCoverage->setAttribute('generated', $time);
4741
$xmlDocument->appendChild($xmlCoverage);
4842

@@ -55,7 +49,6 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
5549

5650
$xmlCoverage->appendChild($xmlProject);
5751

58-
/** @var array<non-empty-string, DOMElement> $packages */
5952
$packages = [];
6053
$report = $coverage->getReport();
6154

@@ -64,9 +57,10 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
6457
continue;
6558
}
6659

60+
/* @var File $item */
61+
6762
$xmlFile = $xmlDocument->createElement('file');
68-
$xmlFile->setAttribute('name', basename($item->pathAsString()));
69-
$xmlFile->setAttribute('path', $item->pathAsString());
63+
$xmlFile->setAttribute('name', $item->pathAsString());
7064

7165
$classes = $item->classesAndTraits();
7266
$coverageData = $item->lineCoverageData();
@@ -110,28 +104,30 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
110104
$lines[$method['startLine']] = [
111105
'ccn' => $method['ccn'],
112106
'count' => $methodCount,
107+
'crap' => $method['crap'],
113108
'type' => 'method',
114-
'signature' => $method['signature'],
115109
'visibility' => $method['visibility'],
110+
'name' => $methodName,
116111
];
117112
}
118113

119114
$xmlClass = $xmlDocument->createElement('class');
120-
$xmlClass->setAttribute('name', str_replace($class['namespace'] . '\\', '', $className));
115+
$xmlClass->setAttribute('name', $className);
116+
$xmlClass->setAttribute('namespace', $namespace);
121117

122118
$xmlFile->appendChild($xmlClass);
123119

124120
$xmlMetrics = $xmlDocument->createElement('metrics');
125121
$xmlMetrics->setAttribute('complexity', (string) $class['ccn']);
126-
$xmlMetrics->setAttribute('elements', (string) ($classMethods + $classStatements + $class['executableBranches']));
127-
$xmlMetrics->setAttribute('coveredelements', (string) ($coveredMethods + $coveredClassStatements + $class['executedBranches']));
122+
$xmlMetrics->setAttribute('methods', (string) $classMethods);
123+
$xmlMetrics->setAttribute('coveredmethods', (string) $coveredMethods);
128124
$xmlMetrics->setAttribute('conditionals', (string) $class['executableBranches']);
129125
$xmlMetrics->setAttribute('coveredconditionals', (string) $class['executedBranches']);
130126
$xmlMetrics->setAttribute('statements', (string) $classStatements);
131127
$xmlMetrics->setAttribute('coveredstatements', (string) $coveredClassStatements);
132-
$xmlMetrics->setAttribute('methods', (string) $classMethods);
133-
$xmlMetrics->setAttribute('coveredmethods', (string) $coveredMethods);
134-
$xmlClass->insertBefore($xmlMetrics, $xmlClass->firstChild);
128+
$xmlMetrics->setAttribute('elements', (string) ($classMethods + $classStatements + $class['executableBranches']));
129+
$xmlMetrics->setAttribute('coveredelements', (string) ($coveredMethods + $coveredClassStatements + $class['executedBranches']));
130+
$xmlClass->appendChild($xmlMetrics);
135131
}
136132

137133
foreach ($coverageData as $line => $data) {
@@ -140,8 +136,7 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
140136
}
141137

142138
$lines[$line] = [
143-
'count' => count($data),
144-
'type' => 'stmt',
139+
'count' => count($data), 'type' => 'stmt',
145140
];
146141
}
147142

@@ -152,20 +147,23 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
152147
$xmlLine->setAttribute('num', (string) $line);
153148
$xmlLine->setAttribute('type', $data['type']);
154149

155-
if (isset($data['ccn'])) {
156-
$xmlLine->setAttribute('complexity', (string) $data['ccn']);
150+
if (isset($data['name'])) {
151+
$xmlLine->setAttribute('name', $data['name']);
157152
}
158153

159-
$xmlLine->setAttribute('count', (string) $data['count']);
154+
if (isset($data['visibility'])) {
155+
$xmlLine->setAttribute('visibility', $data['visibility']);
156+
}
160157

161-
if (isset($data['signature'])) {
162-
$xmlLine->setAttribute('signature', $data['signature']);
158+
if (isset($data['ccn'])) {
159+
$xmlLine->setAttribute('complexity', (string) $data['ccn']);
163160
}
164161

165-
if (isset($data['visibility'])) {
166-
$xmlLine->setAttribute('visibility', $data['visibility']);
162+
if (isset($data['crap'])) {
163+
$xmlLine->setAttribute('crap', (string) $data['crap']);
167164
}
168165

166+
$xmlLine->setAttribute('count', (string) $data['count']);
169167
$xmlFile->appendChild($xmlLine);
170168
}
171169

@@ -175,51 +173,30 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
175173
$xmlMetrics->setAttribute('loc', (string) $linesOfCode->linesOfCode());
176174
$xmlMetrics->setAttribute('ncloc', (string) $linesOfCode->nonCommentLinesOfCode());
177175
$xmlMetrics->setAttribute('classes', (string) $item->numberOfClassesAndTraits());
178-
$xmlMetrics->setAttribute('complexity', (string) $item->cyclomaticComplexity());
179-
$xmlMetrics->setAttribute('elements', (string) ($item->numberOfMethods() + $item->numberOfExecutableLines() + $item->numberOfExecutableBranches()));
180-
$xmlMetrics->setAttribute('coveredelements', (string) ($item->numberOfTestedMethods() + $item->numberOfExecutedLines() + $item->numberOfExecutedBranches()));
176+
$xmlMetrics->setAttribute('methods', (string) $item->numberOfMethods());
177+
$xmlMetrics->setAttribute('coveredmethods', (string) $item->numberOfTestedMethods());
181178
$xmlMetrics->setAttribute('conditionals', (string) $item->numberOfExecutableBranches());
182179
$xmlMetrics->setAttribute('coveredconditionals', (string) $item->numberOfExecutedBranches());
183180
$xmlMetrics->setAttribute('statements', (string) $item->numberOfExecutableLines());
184181
$xmlMetrics->setAttribute('coveredstatements', (string) $item->numberOfExecutedLines());
185-
$xmlMetrics->setAttribute('methods', (string) $item->numberOfMethods());
186-
$xmlMetrics->setAttribute('coveredmethods', (string) $item->numberOfTestedMethods());
187-
$xmlFile->insertBefore($xmlMetrics, $xmlFile->firstChild);
188-
189-
if (!isset($packages[$namespace])) {
190-
$packages[$namespace] = $xmlDocument->createElement('package');
191-
$packages[$namespace]->setAttribute('name', $namespace);
192-
193-
$xmlPackageMetrics = $xmlDocument->createElement('metrics');
194-
$xmlPackageMetrics->setAttribute('complexity', '0');
195-
$xmlPackageMetrics->setAttribute('elements', '0');
196-
$xmlPackageMetrics->setAttribute('coveredelements', '0');
197-
$xmlPackageMetrics->setAttribute('conditionals', '0');
198-
$xmlPackageMetrics->setAttribute('coveredconditionals', '0');
199-
$xmlPackageMetrics->setAttribute('statements', '0');
200-
$xmlPackageMetrics->setAttribute('coveredstatements', '0');
201-
$xmlPackageMetrics->setAttribute('methods', '0');
202-
$xmlPackageMetrics->setAttribute('coveredmethods', '0');
203-
$packages[$namespace]->appendChild($xmlPackageMetrics);
204-
205-
$xmlProject->appendChild($packages[$namespace]);
206-
}
207-
208-
$xmlPackageMetrics = $packages[$namespace]->firstChild;
209-
210-
assert($xmlPackageMetrics instanceof DOMElement);
211-
212-
$xmlPackageMetrics->setAttribute('complexity', (string) ((int) $xmlPackageMetrics->getAttribute('complexity') + $item->cyclomaticComplexity()));
213-
$xmlPackageMetrics->setAttribute('elements', (string) ((int) $xmlPackageMetrics->getAttribute('elements') + $item->numberOfMethods() + $item->numberOfExecutableLines() + $item->numberOfExecutableBranches()));
214-
$xmlPackageMetrics->setAttribute('coveredelements', (string) ((int) $xmlPackageMetrics->getAttribute('coveredelements') + $item->numberOfTestedMethods() + $item->numberOfExecutedLines() + $item->numberOfExecutedBranches()));
215-
$xmlPackageMetrics->setAttribute('conditionals', (string) ((int) $xmlPackageMetrics->getAttribute('conditionals') + $item->numberOfExecutableBranches()));
216-
$xmlPackageMetrics->setAttribute('coveredconditionals', (string) ((int) $xmlPackageMetrics->getAttribute('coveredconditionals') + $item->numberOfExecutedBranches()));
217-
$xmlPackageMetrics->setAttribute('statements', (string) ((int) $xmlPackageMetrics->getAttribute('statements') + $item->numberOfExecutableLines()));
218-
$xmlPackageMetrics->setAttribute('coveredstatements', (string) ((int) $xmlPackageMetrics->getAttribute('coveredstatements') + $item->numberOfExecutedLines()));
219-
$xmlPackageMetrics->setAttribute('methods', (string) ((int) $xmlPackageMetrics->getAttribute('methods') + $item->numberOfMethods()));
220-
$xmlPackageMetrics->setAttribute('coveredmethods', (string) ((int) $xmlPackageMetrics->getAttribute('coveredmethods') + $item->numberOfTestedMethods()));
182+
$xmlMetrics->setAttribute('elements', (string) ($item->numberOfMethods() + $item->numberOfExecutableLines() + $item->numberOfExecutableBranches()));
183+
$xmlMetrics->setAttribute('coveredelements', (string) ($item->numberOfTestedMethods() + $item->numberOfExecutedLines() + $item->numberOfExecutedBranches()));
184+
$xmlFile->appendChild($xmlMetrics);
185+
186+
if ($namespace === 'global') {
187+
$xmlProject->appendChild($xmlFile);
188+
} else {
189+
if (!isset($packages[$namespace])) {
190+
$packages[$namespace] = $xmlDocument->createElement(
191+
'package',
192+
);
193+
194+
$packages[$namespace]->setAttribute('name', $namespace);
195+
$xmlProject->appendChild($packages[$namespace]);
196+
}
221197

222-
$packages[$namespace]->appendChild($xmlFile);
198+
$packages[$namespace]->appendChild($xmlFile);
199+
}
223200
}
224201

225202
$linesOfCode = $report->linesOfCode();
@@ -229,16 +206,15 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
229206
$xmlMetrics->setAttribute('loc', (string) $linesOfCode->linesOfCode());
230207
$xmlMetrics->setAttribute('ncloc', (string) $linesOfCode->nonCommentLinesOfCode());
231208
$xmlMetrics->setAttribute('classes', (string) $report->numberOfClassesAndTraits());
232-
$xmlMetrics->setAttribute('complexity', (string) $report->cyclomaticComplexity());
233-
$xmlMetrics->setAttribute('elements', (string) ($report->numberOfMethods() + $report->numberOfExecutableLines() + $report->numberOfExecutableBranches()));
234-
$xmlMetrics->setAttribute('coveredelements', (string) ($report->numberOfTestedMethods() + $report->numberOfExecutedLines() + $report->numberOfExecutedBranches()));
209+
$xmlMetrics->setAttribute('methods', (string) $report->numberOfMethods());
210+
$xmlMetrics->setAttribute('coveredmethods', (string) $report->numberOfTestedMethods());
235211
$xmlMetrics->setAttribute('conditionals', (string) $report->numberOfExecutableBranches());
236212
$xmlMetrics->setAttribute('coveredconditionals', (string) $report->numberOfExecutedBranches());
237213
$xmlMetrics->setAttribute('statements', (string) $report->numberOfExecutableLines());
238214
$xmlMetrics->setAttribute('coveredstatements', (string) $report->numberOfExecutedLines());
239-
$xmlMetrics->setAttribute('methods', (string) $report->numberOfMethods());
240-
$xmlMetrics->setAttribute('coveredmethods', (string) $report->numberOfTestedMethods());
241-
$xmlProject->insertBefore($xmlMetrics, $xmlProject->firstChild);
215+
$xmlMetrics->setAttribute('elements', (string) ($report->numberOfMethods() + $report->numberOfExecutableLines() + $report->numberOfExecutableBranches()));
216+
$xmlMetrics->setAttribute('coveredelements', (string) ($report->numberOfTestedMethods() + $report->numberOfExecutedLines() + $report->numberOfExecutedBranches()));
217+
$xmlProject->appendChild($xmlMetrics);
242218

243219
$buffer = $xmlDocument->saveXML();
244220

0 commit comments

Comments
 (0)