Skip to content

Commit 15a38ac

Browse files
committed
fix
1 parent f2d9bd3 commit 15a38ac

File tree

5 files changed

+51
-101
lines changed

5 files changed

+51
-101
lines changed

src/Report/Xml/Coverage.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@
1717
*/
1818
final class Coverage
1919
{
20-
private readonly DOMElement $contextNode;
20+
private readonly XMLWriter $xmlWriter;
2121
private readonly string $line;
2222

23-
public function __construct(DOMElement $context, string $line)
23+
public function __construct(
24+
XMLWriter $xmlWriter,
25+
string $line
26+
)
2427
{
25-
$this->contextNode = $context;
28+
$this->xmlWriter = $xmlWriter;
2629
$this->line = $line;
2730
}
2831

2932
public function finalize(array $tests): void
3033
{
31-
$writer = new XMLWriter;
32-
$writer->openMemory();
33-
$writer->startElementNs(null, $this->contextNode->nodeName, Facade::XML_NAMESPACE);
34+
$writer = $this->xmlWriter;
35+
$writer->startElement('line');
3436
$writer->writeAttribute('nr', $this->line);
3537

3638
foreach ($tests as $test) {
@@ -39,13 +41,5 @@ public function finalize(array $tests): void
3941
$writer->endElement();
4042
}
4143
$writer->endElement();
42-
43-
$fragment = $this->contextNode->ownerDocument->createDocumentFragment();
44-
$fragment->appendXML($writer->outputMemory());
45-
46-
$this->contextNode->parentNode->replaceChild(
47-
$fragment,
48-
$this->contextNode,
49-
);
5044
}
5145
}

src/Report/Xml/Facade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ private function processTests(array $tests): void
250250

251251
private function setTotals(AbstractNode $node, Totals $totals): void
252252
{
253+
$this->xmlWriter->startElement('totals');
254+
253255
$loc = $node->linesOfCode();
254256

255257
$totals->setNumLines(
@@ -279,6 +281,8 @@ private function setTotals(AbstractNode $node, Totals $totals): void
279281
$node->numberOfFunctions(),
280282
$node->numberOfTestedFunctions(),
281283
);
284+
285+
$this->xmlWriter->endElement();
282286
}
283287

284288
private function targetDirectory(): string

src/Report/Xml/File.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report\Xml;
1111

12+
use XMLWriter;
1213
use function assert;
1314
use DOMDocument;
1415
use DOMElement;
@@ -19,28 +20,16 @@
1920
*/
2021
class File
2122
{
22-
protected readonly DOMDocument $dom;
23-
private readonly DOMElement $contextNode;
24-
private ?DOMNode $lineCoverage = null;
23+
private XMLWriter $xmlWriter;
2524

26-
public function __construct(DOMElement $context)
25+
public function __construct(XMLWriter $xmlWriter)
2726
{
28-
$this->dom = $context->ownerDocument;
29-
$this->contextNode = $context;
27+
$this->xmlWriter = $xmlWriter;
3028
}
3129

3230
public function totals(): Totals
3331
{
34-
$totalsContainer = $this->contextNode->appendChild(
35-
$this->dom->createElementNS(
36-
Facade::XML_NAMESPACE,
37-
'totals',
38-
),
39-
);
40-
41-
assert($totalsContainer instanceof DOMElement);
42-
43-
return new Totals($totalsContainer);
32+
return new Totals($this->xmlWriter);
4433
}
4534

4635
public function lineCoverage(string $line): Coverage
@@ -64,7 +53,7 @@ public function lineCoverage(string $line): Coverage
6453

6554
assert($lineNode instanceof DOMElement);
6655

67-
return new Coverage($lineNode, $line);
56+
return new Coverage($this->xmlWriter, $line);
6857
}
6958

7059
protected function contextNode(): DOMElement

src/Report/Xml/Node.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,7 @@ public function __construct(XMLWriter $xmlWriter)
2929

3030
public function totals(): Totals
3131
{
32-
$totalsContainer = $this->contextNode()->firstChild;
33-
34-
if ($totalsContainer === null) {
35-
$totalsContainer = $this->contextNode()->appendChild(
36-
$this->dom->createElementNS(
37-
Facade::XML_NAMESPACE,
38-
'totals',
39-
),
40-
);
41-
}
42-
43-
assert($totalsContainer instanceof DOMElement);
44-
45-
return new Totals($totalsContainer);
32+
return new Totals($this->xmlWriter);
4633
}
4734

4835
public function addDirectory(string $name): Directory

src/Report/Xml/Totals.php

Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report\Xml;
1111

