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

keradus / PHP-CS-Fixer / 17319949156

29 Aug 2025 09:20AM UTC coverage: 94.696% (-0.05%) from 94.744%
17319949156

push

github

keradus
CS

28333 of 29920 relevant lines covered (94.7%)

45.63 hits per line

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

97.1
/src/Console/Report/FixReport/JunitReporter.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 PhpCsFixer\Preg;
19
use Symfony\Component\Console\Formatter\OutputFormatter;
20

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

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

43
        $dom = new \DOMDocument('1.0', 'UTF-8');
6✔
44
        $testsuites = $dom->appendChild($dom->createElement('testsuites'));
6✔
45

46
        /** @var \DOMElement $testsuite */
47
        $testsuite = $testsuites->appendChild($dom->createElement('testsuite'));
6✔
48
        $testsuite->setAttribute('name', 'PHP CS Fixer');
6✔
49

50
        $properties = $dom->createElement('properties');
6✔
51
        $property = $dom->createElement('property');
6✔
52
        $property->setAttribute('name', 'about');
6✔
53
        $property->setAttribute('value', Application::getAbout());
6✔
54
        $properties->appendChild($property);
6✔
55
        $testsuite->appendChild($properties);
6✔
56

57
        if (\count($reportSummary->getChanged()) > 0) {
6✔
58
            $this->createFailedTestCases($dom, $testsuite, $reportSummary);
5✔
59
        } else {
60
            $this->createSuccessTestCase($dom, $testsuite);
1✔
61
        }
62

63
        if ($reportSummary->getTime() > 0) {
6✔
64
            $testsuite->setAttribute(
2✔
65
                'time',
2✔
66
                \sprintf(
2✔
67
                    '%.3f',
2✔
68
                    $reportSummary->getTime() / 1_000
2✔
69
                )
2✔
70
            );
2✔
71
        }
72

73
        $dom->formatOutput = true;
6✔
74

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

80
        return $reportSummary->isDecoratedOutput() ? OutputFormatter::escape($result) : $result;
6✔
81
    }
82

83
    private function createSuccessTestCase(\DOMDocument $dom, \DOMElement $testsuite): void
84
    {
85
        $testcase = $dom->createElement('testcase');
1✔
86
        $testcase->setAttribute('name', 'All OK');
1✔
87
        $testcase->setAttribute('assertions', '1');
1✔
88

89
        $testsuite->appendChild($testcase);
1✔
90
        $testsuite->setAttribute('tests', '1');
1✔
91
        $testsuite->setAttribute('assertions', '1');
1✔
92
        $testsuite->setAttribute('failures', '0');
1✔
93
        $testsuite->setAttribute('errors', '0');
1✔
94
    }
95

96
    private function createFailedTestCases(\DOMDocument $dom, \DOMElement $testsuite, ReportSummary $reportSummary): void
97
    {
98
        $assertionsCount = 0;
5✔
99
        foreach ($reportSummary->getChanged() as $file => $fixResult) {
5✔
100
            $testcase = $this->createFailedTestCase(
5✔
101
                $dom,
5✔
102
                $file,
5✔
103
                $fixResult,
5✔
104
                $reportSummary->shouldAddAppliedFixers()
5✔
105
            );
5✔
106
            $testsuite->appendChild($testcase);
5✔
107
            $assertionsCount += (int) $testcase->getAttribute('assertions');
5✔
108
        }
109

110
        $testsuite->setAttribute('tests', (string) \count($reportSummary->getChanged()));
5✔
111
        $testsuite->setAttribute('assertions', (string) $assertionsCount);
5✔
112
        $testsuite->setAttribute('failures', (string) $assertionsCount);
5✔
113
        $testsuite->setAttribute('errors', '0');
5✔
114
    }
115

116
    /**
117
     * @param array{appliedFixers: list<string>, diff: string} $fixResult
118
     */
119
    private function createFailedTestCase(\DOMDocument $dom, string $file, array $fixResult, bool $shouldAddAppliedFixers): \DOMElement
120
    {
121
        $appliedFixersCount = \count($fixResult['appliedFixers']);
5✔
122

123
        $testName = str_replace('.', '_DOT_', Preg::replace('@\.'.pathinfo($file, \PATHINFO_EXTENSION).'$@', '', $file));
5✔
124

125
        $testcase = $dom->createElement('testcase');
5✔
126
        $testcase->setAttribute('name', $testName);
5✔
127
        $testcase->setAttribute('file', $file);
5✔
128
        $testcase->setAttribute('assertions', (string) $appliedFixersCount);
5✔
129

130
        $failure = $dom->createElement('failure');
5✔
131
        $failure->setAttribute('type', 'code_style');
5✔
132
        $testcase->appendChild($failure);
5✔
133

134
        if ($shouldAddAppliedFixers) {
5✔
135
            $failureContent = "applied fixers:\n---------------\n";
2✔
136

137
            foreach ($fixResult['appliedFixers'] as $appliedFixer) {
2✔
138
                $failureContent .= "* {$appliedFixer}\n";
2✔
139
            }
140
        } else {
141
            $failureContent = "Wrong code style\n";
3✔
142
        }
143

144
        if ('' !== $fixResult['diff']) {
5✔
145
            $failureContent .= "\nDiff:\n---------------\n\n".$fixResult['diff'];
4✔
146
        }
147

148
        $failure->appendChild($dom->createCDATASection(trim($failureContent)));
5✔
149

150
        return $testcase;
5✔
151
    }
152
}
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