1212use const DIRECTORY_SEPARATOR ;
1313use function basename ;
1414use function count ;
15- use function dirname ;
16- use function file_put_contents ;
1715use function preg_match ;
1816use function range ;
19- use function str_contains ;
2017use function str_replace ;
2118use function time ;
2219use DOMImplementation ;
2320use SebastianBergmann \CodeCoverage \CodeCoverage ;
2421use SebastianBergmann \CodeCoverage \Driver \WriteOperationFailedException ;
2522use SebastianBergmann \CodeCoverage \Node \File ;
2623use SebastianBergmann \CodeCoverage \Util \Filesystem ;
24+ use SebastianBergmann \CodeCoverage \Util \Xml ;
2725
2826final class Cobertura
2927{
3028 /**
29+ * @param null|non-empty-string $target
30+ *
3131 * @throws WriteOperationFailedException
3232 */
3333 public function process (CodeCoverage $ coverage , ?string $ target = null ): string
@@ -44,10 +44,9 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
4444 'http://cobertura.sourceforge.net/xml/coverage-04.dtd ' ,
4545 );
4646
47- $ document = $ implementation ->createDocument ('' , '' , $ documentType );
48- $ document ->xmlVersion = '1.0 ' ;
49- $ document ->encoding = 'UTF-8 ' ;
50- $ document ->formatOutput = true ;
47+ $ document = $ implementation ->createDocument ('' , '' , $ documentType );
48+ $ document ->xmlVersion = '1.0 ' ;
49+ $ document ->encoding = 'UTF-8 ' ;
5150
5251 $ coverageElement = $ document ->createElement ('coverage ' );
5352
@@ -289,16 +288,10 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
289288
290289 $ coverageElement ->setAttribute ('complexity ' , (string ) $ complexity );
291290
292- $ buffer = $ document-> saveXML ( );
291+ $ buffer = Xml:: asString ( $ document );
293292
294293 if ($ target !== null ) {
295- if (!str_contains ($ target , ':// ' )) {
296- Filesystem::createDirectory (dirname ($ target ));
297- }
298-
299- if (@file_put_contents ($ target , $ buffer ) === false ) {
300- throw new WriteOperationFailedException ($ target );
301- }
294+ Filesystem::write ($ target , $ buffer );
302295 }
303296
304297 return $ buffer ;
0 commit comments