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

PHPCSStandards / PHP_CodeSniffer / 9756688221

02 Jul 2024 07:22AM UTC coverage: 75.507%. Remained the same
9756688221

push

github

web-flow
Merge pull request #523 from PHPCSStandards/feature/docs-improve-reports-array-format-info

Documentation: improve information about Report data format + remove unused foreach keys

0 of 9 new or added lines in 4 files covered. (0.0%)

1 existing line in 1 file now uncovered.

23275 of 30825 relevant lines covered (75.51%)

61.66 hits per line

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

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

10
namespace PHP_CodeSniffer\Reports;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Util\Timing;
14

15
class Full implements Report
16
{
17

18

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

41
        // The length of the word ERROR or WARNING; used for padding.
42
        if ($report['warnings'] > 0) {
×
43
            $typeLength = 7;
×
44
        } else {
45
            $typeLength = 5;
×
46
        }
47

48
        // Work out the max line number length for formatting.
49
        $maxLineNumLength = max(array_map('strlen', array_keys($report['messages'])));
×
50

51
        // The padding that all lines will require that are
52
        // printing an error message overflow.
53
        $paddingLine2  = str_repeat(' ', ($maxLineNumLength + 1));
×
54
        $paddingLine2 .= ' | ';
×
55
        $paddingLine2 .= str_repeat(' ', $typeLength);
×
56
        $paddingLine2 .= ' | ';
×
57
        if ($report['fixable'] > 0) {
×
58
            $paddingLine2 .= '    ';
×
59
        }
60

61
        $paddingLength = strlen($paddingLine2);
×
62

63
        // Make sure the report width isn't too big.
64
        $maxErrorLength = 0;
×
NEW
65
        foreach ($report['messages'] as $lineErrors) {
×
NEW
66
            foreach ($lineErrors as $colErrors) {
×
UNCOV
67
                foreach ($colErrors as $error) {
×
68
                    // Start with the presumption of a single line error message.
69
                    $length    = strlen($error['message']);
×
70
                    $srcLength = (strlen($error['source']) + 3);
×
71
                    if ($showSources === true) {
×
72
                        $length += $srcLength;
×
73
                    }
74

75
                    // ... but also handle multi-line messages correctly.
76
                    if (strpos($error['message'], "\n") !== false) {
×
77
                        $errorLines = explode("\n", $error['message']);
×
78
                        $length     = max(array_map('strlen', $errorLines));
×
79

80
                        if ($showSources === true) {
×
81
                            $lastLine = array_pop($errorLines);
×
82
                            $length   = max($length, (strlen($lastLine) + $srcLength));
×
83
                        }
84
                    }
85

86
                    $maxErrorLength = max($maxErrorLength, ($length + 1));
×
87
                }//end foreach
88
            }//end foreach
89
        }//end foreach
90

91
        $file       = $report['filename'];
×
92
        $fileLength = strlen($file);
×
93
        $maxWidth   = max(($fileLength + 6), ($maxErrorLength + $paddingLength));
×
94
        $width      = min($width, $maxWidth);
×
95
        if ($width < 70) {
×
96
            $width = 70;
×
97
        }
98

99
        echo PHP_EOL."\033[1mFILE: ";
×
100
        if ($fileLength <= ($width - 6)) {
×
101
            echo $file;
×
102
        } else {
103
            echo '...'.substr($file, ($fileLength - ($width - 6)));
×
104
        }
105

106
        echo "\033[0m".PHP_EOL;
×
107
        echo str_repeat('-', $width).PHP_EOL;
×
108

109
        echo "\033[1m".'FOUND '.$report['errors'].' ERROR';
×
110
        if ($report['errors'] !== 1) {
×
111
            echo 'S';
×
112
        }
113

114
        if ($report['warnings'] > 0) {
×
115
            echo ' AND '.$report['warnings'].' WARNING';
×
116
            if ($report['warnings'] !== 1) {
×
117
                echo 'S';
×
118
            }
119
        }
120

121
        echo ' AFFECTING '.count($report['messages']).' LINE';
×
122
        if (count($report['messages']) !== 1) {
×
123
            echo 'S';
×
124
        }
125

126
        echo "\033[0m".PHP_EOL;
×
127
        echo str_repeat('-', $width).PHP_EOL;
×
128

129
        // The maximum amount of space an error message can use.
130
        $maxErrorSpace = ($width - $paddingLength - 1);
×
131

132
        $beforeMsg = '';
×
133
        $afterMsg  = '';
×
134
        if ($showSources === true) {
×
135
            $beforeMsg = "\033[1m";
×
136
            $afterMsg  = "\033[0m";
×
137
        }
138

139
        $beforeAfterLength = strlen($beforeMsg.$afterMsg);
×
140

141
        foreach ($report['messages'] as $line => $lineErrors) {
×
NEW
142
            foreach ($lineErrors as $colErrors) {
×
143
                foreach ($colErrors as $error) {
×
144
                    $errorMsg = wordwrap(
×
145
                        $error['message'],
×
146
                        $maxErrorSpace
×
147
                    );
×
148

149
                    // Add the padding _after_ the wordwrap as the message itself may contain line breaks
150
                    // and those lines will also need to receive padding.
151
                    $errorMsg = str_replace("\n", $afterMsg.PHP_EOL.$paddingLine2.$beforeMsg, $errorMsg);
×
152
                    $errorMsg = $beforeMsg.$errorMsg.$afterMsg;
×
153

154
                    if ($showSources === true) {
×
155
                        $lastMsg          = $errorMsg;
×
156
                        $startPosLastLine = strrpos($errorMsg, PHP_EOL.$paddingLine2.$beforeMsg);
×
157
                        if ($startPosLastLine !== false) {
×
158
                            // Message is multiline. Grab the text of last line of the message, including the color codes.
159
                            $lastMsg = substr($errorMsg, ($startPosLastLine + strlen(PHP_EOL.$paddingLine2)));
×
160
                        }
161

162
                        // When show sources is used, the message itself will be bolded, so we need to correct the length.
163
                        $sourceSuffix = '('.$error['source'].')';
×
164

165
                        $lastMsgPlusSourceLength = strlen($lastMsg);
×
166
                        // Add space + source suffix length.
167
                        $lastMsgPlusSourceLength += (1 + strlen($sourceSuffix));
×
168
                        // Correct for the color codes.
169
                        $lastMsgPlusSourceLength -= $beforeAfterLength;
×
170

171
                        if ($lastMsgPlusSourceLength > $maxErrorSpace) {
×
172
                            $errorMsg .= PHP_EOL.$paddingLine2.$sourceSuffix;
×
173
                        } else {
174
                            $errorMsg .= ' '.$sourceSuffix;
×
175
                        }
176
                    }//end if
177

178
                    // The padding that goes on the front of the line.
179
                    $padding = ($maxLineNumLength - strlen($line));
×
180

181
                    echo ' '.str_repeat(' ', $padding).$line.' | ';
×
182
                    if ($error['type'] === 'ERROR') {
×
183
                        echo "\033[31mERROR\033[0m";
×
184
                        if ($report['warnings'] > 0) {
×
185
                            echo '  ';
×
186
                        }
187
                    } else {
188
                        echo "\033[33mWARNING\033[0m";
×
189
                    }
190

191
                    echo ' | ';
×
192
                    if ($report['fixable'] > 0) {
×
193
                        echo '[';
×
194
                        if ($error['fixable'] === true) {
×
195
                            echo 'x';
×
196
                        } else {
197
                            echo ' ';
×
198
                        }
199

200
                        echo '] ';
×
201
                    }
202

203
                    echo $errorMsg.PHP_EOL;
×
204
                }//end foreach
205
            }//end foreach
206
        }//end foreach
207

208
        echo str_repeat('-', $width).PHP_EOL;
×
209
        if ($report['fixable'] > 0) {
×
210
            echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL;
×
211
            echo str_repeat('-', $width).PHP_EOL;
×
212
        }
213

214
        echo PHP_EOL;
×
215
        return true;
×
216

217
    }//end generateFileReport()
