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

PHPCSStandards / PHP_CodeSniffer / 17662127818

12 Sep 2025 01:50AM UTC coverage: 78.786%. Remained the same
17662127818

push

github

web-flow
Merge pull request #1241 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-3

CS: normalize code style rules [3]

343 of 705 new or added lines in 108 files covered. (48.65%)

3 existing lines in 3 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

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

0.0
/src/Reports/Junit.php
1
<?php
2
/**
3
 * JUnit report for PHP_CodeSniffer.
4
 *
5
 * @author    Oleg Lobach <oleg@lobach.info>
6
 * @author    Greg Sherwood <gsherwood@squiz.net>
7
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
8
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
9
 */
10

11
namespace PHP_CodeSniffer\Reports;
12

13
use PHP_CodeSniffer\Config;
14
use PHP_CodeSniffer\Files\File;
15
use XMLWriter;
16

17
class Junit implements Report
18
{
19

20

21
    /**
22
     * Generate a partial report for a single processed file.
23
     *
24
     * Function should return TRUE if it printed or stored data about the file
25
     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
26
     * its data should be counted in the grand totals.
27
     *
28
     * @param array<string, string|int|array> $report      Prepared report data.
29
     *                                                     See the {@see Report} interface for a detailed specification.
30
     * @param \PHP_CodeSniffer\Files\File     $phpcsFile   The file being reported on.
31
     * @param bool                            $showSources Show sources?
32
     * @param int                             $width       Maximum allowed line width.
33
     *
34
     * @return bool
35
     */
36
    public function generateFileReport(array $report, File $phpcsFile, bool $showSources = false, int $width = 80)
×
37
    {
38
        $out = new XMLWriter;
×
39
        $out->openMemory();
×
40
        $out->setIndent(true);
×
41

42
        $out->startElement('testsuite');
×
43
        $out->writeAttribute('name', $report['filename']);
×
44
        $out->writeAttribute('errors', 0);
×
45

46
        if (count($report['messages']) === 0) {
×
47
            $out->writeAttribute('tests', 1);
×
48
            $out->writeAttribute('failures', 0);
×
49

50
            $out->startElement('testcase');
×
51
            $out->writeAttribute('name', $report['filename']);
×
52
            $out->endElement();
×
53
        } else {
54
            $failures = ($report['errors'] + $report['warnings']);
×
55
            $out->writeAttribute('tests', $failures);
×
56
            $out->writeAttribute('failures', $failures);
×
57

58
            foreach ($report['messages'] as $line => $lineErrors) {
×
59
                foreach ($lineErrors as $column => $colErrors) {
×
60
                    foreach ($colErrors as $error) {
×
61
                        $out->startElement('testcase');
×
NEW
62
                        $out->writeAttribute('name', $error['source'] . ' at ' . $report['filename'] . " ($line:$column)");
×
63

64
                        $error['type'] = strtolower($error['type']);
×
65
                        if ($phpcsFile->config->encoding !== 'utf-8') {
×
66
                            $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']);
×
67
                        }
68

69
                        $out->startElement('failure');
×
70
                        $out->writeAttribute('type', $error['type']);
×
71
                        $out->writeAttribute('message', $error['message']);
×
72
                        $out->endElement();
×
73

74
                        $out->endElement();
×
75
                    }
76
                }
77
            }
78
        }//end if
79

80
        $out->endElement();
×
81
        echo $out->flush();
×
82
        return true;
×
83

84
    }//end generateFileReport()
85

86

87
    /**
88
     * Prints all violations for processed files, in a proprietary XML format.
89
     *
90
     * @param string $cachedData    Any partial report data that was returned from
91
     *                              generateFileReport during the run.
92
     * @param int    $totalFiles    Total number of files processed during the run.
93
     * @param int    $totalErrors   Total number of errors found during the run.
94
     * @param int    $totalWarnings Total number of warnings found during the run.
95
     * @param int    $totalFixable  Total number of problems that can be fixed.
96
     * @param bool   $showSources   Show sources?
97
     * @param int    $width         Maximum allowed line width.
98
     * @param bool   $interactive   Are we running in interactive mode?
99
     * @param bool   $toScreen      Is the report being printed to screen?
100
     *
101
     * @return void
102
     */
103
    public function generate(
×
104
        string $cachedData,
105
        int $totalFiles,
106
        int $totalErrors,
107
        int $totalWarnings,
108
        int $totalFixable,
109
        bool $showSources = false,
110
        int $width = 80,
111
        bool $interactive = false,
112
        bool $toScreen = true
113
    ) {
114
        // Figure out the total number of tests.
115
        $tests   = 0;
×
116
        $matches = [];
×
117
        preg_match_all('/tests="([0-9]+)"/', $cachedData, $matches);
×
118
        if (isset($matches[1]) === true) {
×
119
            foreach ($matches[1] as $match) {
×
120
                $tests += $match;
×
121
            }
122
        }
123

124
        $failures = ($totalErrors + $totalWarnings);
×
NEW
125
        echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
×
NEW
126
        echo '<testsuites name="PHP_CodeSniffer ' . Config::VERSION . '" errors="0" tests="' . $tests . '" failures="' . $failures . '">' . PHP_EOL;
×
127
        echo $cachedData;
×
NEW
128
        echo '</testsuites>' . PHP_EOL;
×
129

130
    }//end generate()
131

132

133
}//end class
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