12
12
use const DIRECTORY_SEPARATOR ;
13
13
use function basename ;
14
14
use function count ;
15
- use function dirname ;
16
- use function file_put_contents ;
17
15
use function preg_match ;
18
16
use function range ;
19
- use function str_contains ;
20
17
use function str_replace ;
21
18
use function time ;
22
19
use DOMImplementation ;
23
20
use SebastianBergmann \CodeCoverage \CodeCoverage ;
24
21
use SebastianBergmann \CodeCoverage \Driver \WriteOperationFailedException ;
25
22
use SebastianBergmann \CodeCoverage \Node \File ;
26
23
use SebastianBergmann \CodeCoverage \Util \Filesystem ;
24
+ use SebastianBergmann \CodeCoverage \Util \Xml ;
27
25
28
26
final class Cobertura
29
27
{
30
28
/**
29
+ * @param null|non-empty-string $target
30
+ *
31
31
* @throws WriteOperationFailedException
32
32
*/
33
33
public function process (CodeCoverage $ coverage , ?string $ target = null ): string
@@ -44,10 +44,9 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
44
44
'http://cobertura.sourceforge.net/xml/coverage-04.dtd ' ,
45
45
);
46
46
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 ' ;
51
50
52
51
$ coverageElement = $ document ->createElement ('coverage ' );
53
52
@@ -289,16 +288,10 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
289
288
290
289
$ coverageElement ->setAttribute ('complexity ' , (string ) $ complexity );
291
290
292
- $ buffer = $ document-> saveXML ( );
291
+ $ buffer = Xml:: asString ( $ document );
293
292
294
293
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 );
302
295
}
303
296
304
297
return $ buffer ;
0 commit comments