218

219

220
    /**
221
     * Prints all errors and warnings for each file processed.
222
     *
223
     * @param string $cachedData    Any partial report data that was returned from
224
     *                              generateFileReport during the run.
225
     * @param int    $totalFiles    Total number of files processed during the run.
226
     * @param int    $totalErrors   Total number of errors found during the run.
227
     * @param int    $totalWarnings Total number of warnings found during the run.
228
     * @param int    $totalFixable  Total number of problems that can be fixed.
229
     * @param bool   $showSources   Show sources?
230
     * @param int    $width         Maximum allowed line width.
231
     * @param bool   $interactive   Are we running in interactive mode?
232
     * @param bool   $toScreen      Is the report being printed to screen?
233
     *
234
     * @return void
235
     */
236
    public function generate(
×
237
        $cachedData,
238
        $totalFiles,
239
        $totalErrors,
240
        $totalWarnings,
241
        $totalFixable,
242
        $showSources=false,
243
        $width=80,
244
        $interactive=false,
245
        $toScreen=true
246
    ) {
247
        if ($cachedData === '') {
×
248
            return;
×
249
        }
250

251
        echo $cachedData;
×
252

253
        if ($toScreen === true && $interactive === false) {
×
254
            Timing::printRunTime();
×
255
        }
256

257
    }//end generate()
258

259

260
}//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