• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

keradus / PHP-CS-Fixer / 17279562118

27 Aug 2025 09:47PM UTC coverage: 94.693%. Remained the same
17279562118

push

github

keradus
CS

28316 of 29903 relevant lines covered (94.69%)

45.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/Console/Report/FixReport/XmlReporter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz RumiƄski <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Console\Report\FixReport;
16

17
use PhpCsFixer\Console\Application;
18
use Symfony\Component\Console\Formatter\OutputFormatter;
19

20
/**
21
 * @author Boris Gorbylev <ekho@ekho.name>
22
 *
23
 * @readonly
24
 *
25
 * @internal
26
 *
27
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
28
 */
29
final class XmlReporter implements ReporterInterface
30
{
31
    public function getFormat(): string
32
    {
33
        return 'xml';
1✔
34
    }
35

36
    public function generate(ReportSummary $reportSummary): string
37
    {
38
        if (!\extension_loaded('dom')) {
6✔
39
            throw new \RuntimeException('Cannot generate report! `ext-dom` is not available!');
×
40
        }
41

42
        $dom = new \DOMDocument('1.0', 'UTF-8');
6✔
43
        // new nodes should be added to this or existing children
44
        $root = $dom->createElement('report');
6✔
45
        $dom->appendChild($root);
6✔
46

47
        $root->appendChild($this->createAboutElement($dom, Application::getAbout()));
6✔
48

49
        $filesXML = $dom->createElement('files');
6✔
50
        $root->appendChild($filesXML);
6✔
51

52
        $i = 1;
6✔
53
        foreach ($reportSummary->getChanged() as $file => $fixResult) {
6✔
54
            $fileXML = $dom->createElement('file');
5✔
55
            $fileXML->setAttribute('id', (string) $i++);
5✔
56
            $fileXML->setAttribute('name', $file);
5✔
57
            $filesXML->appendChild($fileXML);
5✔
58

59
            if ($reportSummary->shouldAddAppliedFixers()) {
5✔
60
                $fileXML->appendChild(
2✔
61
                    $this->createAppliedFixersElement($dom, $fixResult['appliedFixers']),
2✔
62
                );
2✔
63
            }
64

65
            if ('' !== $fixResult['diff']) {
5✔
66
                $fileXML->appendChild($this->createDiffElement($dom, $fixResult['diff']));
4✔
67
            }
68
        }
69

70
        if (0 !== $reportSummary->getTime()) {
6✔
71
            $root->appendChild($this->createTimeElement($reportSummary->getTime(), $dom));
2✔
72
        }
73

74
        if (0 !== $reportSummary->getMemory()) {
6✔
75
            $root->appendChild($this->createMemoryElement($reportSummary->getMemory(), $dom));
2✔
76
        }
77

78
        $dom->formatOutput = true;
6✔
79

80
        $result = $dom->saveXML();
6✔
81
        if (false === $result) {
6✔
82
            throw new \RuntimeException('Failed to generate XML output');
×
83
        }
84

85
        return $reportSummary->isDecoratedOutput() ? OutputFormatter::escape($result) : $result;
6✔
86
    }
87

88
    /**
89
     * @param list<string> $appliedFixers
90
     */
91
    private function createAppliedFixersElement(\DOMDocument $dom, array $appliedFixers): \DOMElement
92
    {
93
        $appliedFixersXML = $dom->createElement('applied_fixers');
2✔
94

95
        foreach ($appliedFixers as $appliedFixer) {
2✔
96
            $appliedFixerXML = $dom->createElement('applied_fixer');
2✔
97
            $appliedFixerXML->setAttribute('name', $appliedFixer);
2✔
98
            $appliedFixersXML->appendChild($appliedFixerXML);
2✔
99
        }
100

101
        return $appliedFixersXML;
2✔
102
    }
103

104
    private function createDiffElement(\DOMDocument $dom, string $diff): \DOMElement
105
    {
106
        $diffXML = $dom->createElement('diff');
4✔
107
        $diffXML->appendChild($dom->createCDATASection($diff));
4✔
108

109
        return $diffXML;
4✔
110
    }
111

112
    private function createTimeElement(float $time, \DOMDocument $dom): \DOMElement
113
    {
114
        $time = round($time / 1_000, 3);
2✔
115

116
        $timeXML = $dom->createElement('time');
2✔
117
        $timeXML->setAttribute('unit', 's');
2✔
118
        $timeTotalXML = $dom->createElement('total');
2✔
119
        $timeTotalXML->setAttribute('value', (string) $time);
2✔
120
        $timeXML->appendChild($timeTotalXML);
2✔
121

122
        return $timeXML;
2✔
123
    }
124

125
    private function createMemoryElement(float $memory, \DOMDocument $dom): \DOMElement
126
    {
127
        $memory = round($memory / 1_024 / 1_024, 3);
2✔
128

129
        $memoryXML = $dom->createElement('memory');
2✔
130
        $memoryXML->setAttribute('value', (string) $memory);
2✔
131
        $memoryXML->setAttribute('unit', 'MB');
2✔
132

133
        return $memoryXML;
2✔
134
    }
135

136
    private function createAboutElement(\DOMDocument $dom, string $about): \DOMElement
137
    {
138
        $xml = $dom->createElement('about');
6✔
139
        $xml->setAttribute('value', $about);
6✔
140

141
        return $xml;
6✔
142
    }
143
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc