1010namespace SebastianBergmann \CodeCoverage \Report \Xml ;
1111
1212use const DIRECTORY_SEPARATOR ;
13+ use const PHP_EOL ;
1314use function count ;
1415use function dirname ;
1516use function file_exists ;
@@ -62,6 +63,9 @@ public function __construct(string $version)
6263 $ this ->phpUnitVersion = $ version ;
6364 }
6465
66+ /**
67+ * @throws XmlException
68+ */
6569 public function process (CodeCoverage $ coverage , string $ target ): void
6670 {
6771 if (substr ($ target , -1 , 1 ) !== DIRECTORY_SEPARATOR ) {
@@ -111,6 +115,9 @@ private function initTargetDirectory(string $directory): void
111115 DirectoryUtil::create ($ directory );
112116 }
113117
118+ /**
119+ * @throws XmlException
120+ */
114121 private function processDirectory (DirectoryNode $ directory , Node $ context ): void
115122 {
116123 $ directoryName = $ directory ->name ();
@@ -132,6 +139,9 @@ private function processDirectory(DirectoryNode $directory, Node $context): void
132139 }
133140 }
134141
142+ /**
143+ * @throws XmlException
144+ */
135145 private function processFile (FileNode $ file , Directory $ context ): void
136146 {
137147 $ fileObject = $ context ->addFile (
@@ -266,6 +276,9 @@ private function targetDirectory(): string
266276 return $ this ->target ;
267277 }
268278
279+ /**
280+ * @throws XmlException
281+ */
269282 private function saveDocument (DOMDocument $ document , string $ name ): void
270283 {
271284 $ filename = sprintf ('%s/%s.xml ' , $ this ->targetDirectory (), $ name );
@@ -274,24 +287,32 @@ private function saveDocument(DOMDocument $document, string $name): void
274287 $ document ->preserveWhiteSpace = false ;
275288 $ this ->initTargetDirectory (dirname ($ filename ));
276289
277- $ original = libxml_use_internal_errors (true );
290+ file_put_contents ($ filename , $ this ->documentAsString ($ document ));
291+ }
278292
279- /* @see https://bugs.php.net/bug.php?id=79191 */
280- $ xml = $ document ->saveXML ();
293+ /**
294+ * @throws XmlException
295+ *
296+ * @see https://bugs.php.net/bug.php?id=79191
297+ */
298+ private function documentAsString (DOMDocument $ document ): string
299+ {
300+ $ xmlErrorHandling = libxml_use_internal_errors (true );
301+ $ xml = $ document ->saveXML ();
281302
282303 if ($ xml === false ) {
283304 $ message = 'Unable to generate the XML ' ;
284305
285306 foreach (libxml_get_errors () as $ error ) {
286- $ message .= "\n" . $ error ->message ;
307+ $ message .= PHP_EOL . $ error ->message ;
287308 }
288309
289310 throw new XmlException ($ message );
290311 }
291312
292313 libxml_clear_errors ();
293- libxml_use_internal_errors ($ original );
314+ libxml_use_internal_errors ($ xmlErrorHandling );
294315
295- file_put_contents ( $ filename , $ xml) ;
316+ return $ xml ;
296317 }
297318}
0 commit comments