Skip to content

Commit 9bb29e1

Browse files
committed
fix
1 parent 90917ad commit 9bb29e1

File tree

4 files changed

+40
-56
lines changed

4 files changed

+40
-56
lines changed

src/Report/Xml/Facade.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ private function processDirectory(DirectoryNode $directory, Node $context): void
124124
$directoryName = '/';
125125
}
126126

127-
$directoryObject = $context->addDirectory($directoryName);
127+
$this->xmlWriter->startElement('directory');
128+
$this->xmlWriter->writeAttribute('name', $directoryName);
129+
$directoryObject = $context->addDirectory();
128130

129131
$this->setTotals($directory, $directoryObject->totals());
130132

@@ -135,17 +137,19 @@ private function processDirectory(DirectoryNode $directory, Node $context): void
135137
foreach ($directory->files() as $node) {
136138
$this->processFile($node, $directoryObject);
137139
}
140+
$this->xmlWriter->endElement();
138141
}
139142

140143
/**
141144
* @throws XmlException
142145
*/
143146
private function processFile(FileNode $file, Directory $context): void
144147
{
145-
$fileObject = $context->addFile(
146-
$file->name(),
147-
$file->id() . '.xml',
148-
);
148+
$this->xmlWriter->startElement('file');
149+
$this->xmlWriter->writeAttribute('name', $file->name());
150+
$this->xmlWriter->writeAttribute('href', $file->id() . '.xml');
151+
152+
$fileObject = $context->addFile();
149153

150154
$this->setTotals($file, $fileObject->totals());
151155

@@ -154,7 +158,7 @@ private function processFile(FileNode $file, Directory $context): void
154158
strlen($this->project->projectSourceDirectory()),
155159
);
156160

157-
$fileReport = new Report($path);
161+
$fileReport = new Report($this->xmlWriter, $path);
158162

159163
$this->setTotals($file, $fileReport->totals());
160164

@@ -177,16 +181,21 @@ private function processFile(FileNode $file, Directory $context): void
177181
}
178182
$this->xmlWriter->endElement();
179183

184+
$this->xmlWriter->endElement();
185+
180186
$fileReport->source()->setSourceCode(
181187
file_get_contents($file->pathAsString()),
182188
);
189+
$fileReport->finalize();
183190

184191
$this->saveDocument($fileReport->asDom(), $file->id());
185192
}
186193

187194
private function processUnit(ProcessedClassType|ProcessedTraitType $unit, Report $report): void
188195
{
189196
if ($unit instanceof ProcessedClassType) {
197+
$this->xmlWriter->startElement('class');
198+
190199
$unitObject = $report->classObject(
191200
$unit->className,
192201
$unit->namespace,
@@ -196,6 +205,8 @@ private function processUnit(ProcessedClassType|ProcessedTraitType $unit, Report
196205
(float) $unit->crap,
197206
);
198207
} else {
208+
$this->xmlWriter->startElement('trait');
209+
199210
$unitObject = $report->traitObject(
200211
$unit->traitName,
201212
$unit->namespace,
@@ -218,6 +229,8 @@ private function processUnit(ProcessedClassType|ProcessedTraitType $unit, Report
218229
$method->crap,
219230
);
220231
}
232+
233+
$this->xmlWriter->endElement();
221234
}
222235

223236
private function processFunction(ProcessedFunctionType $function, Report $report): void

src/Report/Xml/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class File
2222
{
23-
private XMLWriter $xmlWriter;
23+
protected XMLWriter $xmlWriter;
2424

2525
public function __construct(XMLWriter $xmlWriter)
2626
{

src/Report/Xml/Node.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,14 @@ public function totals(): Totals
3232
return new Totals($this->xmlWriter);
3333
}
3434

35-
public function addDirectory(string $name): Directory
35+
public function addDirectory(): Directory
3636
{
37-
$dirNode = $this->dom->createElementNS(
38-
Facade::XML_NAMESPACE,
39-
'directory',
40-
);
41-
42-
$dirNode->setAttribute('name', $name);
43-
$this->contextNode()->appendChild($dirNode);
44-
45-
return new Directory($dirNode);
37+
return new Directory($this->xmlWriter);
4638
}
4739

48-
public function addFile(string $name, string $href): File
40+
public function addFile(): File
4941
{
50-
$fileNode = $this->dom->createElementNS(
51-
Facade::XML_NAMESPACE,
52-
'file',
53-
);
54-
55-
$fileNode->setAttribute('name', $name);
56-
$fileNode->setAttribute('href', $href);
57-
$this->contextNode()->appendChild($fileNode);
58-
59-
return new File($fileNode);
42+
return new File($this->xmlWriter);
6043
}
6144

6245
protected function contextNode(): DOMElement

src/Report/Xml/Report.php

Lines changed: 16 additions & 28 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 function basename;
1415
use function dirname;
@@ -22,27 +23,32 @@ final class Report extends File
2223
{
2324
private readonly string $name;
2425

25-
public function __construct(string $name)
26+
public function __construct(XMLWriter $xmlWriter, string $name)
2627
{
28+
/*
2729
$dom = new DOMDocument;
2830
$dom->loadXML('<?xml version="1.0" ?><phpunit xmlns="https://schema.phpunit.de/coverage/1.0"><file /></phpunit>');
2931
3032
$contextNode = $dom->getElementsByTagNameNS(
3133
Facade::XML_NAMESPACE,
3234
'file',
3335
)->item(0);
34-
35-
parent::__construct($contextNode);
36+
*/
37+
parent::__construct($xmlWriter);
3638

3739
$this->name = $name;
40+
41+
$xmlWriter->startDocument('1.0', 'UTF-8');
42+
$xmlWriter->startElement('phpunit');
43+
$xmlWriter->startElement('file');
44+
$xmlWriter->writeAttribute('name', basename($this->name));
45+
$xmlWriter->writeAttribute('path', dirname($this->name));
3846
}
3947

40-
public function asDom(): DOMDocument
48+
public function finalize(): void
4149
{
42-
$this->contextNode()->setAttribute('name', basename($this->name));
43-
$this->contextNode()->setAttribute('path', dirname($this->name));
44-
45-
return $this->dom;
50+
$this->xmlWriter->endElement();
51+
$this->xmlWriter->endElement();
4652
}
4753

4854
public function functionObject(
@@ -85,16 +91,7 @@ public function classObject(
8591
int $executed,
8692
float $crap
8793
): Unit {
88-
$node = $this->contextNode()->appendChild(
89-
$this->dom->createElementNS(
90-
Facade::XML_NAMESPACE,
91-
'class',
92-
),
93-
);
94-
95-
assert($node instanceof DOMElement);
96-
97-
return new Unit($node, $name, $namespace, $start, $executable, $executed, $crap);
94+
return new Unit($this->xmlWriter, $name, $namespace, $start, $executable, $executed, $crap);
9895
}
9996

10097
public function traitObject(
@@ -105,16 +102,7 @@ public function traitObject(
105102
int $executed,
106103
float $crap
107104
): Unit {
108-
$node = $this->contextNode()->appendChild(
109-
$this->dom->createElementNS(
110-
Facade::XML_NAMESPACE,
111-
'trait',
112-
),
113-
);
114-
115-
assert($node instanceof DOMElement);
116-
117-
return new Unit($node, $name, $namespace, $start, $executable, $executed, $crap);
105+
return new Unit($this->xmlWriter, $name, $namespace, $start, $executable, $executed, $crap);
118106
}
119107

120108
public function source(): Source

0 commit comments

Comments
 (0)