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

keradus / PHP-CS-Fixer / 18589345849

16 Oct 2025 09:31PM UTC coverage: 94.158% (+0.01%) from 94.148%
18589345849

push

github

web-flow
docs: update usage documentation for describe `--expand` and `@` (#9119)

28675 of 30454 relevant lines covered (94.16%)

45.16 hits per line

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

97.14
/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
        $testsuite = $testsuites->appendChild($dom->createElement('testsuite'));
6✔
47
        \assert($testsuite instanceof \DOMElement);
6✔
48

49
        $testsuite->setAttribute('name', 'PHP CS Fixer');
6✔
50

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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