Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/Report/Xml/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function assert;
use DOMDocument;
use DOMElement;
use DOMNode;

/**
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
Expand All @@ -20,6 +21,7 @@ class File
{
private readonly DOMDocument $dom;
private readonly DOMElement $contextNode;
private ?DOMNode $lineCoverage = null;

public function __construct(DOMElement $context)
{
Expand All @@ -29,16 +31,12 @@ public function __construct(DOMElement $context)

public function totals(): Totals
{
$totalsContainer = $this->contextNode->firstChild;

if ($totalsContainer === null) {
$totalsContainer = $this->contextNode->appendChild(
$this->dom->createElementNS(
Facade::XML_NAMESPACE,
'totals',
),
);
}
$totalsContainer = $this->contextNode->appendChild(
$this->dom->createElementNS(
Facade::XML_NAMESPACE,
'totals',
),
);

assert($totalsContainer instanceof DOMElement);

Expand All @@ -47,21 +45,17 @@ public function totals(): Totals

public function lineCoverage(string $line): Coverage
{
$coverage = $this->contextNode->getElementsByTagNameNS(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this removes one DOM call per line of code beeing analyzed. I expect this to improve performance.. will measure it

Copy link
Contributor Author

@staabm staabm Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its 1-2 seconds faster. previously 21-22 seconds now ~19-20

Facade::XML_NAMESPACE,
'coverage',
)->item(0);

if ($coverage === null) {
$coverage = $this->contextNode->appendChild(
if ($this->lineCoverage === null) {
$this->lineCoverage = $this->contextNode->appendChild(
$this->dom->createElementNS(
Facade::XML_NAMESPACE,
'coverage',
),
);
}
assert($this->lineCoverage instanceof DOMElement);

$lineNode = $coverage->appendChild(
$lineNode = $this->lineCoverage->appendChild(
$this->dom->createElementNS(
Facade::XML_NAMESPACE,
'line',
Expand Down