|
9 | 9 | */ |
10 | 10 | namespace SebastianBergmann\CodeCoverage\Report\Xml; |
11 | 11 |
|
| 12 | +use XMLWriter; |
12 | 13 | use function sprintf; |
13 | 14 | use DOMElement; |
14 | 15 | use SebastianBergmann\CodeCoverage\Util\Percentage; |
|
18 | 19 | */ |
19 | 20 | final readonly class Totals |
20 | 21 | { |
21 | | - private DOMElement $linesNode; |
22 | | - private DOMElement $methodsNode; |
23 | | - private DOMElement $functionsNode; |
24 | | - private DOMElement $classesNode; |
25 | | - private DOMElement $traitsNode; |
| 22 | + private XMLWriter $xmlWriter; |
26 | 23 |
|
27 | | - public function __construct(DOMElement $container) |
| 24 | + public function __construct(XMLWriter $xmlWriter) |
28 | 25 | { |
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; |
61 | 27 | } |
62 | 28 |
|
63 | 29 | public function setNumLines(int $loc, int $cloc, int $ncloc, int $executable, int $executed): void |
64 | 30 | { |
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( |
71 | 38 | 'percent', |
72 | 39 | $executable === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($executed, $executable)->asFloat()), |
73 | 40 | ); |
| 41 | + $this->xmlWriter->endElement(); |
74 | 42 | } |
75 | 43 |
|
76 | 44 | public function setNumClasses(int $count, int $tested): void |
77 | 45 | { |
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( |
81 | 50 | 'percent', |
82 | 51 | $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()), |
83 | 52 | ); |
| 53 | + $this->xmlWriter->endElement(); |
84 | 54 | } |
85 | 55 |
|
86 | 56 | public function setNumTraits(int $count, int $tested): void |
87 | 57 | { |
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( |
91 | 62 | 'percent', |
92 | 63 | $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()), |
93 | 64 | ); |
| 65 | + $this->xmlWriter->endElement(); |
94 | 66 | } |
95 | 67 |
|
96 | 68 | public function setNumMethods(int $count, int $tested): void |
97 | 69 | { |
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( |
101 | 74 | 'percent', |
102 | 75 | $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()), |
103 | 76 | ); |
| 77 | + $this->xmlWriter->endElement(); |
104 | 78 | } |
105 | 79 |
|
106 | 80 | public function setNumFunctions(int $count, int $tested): void |
107 | 81 | { |
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( |
111 | 86 | 'percent', |
112 | 87 | $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()), |
113 | 88 | ); |
| 89 | + $this->xmlWriter->endElement(); |
114 | 90 | } |
115 | 91 | } |
0 commit comments