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

keradus / PHP-CS-Fixer / 15997074480

30 Jun 2025 10:32PM UTC coverage: 94.846% (+0.007%) from 94.839%
15997074480

push

github

web-flow
chore: partially apply NoExtraBlankLinesFixer:tokens.comma (#8762)

28193 of 29725 relevant lines covered (94.85%)

45.34 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
final class JunitReporter implements ReporterInterface
29
{
30
    public function getFormat(): string
31
    {
32
        return 'junit';
1✔
33
    }
34

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

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

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

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

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

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

71
        $dom->formatOutput = true;
6✔
72

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

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

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

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

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

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

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

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

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

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

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

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

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

146
        $failure->appendChild($dom->createCDATASection(trim($failureContent)));
5✔
147

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