Skip to content

Commit c7e45f1

Browse files
committed
Faster coverage-xml report
1 parent 9838486 commit c7e45f1

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

src/Report/Xml/Coverage.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,25 @@
1818
*/
1919
final class Coverage
2020
{
21-
private readonly XMLWriter $writer;
2221
private readonly DOMElement $contextNode;
23-
private bool $finalized = false;
22+
private readonly string $line;
2423

2524
public function __construct(DOMElement $context, string $line)
2625
{
2726
$this->contextNode = $context;
28-
29-
$this->writer = new XMLWriter;
30-
$this->writer->openMemory();
31-
$this->writer->startElementNs(null, $context->nodeName, 'https://schema.phpunit.de/coverage/1.0');
32-
$this->writer->writeAttribute('nr', $line);
27+
$this->line = $line;
3328
}
3429

35-
/**
36-
* @throws ReportAlreadyFinalizedException
37-
*/
38-
public function addTest(string $test): void
30+
public function finalize(array $tests): void
3931
{
40-
if ($this->finalized) {
41-
// @codeCoverageIgnoreStart
42-
throw new ReportAlreadyFinalizedException;
43-
// @codeCoverageIgnoreEnd
32+
$xml = '<line nr="'. $this->line .'">';
33+
foreach ($tests as $test) {
34+
$xml .= '<covered by="'. htmlspecialchars($test, ENT_QUOTES | ENT_XML1) .'" />';
4435
}
45-
46-
$this->writer->startElement('covered');
47-
$this->writer->writeAttribute('by', $test);
48-
$this->writer->endElement();
49-
}
50-
51-
public function finalize(): void
52-
{
53-
$this->writer->endElement();
36+
$xml .= '</line>';
5437

5538
$fragment = $this->contextNode->ownerDocument->createDocumentFragment();
56-
$fragment->appendXML($this->writer->outputMemory());
39+
$fragment->appendXML($xml);
5740

5841
$this->contextNode->parentNode->replaceChild(
5942
$fragment,

src/Report/Xml/Facade.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,7 @@ private function processFile(FileNode $file, Directory $context): void
165165
}
166166

167167
$coverage = $fileReport->lineCoverage((string) $line);
168-
169-
foreach ($tests as $test) {
170-
$coverage->addTest($test);
171-
}
172-
173-
$coverage->finalize();
168+
$coverage->finalize($tests);
174169
}
175170

176171
$fileReport->source()->setSourceCode(

0 commit comments

Comments
 (0)