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

keradus / PHP-CS-Fixer / 16999983712

15 Aug 2025 09:42PM UTC coverage: 94.75% (-0.09%) from 94.839%
16999983712

push

github

keradus
ci: more self-fixing checks on lowest/highest PHP

28263 of 29829 relevant lines covered (94.75%)

45.88 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
final class XmlReporter implements ReporterInterface
28
{
29
    public function getFormat(): string
30
    {
31
        return 'xml';
1✔
32
    }
33

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

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

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

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

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

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

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

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

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

76
        $dom->formatOutput = true;
6✔
77

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

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

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

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

99
        return $appliedFixersXML;
2✔
100
    }
101

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

107
        return $diffXML;
4✔
108
    }
109

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

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

120
        return $timeXML;
2✔
121
    }
122

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

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

131
        return $memoryXML;
2✔
132
    }
133

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

139
        return $xml;
6✔
140
    }
141
}
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