12+
use XMLWriter;
1213
use function sprintf;
1314
use DOMElement;
1415
use SebastianBergmann\CodeCoverage\Util\Percentage;
@@ -18,98 +19,73 @@
1819
*/
1920
final readonly class Totals
2021
{
21-
private DOMElement $linesNode;
22-
private DOMElement $methodsNode;
23-
private DOMElement $functionsNode;
24-
private DOMElement $classesNode;
25-
private DOMElement $traitsNode;
22+
private XMLWriter $xmlWriter;
2623

27-
public function __construct(DOMElement $container)
24+
public function __construct(XMLWriter $xmlWriter)
2825
{
29-
$dom = $container->ownerDocument;
30-
31-
$this->linesNode = $dom->createElementNS(
32-
Facade::XML_NAMESPACE,
33-
'lines',
34-
);
35-
36-
$this->methodsNode = $dom->createElementNS(
37-
Facade::XML_NAMESPACE,
38-
'methods',
39-
);
40-
41-
$this->functionsNode = $dom->createElementNS(
42-
Facade::XML_NAMESPACE,
43-
'functions',
44-
);
45-
46-
$this->classesNode = $dom->createElementNS(
47-
Facade::XML_NAMESPACE,
48-
'classes',
49-
);
50-
51-
$this->traitsNode = $dom->createElementNS(
52-
Facade::XML_NAMESPACE,
53-
'traits',
54-
);
55-
56-
$container->appendChild($this->linesNode);
57-
$container->appendChild($this->methodsNode);
58-
$container->appendChild($this->functionsNode);
59-
$container->appendChild($this->classesNode);
60-
$container->appendChild($this->traitsNode);
26+
$this->xmlWriter = $xmlWriter;
6127
}
6228

6329
public function setNumLines(int $loc, int $cloc, int $ncloc, int $executable, int $executed): void
6430
{
65-
$this->linesNode->setAttribute('total', (string) $loc);
66-
$this->linesNode->setAttribute('comments', (string) $cloc);
67-
$this->linesNode->setAttribute('code', (string) $ncloc);
68-
$this->linesNode->setAttribute('executable', (string) $executable);
69-
$this->linesNode->setAttribute('executed', (string) $executed);
70-
$this->linesNode->setAttribute(
31+
$this->xmlWriter->startElement('lines');
32+
$this->xmlWriter->writeAttribute('total', (string) $loc);
33+
$this->xmlWriter->writeAttribute('comments', (string) $cloc);
34+
$this->xmlWriter->writeAttribute('code', (string) $ncloc);
35+
$this->xmlWriter->writeAttribute('executable', (string) $executable);
36+
$this->xmlWriter->writeAttribute('executed', (string) $executed);
37+
$this->xmlWriter->writeAttribute(
7138
'percent',
7239
$executable === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($executed, $executable)->asFloat()),
7340
);
41+
$this->xmlWriter->endElement();
7442
}
7543

7644
public function setNumClasses(int $count, int $tested): void
7745
{
78-
$this->classesNode->setAttribute('count', (string) $count);
79-
$this->classesNode->setAttribute('tested', (string) $tested);
80-
$this->classesNode->setAttribute(
46+
$this->xmlWriter->startElement('classes');
47+
$this->xmlWriter->writeAttribute('count', (string) $count);
48+
$this->xmlWriter->writeAttribute('tested', (string) $tested);
49+
$this->xmlWriter->writeAttribute(
8150
'percent',
8251
$count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()),
8352
);
53+
$this->xmlWriter->endElement();
8454
}
8555

8656
public function setNumTraits(int $count, int $tested): void
8757
{
88-
$this->traitsNode->setAttribute('count', (string) $count);
89-
$this->traitsNode->setAttribute('tested', (string) $tested);
90-
$this->traitsNode->setAttribute(
58+
$this->xmlWriter->startElement('traits');
59+
$this->xmlWriter->writeAttribute('count', (string) $count);
60+
$this->xmlWriter->writeAttribute('tested', (string) $tested);
61+
$this->xmlWriter->writeAttribute(
9162
'percent',
9263
$count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()),
9364
);
65+
$this->xmlWriter->endElement();
9466
}
9567

9668
public function setNumMethods(int $count, int $tested): void
9769
{
98-
$this->methodsNode->setAttribute('count', (string) $count);
99-
$this->methodsNode->setAttribute('tested', (string) $tested);
100-
$this->methodsNode->setAttribute(
70+
$this->xmlWriter->startElement('methods');
71+
$this->xmlWriter->writeAttribute('count', (string) $count);
72+
$this->xmlWriter->writeAttribute('tested', (string) $tested);
73+
$this->xmlWriter->writeAttribute(
10174
'percent',
10275
$count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()),
10376
);
77+
$this->xmlWriter->endElement();
10478
}
10579

10680
public function setNumFunctions(int $count, int $tested): void
10781
{
108-
$this->functionsNode->setAttribute('count', (string) $count);
109-
$this->functionsNode->setAttribute('tested', (string) $tested);
110-
$this->functionsNode->setAttribute(
82+
$this->xmlWriter->startElement('functions');
83+
$this->xmlWriter->writeAttribute('count', (string) $count);
84+
$this->xmlWriter->writeAttribute('tested', (string) $tested);
85+
$this->xmlWriter->writeAttribute(
11186
'percent',
11287
$count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()),
11388
);
89+
$this->xmlWriter->endElement();
11490
}
11591
}

0 commit comments

Comments
 (0)