From 4ac7a8a05c111f83a57d73f108cdedd576056e0b Mon Sep 17 00:00:00 2001 From: Markus Staab <47448731+clxmstaab@users.noreply.github.com> Date: Thu, 20 Nov 2025 10:31:47 +0100 Subject: [PATCH 1/2] BuildInformation: Move always available information into __construct() Reducing the api surface along the way --- src/Report/Xml/BuildInformation.php | 20 ++++++++------------ src/Report/Xml/Facade.php | 10 ++++++---- src/Report/Xml/Project.php | 18 +++++++++++++++--- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Report/Xml/BuildInformation.php b/src/Report/Xml/BuildInformation.php index c91e5989e..19d0aa42d 100644 --- a/src/Report/Xml/BuildInformation.php +++ b/src/Report/Xml/BuildInformation.php @@ -22,13 +22,15 @@ { private DOMElement $contextNode; - public function __construct(DOMElement $contextNode) - { + public function __construct( + DOMElement $contextNode, + Runtime $runtime, + DateTimeImmutable $buildDate, + string $phpUnitVersion, + string $coverageVersion + ) { $this->contextNode = $contextNode; - } - public function setRuntimeInformation(Runtime $runtime): void - { $runtimeNode = $this->nodeByName('runtime'); $runtimeNode->setAttribute('name', $runtime->getName()); @@ -46,15 +48,9 @@ public function setRuntimeInformation(Runtime $runtime): void $driverNode->setAttribute('name', 'pcov'); $driverNode->setAttribute('version', phpversion('pcov')); } - } - public function setBuildTime(DateTimeImmutable $date): void - { - $this->contextNode->setAttribute('time', $date->format('D M j G:i:s T Y')); - } + $this->contextNode->setAttribute('time', $buildDate->format('D M j G:i:s T Y')); - public function setGeneratorVersions(string $phpUnitVersion, string $coverageVersion): void - { $this->contextNode->setAttribute('phpunit', $phpUnitVersion); $this->contextNode->setAttribute('coverage', $coverageVersion); } diff --git a/src/Report/Xml/Facade.php b/src/Report/Xml/Facade.php index 1bc1e009d..857423922 100644 --- a/src/Report/Xml/Facade.php +++ b/src/Report/Xml/Facade.php @@ -79,10 +79,12 @@ public function process(CodeCoverage $coverage, string $target): void private function setBuildInformation(): void { - $buildNode = $this->project->buildInformation(); - $buildNode->setRuntimeInformation(new Runtime); - $buildNode->setBuildTime(new DateTimeImmutable); - $buildNode->setGeneratorVersions($this->phpUnitVersion, Version::id()); + $this->project->buildInformation( + new Runtime, + new DateTimeImmutable, + $this->phpUnitVersion, + Version::id(), + ); } /** diff --git a/src/Report/Xml/Project.php b/src/Report/Xml/Project.php index 160170320..6980a943a 100644 --- a/src/Report/Xml/Project.php +++ b/src/Report/Xml/Project.php @@ -10,8 +10,10 @@ namespace SebastianBergmann\CodeCoverage\Report\Xml; use function assert; +use DateTimeImmutable; use DOMDocument; use DOMElement; +use SebastianBergmann\Environment\Runtime; /** * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage @@ -40,8 +42,12 @@ public function projectSourceDirectory(): string return $this->directory; } - public function buildInformation(): BuildInformation - { + public function buildInformation( + Runtime $runtime, + DateTimeImmutable $buildDate, + string $phpUnitVersion, + string $coverageVersion + ): void { $buildNode = $this->dom()->getElementsByTagNameNS( Facade::XML_NAMESPACE, 'build', @@ -58,7 +64,13 @@ public function buildInformation(): BuildInformation assert($buildNode instanceof DOMElement); - return new BuildInformation($buildNode); + new BuildInformation( + $buildNode, + $runtime, + $buildDate, + $phpUnitVersion, + $coverageVersion, + ); } public function tests(): Tests From 6a5a7ed5bf10c4871a3427f40817debdf67cfcc4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 20 Nov 2025 10:35:23 +0100 Subject: [PATCH 2/2] Remove unnused DOM re-attach path --- src/Report/Xml/BuildInformation.php | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/Report/Xml/BuildInformation.php b/src/Report/Xml/BuildInformation.php index 19d0aa42d..654eecb31 100644 --- a/src/Report/Xml/BuildInformation.php +++ b/src/Report/Xml/BuildInformation.php @@ -57,19 +57,12 @@ public function __construct( private function nodeByName(string $name): DOMElement { - $node = $this->contextNode->getElementsByTagNameNS( - Facade::XML_NAMESPACE, - $name, - )->item(0); - - if ($node === null) { - $node = $this->contextNode->appendChild( - $this->contextNode->ownerDocument->createElementNS( - Facade::XML_NAMESPACE, - $name, - ), - ); - } + $node = $this->contextNode->appendChild( + $this->contextNode->ownerDocument->createElementNS( + Facade::XML_NAMESPACE, + $name, + ), + ); assert($node instanceof DOMElement);