Skip to content

Commit 75f911e

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

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

src/Report/Xml/Coverage.php

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,42 @@
1010
namespace SebastianBergmann\CodeCoverage\Report\Xml;
1111

1212
use DOMElement;
13-
use SebastianBergmann\CodeCoverage\ReportAlreadyFinalizedException;
1413
use XMLWriter;
1514

1615
/**
1716
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
1817
*/
1918
final class Coverage
2019
{
21-
private readonly XMLWriter $writer;
2220
private readonly DOMElement $contextNode;
23-
private bool $finalized = false;
21+
private readonly string $line;
2422

2523
public function __construct(DOMElement $context, string $line)
2624
{
2725
$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);
26+
$this->line = $line;
3327
}
3428

35-
/**
36-
* @throws ReportAlreadyFinalizedException
37-
*/
38-
public function addTest(string $test): void
29+
public function finalize(array $tests): void
3930
{
40-
if ($this->finalized) {
41-
// @codeCoverageIgnoreStart
42-
throw new ReportAlreadyFinalizedException;
43-
// @codeCoverageIgnoreEnd
31+
$writer = new XMLWriter;
32+
$writer->openMemory();
33+
$writer->startElementNs(null, $this->contextNode->nodeName, 'https://schema.phpunit.de/coverage/1.0');
34+
$writer->writeAttribute('nr', $this->line);
35+
36+
foreach ($tests as $test) {
37+
$writer->startElement('covered');
38+
$writer->writeAttribute('by', $test);
39+
$writer->endElement();
4440
}
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();
41+
$writer->endElement();
5442

5543
$fragment = $this->contextNode->ownerDocument->createDocumentFragment();
56-
$fragment->appendXML($this->writer->outputMemory());
44+
$fragment->appendXML($writer->outputMemory());
5745

5846
$this->contextNode->parentNode->replaceChild(
5947
$fragment,
6048
$this->contextNode,
6149
);
62-
63-
$this->finalized = true;
6450
}
6551
}

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)