|
9 | 9 | */ |
10 | 10 | namespace SebastianBergmann\CodeCoverage\Report\Xml; |
11 | 11 |
|
12 | | -use function assert; |
| 12 | +use XMLWriter; |
13 | 13 | use function phpversion; |
14 | 14 | use DateTimeImmutable; |
15 | | -use DOMElement; |
16 | 15 | use SebastianBergmann\Environment\Runtime; |
17 | 16 |
|
18 | 17 | /** |
19 | 18 | * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage |
20 | 19 | */ |
21 | 20 | final readonly class BuildInformation |
22 | 21 | { |
23 | | - private DOMElement $contextNode; |
24 | | - |
25 | | - public function __construct(DOMElement $contextNode) |
26 | | - { |
27 | | - $this->contextNode = $contextNode; |
28 | | - } |
| 22 | + private Runtime $runtime; |
| 23 | + private DateTimeImmutable $buildTime; |
| 24 | + private string $phpunitVersion; |
| 25 | + private string $coverageVersion; |
29 | 26 |
|
30 | 27 | public function setRuntimeInformation(Runtime $runtime): void |
31 | 28 | { |
32 | | - $runtimeNode = $this->nodeByName('runtime'); |
33 | | - |
34 | | - $runtimeNode->setAttribute('name', $runtime->getName()); |
35 | | - $runtimeNode->setAttribute('version', $runtime->getVersion()); |
36 | | - $runtimeNode->setAttribute('url', $runtime->getVendorUrl()); |
37 | | - |
38 | | - $driverNode = $this->nodeByName('driver'); |
39 | | - |
40 | | - if ($runtime->hasXdebug()) { |
41 | | - $driverNode->setAttribute('name', 'xdebug'); |
42 | | - $driverNode->setAttribute('version', phpversion('xdebug')); |
43 | | - } |
44 | | - |
45 | | - if ($runtime->hasPCOV()) { |
46 | | - $driverNode->setAttribute('name', 'pcov'); |
47 | | - $driverNode->setAttribute('version', phpversion('pcov')); |
48 | | - } |
| 29 | + $this->runtime = $runtime; |
49 | 30 | } |
50 | 31 |
|
51 | 32 | public function setBuildTime(DateTimeImmutable $date): void |
52 | 33 | { |
53 | | - $this->contextNode->setAttribute('time', $date->format('D M j G:i:s T Y')); |
| 34 | + $this->buildTime = $date; |
54 | 35 | } |
55 | 36 |
|
56 | 37 | public function setGeneratorVersions(string $phpUnitVersion, string $coverageVersion): void |
57 | 38 | { |
58 | | - $this->contextNode->setAttribute('phpunit', $phpUnitVersion); |
59 | | - $this->contextNode->setAttribute('coverage', $coverageVersion); |
| 39 | + $this->phpunitVersion = $phpUnitVersion; |
| 40 | + $this->coverageVersion = $coverageVersion; |
60 | 41 | } |
61 | 42 |
|
62 | | - private function nodeByName(string $name): DOMElement |
| 43 | + public function write(XMLWriter $writer): void |
63 | 44 | { |
64 | | - $node = $this->contextNode->getElementsByTagNameNS( |
65 | | - Facade::XML_NAMESPACE, |
66 | | - $name, |
67 | | - )->item(0); |
| 45 | + $writer->startElement('build'); |
| 46 | + $writer->writeAttribute('time', $this->buildTime->format('D M j G:i:s T Y')); |
| 47 | + $writer->writeAttribute('phpunit', $this->phpunitVersion); |
| 48 | + $writer->writeAttribute('coverage', $this->coverageVersion); |
68 | 49 |
|
69 | | - if ($node === null) { |
70 | | - $node = $this->contextNode->appendChild( |
71 | | - $this->contextNode->ownerDocument->createElementNS( |
72 | | - Facade::XML_NAMESPACE, |
73 | | - $name, |
74 | | - ), |
75 | | - ); |
76 | | - } |
| 50 | + $writer->startElement('runtime'); |
| 51 | + $writer->writeAttribute('name', $this->runtime->getName()); |
| 52 | + $writer->writeAttribute('version', $this->runtime->getVersion()); |
| 53 | + $writer->writeAttribute('url', $this->runtime->getVendorUrl()); |
| 54 | + $writer->endElement(); |
77 | 55 |
|
78 | | - assert($node instanceof DOMElement); |
| 56 | + $writer->startElement('driver'); |
| 57 | + if ($this->runtime->hasXdebug()) { |
| 58 | + $writer->writeAttribute('name', 'xdebug'); |
| 59 | + $writer->writeAttribute('version', phpversion('xdebug')); |
| 60 | + } |
| 61 | + if ($this->runtime->hasPCOV()) { |
| 62 | + $writer->writeAttribute('name', 'pcov'); |
| 63 | + $writer->writeAttribute('version', phpversion('pcov')); |
| 64 | + } |
| 65 | + $writer->endElement(); |
79 | 66 |
|
80 | | - return $node; |
| 67 | + $writer->endElement(); |
81 | 68 | } |
| 69 | + |
82 | 70 | } |
0 commit comments