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

PHPCSStandards / PHP_CodeSniffer / 17690095598

13 Sep 2025 01:53AM UTC coverage: 78.501% (-0.006%) from 78.507%
17690095598

Pull #1251

github

web-flow
Merge 69eab3fa8 into 66b2fa9f8
Pull Request #1251: Assorted minor fixes

4 of 5 new or added lines in 4 files covered. (80.0%)

2 existing lines in 2 files now uncovered.

25304 of 32234 relevant lines covered (78.5%)

74.7 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($report, File $phpcsFile, $showSources=false, $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');
×
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
        $cachedData,
105
        $totalFiles,
106
        $totalErrors,
107
        $totalWarnings,
108
        $totalFixable,
109
        $showSources=false,
110
        $width=80,
111
        $interactive=false,
112
        $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);
×
NEW
118
        if (empty($matches[1]) === false) {
×
119
            foreach ($matches[1] as $match) {
×
120
                $tests += $match;
×
121
            }
122
        }
123

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