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

PHPCSStandards / PHP_CodeSniffer / 14424850286

13 Apr 2025 01:29AM UTC coverage: 77.557% (-0.003%) from 77.56%
14424850286

push

github

jrfnl
Report/Code: fix fatal potential fatal error when combined with Diff report

Okay, so this is an awkward one.

If both the `Code` + the `Diff` report were requested + caching was turned on + the _order_ of the requested reports was `Code,Diff` (i.e. first the code report), the following fatal error which would occur:
```
Fatal error: Uncaught Error: Call to a member function getFixableCount() on null in path/to/PHP_CodeSniffer/src/Fixer.php:144
Stack trace:
#0 path/to/PHP_CodeSniffer/src/Reports/Diff.php(73): PHP_CodeSniffer\Fixer->fixFile()
#1 path/to/PHP_CodeSniffer/src/Reporter.php(285): PHP_CodeSniffer\Reports\Diff->generateFileReport(Array, Object(PHP_CodeSniffer\Files\LocalFile), true, 150)
#2 path/to/PHP_CodeSniffer/src/Runner.php(659): PHP_CodeSniffer\Reporter->cacheFileReport(Object(PHP_CodeSniffer\Files\LocalFile))
#3 path/to/PHP_CodeSniffer/src/Runner.php(400): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile))
#4 path/to/PHP_CodeSniffer/src/Runner.php(119): PHP_CodeSniffer\Runner->run()
#5 path/to/PHP_CodeSniffer/bin/phpcs(30): PHP_CodeSniffer\Runner->runPHPCS()
#6 {main}
  thrown in path/to/PHP_CodeSniffer/src/Fixer.php on line 144
```

To reproduce the issue (on `master`):
1. Create a small test file like:
    ```php
    <?php

    echo 'hello'.callMe( $p );
    ```
2. Run the following command to initialize the cache:
    ```bash
    phpcs -ps ./test.php --standard=PSR2 --report=Code,Diff --cache
    ```
    All should be fine.
3. Now run the same command again and see the fatal error.

Some investigating later, it turns out that both the `Code` report, as well as the `Diff` report, re-parse the current file, with the `Diff` report initializing the fixer once the file has reparsed, but the `Code` report not doing so (as it doesn't need the fixer).
The problem with that is that the `Code` report basically leaves the `Fixer` in an invalid state, leading to the above error.

While it is up for debate whether repor... (continued)

0 of 1 new or added line in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

19345 of 24943 relevant lines covered (77.56%)

78.9 hits per line

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

0.0
/src/Reports/Code.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 Exception;
13
use PHP_CodeSniffer\Files\File;
14
use PHP_CodeSniffer\Util\Common;
15

16
class Code implements Report
17
{
18

19

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

42
        // How many lines to show above and below the error line.
43
        $surroundingLines = 2;
×
44

45
        $file   = $report['filename'];
×
46
        $tokens = $phpcsFile->getTokens();
×
47
        if (empty($tokens) === true) {
×
48
            if (PHP_CODESNIFFER_VERBOSITY === 1) {
×
49
                $startTime = microtime(true);
×
50
                Common::forcePrintStatusMessage('CODE report is parsing '.basename($file).' ', 0, true);
×
51
            } else if (PHP_CODESNIFFER_VERBOSITY > 1) {
×
52
                Common::forcePrintStatusMessage("CODE report is forcing parse of $file", 0);
×
53
            }
54

55
            try {
56
                $phpcsFile->parse();
×
57

58
                // Make sure the fixer is aware of the reparsed file to prevent a race-condition
59
                // with the Diff report also re-parsing the file.
NEW
60
                $phpcsFile->fixer->startFile($phpcsFile);
×
UNCOV
61
            } catch (Exception $e) {
×
62
                // This is a second parse, so ignore exceptions.
63
                // They would have been added to the file's error list already.
64
            }
65

66
            if (PHP_CODESNIFFER_VERBOSITY === 1) {
×
67
                $timeTaken = ((microtime(true) - $startTime) * 1000);
×
68
                if ($timeTaken < 1000) {
×
69
                    $timeTaken = round($timeTaken);
×
70
                    Common::forcePrintStatusMessage("DONE in {$timeTaken}ms", 0);
×
71
                } else {
72
                    $timeTaken = round(($timeTaken / 1000), 2);
×
73
                    Common::forcePrintStatusMessage("DONE in $timeTaken secs", 0);
×
74
                }
75
            }
76

77
            $tokens = $phpcsFile->getTokens();
×
78
        }//end if
79

80
        // Create an array that maps lines to the first token on the line.
81
        $lineTokens = [];
×
82
        $lastLine   = 0;
×
83
        $stackPtr   = 0;
×
84
        foreach ($tokens as $stackPtr => $token) {
×
85
            if ($token['line'] !== $lastLine) {
×
86
                if ($lastLine > 0) {
×
87
                    $lineTokens[$lastLine]['end'] = ($stackPtr - 1);
×
88
                }
89

90
                $lastLine++;
×
91
                $lineTokens[$lastLine] = [
×
92
                    'start' => $stackPtr,
×
93
                    'end'   => null,
×
94
                ];
×
95
            }
96
        }
97

98
        // Make sure the last token in the file sits on an imaginary
99
        // last line so it is easier to generate code snippets at the
100
        // end of the file.
101
        $lineTokens[$lastLine]['end'] = $stackPtr;
×
102

103
        // Determine the longest code line we will be showing.
104
        $maxSnippetLength = 0;
×
105
        $eolLen           = strlen($phpcsFile->eolChar);
×
106
        foreach ($report['messages'] as $line => $lineErrors) {
×
107
            $startLine = max(($line - $surroundingLines), 1);
×
108
            $endLine   = min(($line + $surroundingLines), $lastLine);
×
109

110
            $maxLineNumLength = strlen($endLine);
×
111

112
            for ($i = $startLine; $i <= $endLine; $i++) {
×
113
                if ($i === 1) {
×
114
                    continue;
×
115
                }
116

117
                $lineLength       = ($tokens[($lineTokens[$i]['start'] - 1)]['column'] + $tokens[($lineTokens[$i]['start'] - 1)]['length'] - $eolLen);
×
118
                $maxSnippetLength = max($lineLength, $maxSnippetLength);
×
119
            }
120
        }
121

122
        $maxSnippetLength += ($maxLineNumLength + 8);
×
123

124
        // Determine the longest error message we will be showing.
125
        $maxErrorLength = 0;
×
126
        foreach ($report['messages'] as $lineErrors) {
×
127
            foreach ($lineErrors as $colErrors) {
×
128
                foreach ($colErrors as $error) {
×
129
                    $length = strlen($error['message']);
×
130
                    if ($showSources === true) {
×
131
                        $length += (strlen($error['source']) + 3);
×
132
                    }
133

134
                    $maxErrorLength = max($maxErrorLength, ($length + 1));
×
135
                }
136
            }
137
        }
138

139
        // The padding that all lines will require that are printing an error message overflow.
140
        if ($report['warnings'] > 0) {
×
141
            $typeLength = 7;
×
142
        } else {
143
            $typeLength = 5;
×
144
        }
145

146
        $errorPadding  = str_repeat(' ', ($maxLineNumLength + 7));
×
147
        $errorPadding .= str_repeat(' ', $typeLength);
×
148
        $errorPadding .= ' ';
×
149
        if ($report['fixable'] > 0) {
×
150
            $errorPadding .= '    ';
×
151
        }
152

153
        $errorPaddingLength = strlen($errorPadding);
×
154

155
        // The maximum amount of space an error message can use.
156
        $maxErrorSpace = ($width - $errorPaddingLength);
×
157
        if ($showSources === true) {
×
158
            // Account for the chars used to print colors.
159
            $maxErrorSpace += 8;
×
160
        }
161

162
        // Figure out the max report width we need and can use.
163
        $fileLength = strlen($file);
×
164
        $maxWidth   = max(($fileLength + 6), ($maxErrorLength + $errorPaddingLength));
×
165
        $width      = max(min($width, $maxWidth), $maxSnippetLength);
×
166
        if ($width < 70) {
×
167
            $width = 70;
×
168
        }
169

170
        // Print the file header.
171
        echo PHP_EOL."\033[1mFILE: ";
×
172
        if ($fileLength <= ($width - 6)) {
×
173
            echo $file;
×
174
        } else {
175
            echo '...'.substr($file, ($fileLength - ($width - 6)));
×
176
        }
177

178
        echo "\033[0m".PHP_EOL;
×
179
        echo str_repeat('-', $width).PHP_EOL;
×
180

181
        echo "\033[1m".'FOUND '.$report['errors'].' ERROR';
×
182
        if ($report['errors'] !== 1) {
×
183
            echo 'S';
×
184
        }
185

186
        if ($report['warnings'] > 0) {
×
187
            echo ' AND '.$report['warnings'].' WARNING';
×
188
            if ($report['warnings'] !== 1) {
×
189
                echo 'S';
×
190
            }
191
        }
192

193
        echo ' AFFECTING '.count($report['messages']).' LINE';
×
194
        if (count($report['messages']) !== 1) {
×
195
            echo 'S';
×
196
        }
197

198
        echo "\033[0m".PHP_EOL;
×
199

200
        foreach ($report['messages'] as $line => $lineErrors) {
×
201
            $startLine = max(($line - $surroundingLines), 1);
×
202
            $endLine   = min(($line + $surroundingLines), $lastLine);
×
203

204
            $snippet = '';
×
205
            if (isset($lineTokens[$startLine]) === true) {
×
206
                for ($i = $lineTokens[$startLine]['start']; $i <= $lineTokens[$endLine]['end']; $i++) {
×
207
                    $snippetLine = $tokens[$i]['line'];
×
208
                    if ($lineTokens[$snippetLine]['start'] === $i) {
×
209
                        // Starting a new line.
210
                        if ($snippetLine === $line) {
×
211
                            $snippet .= "\033[1m".'>> ';
×
212
                        } else {
213
                            $snippet .= '   ';
×
214
                        }
215

216
                        $snippet .= str_repeat(' ', ($maxLineNumLength - strlen($snippetLine)));
×
217
                        $snippet .= $snippetLine.':  ';
×
218
                        if ($snippetLine === $line) {
×
219
                            $snippet .= "\033[0m";
×
220
                        }
221
                    }
222

223
                    if (isset($tokens[$i]['orig_content']) === true) {
×
224
                        $tokenContent = $tokens[$i]['orig_content'];
×
225
                    } else {
226
                        $tokenContent = $tokens[$i]['content'];
×
227
                    }
228

229
                    if (strpos($tokenContent, "\t") !== false) {
×
230
                        $token            = $tokens[$i];
×
231
                        $token['content'] = $tokenContent;
×
232
                        if (stripos(PHP_OS, 'WIN') === 0) {
×
233
                            $tab = "\000";
×
234
                        } else {
235
                            $tab = "\033[30;1m»\033[0m";
×
236
                        }
237

238
                        $phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000");
×
239
                        $tokenContent = $token['content'];
×
240
                    }
241

242
                    $tokenContent = Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]);
×
243
                    $tokenContent = str_replace("\000", ' ', $tokenContent);
×
244

245
                    $underline = false;
×
246
                    if ($snippetLine === $line && isset($lineErrors[$tokens[$i]['column']]) === true) {
×
247
                        $underline = true;
×
248
                    }
249

250
                    // Underline invisible characters as well.
251
                    if ($underline === true && trim($tokenContent) === '') {
×
252
                        $snippet .= "\033[4m".' '."\033[0m".$tokenContent;
×
253
                    } else {
254
                        if ($underline === true) {
×
255
                            $snippet .= "\033[4m";
×
256
                        }
257

258
                        $snippet .= $tokenContent;
×
259

260
                        if ($underline === true) {
×
261
                            $snippet .= "\033[0m";
×
262
                        }
263
                    }
264
                }//end for
265
            }//end if
266

267
            echo str_repeat('-', $width).PHP_EOL;
×
268

269
            foreach ($lineErrors as $colErrors) {
×
270
                foreach ($colErrors as $error) {
×
271
                    $padding = ($maxLineNumLength - strlen($line));
×
272
                    echo 'LINE '.str_repeat(' ', $padding).$line.': ';
×
273

274
                    if ($error['type'] === 'ERROR') {
×
275
                        echo "\033[31mERROR\033[0m";
×
276
                        if ($report['warnings'] > 0) {
×
277
                            echo '  ';
×
278
                        }
279
                    } else {
280
                        echo "\033[33mWARNING\033[0m";
×
281
                    }
282

283
                    echo ' ';
×
284
                    if ($report['fixable'] > 0) {
×
285
                        echo '[';
×
286
                        if ($error['fixable'] === true) {
×
287
                            echo 'x';
×
288
                        } else {
289
                            echo ' ';
×
290
                        }
291

292
                        echo '] ';
×
293
                    }
294

295
                    $message = $error['message'];
×
296
                    $message = str_replace("\n", "\n".$errorPadding, $message);
×
297
                    if ($showSources === true) {
×
298
                        $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')';
×
299
                    }
300

301
                    $errorMsg = wordwrap(
×
302
                        $message,
×
303
                        $maxErrorSpace,
×
304
                        PHP_EOL.$errorPadding
×
305
                    );
×
306

307
                    echo $errorMsg.PHP_EOL;
×
308
                }//end foreach
309
            }//end foreach
310

311
            echo str_repeat('-', $width).PHP_EOL;
×
312
            echo rtrim($snippet).PHP_EOL;
×
313
        }//end foreach
314

315
        echo str_repeat('-', $width).PHP_EOL;
×
316
        if ($report['fixable'] > 0) {
×
317
            echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL;
×
318
            echo str_repeat('-', $width).PHP_EOL;
×
319
        }
320

321
        return true;
×
322

323
    }//end generateFileReport()
324

325

326
    /**
327
     * Prints all errors and warnings for each file processed.
328
     *
329
     * @param string $cachedData    Any partial report data that was returned from
330
     *                              generateFileReport during the run.
331
     * @param int    $totalFiles    Total number of files processed during the run.
332
     * @param int    $totalErrors   Total number of errors found during the run.
333
     * @param int    $totalWarnings Total number of warnings found during the run.
334
     * @param int    $totalFixable  Total number of problems that can be fixed.
335
     * @param bool   $showSources   Show sources?
336
     * @param int    $width         Maximum allowed line width.
337
     * @param bool   $interactive   Are we running in interactive mode?
338
     * @param bool   $toScreen      Is the report being printed to screen?
339
     *
340
     * @return void
341
     */
342
    public function generate(
×
343
        $cachedData,
344
        $totalFiles,
345
        $totalErrors,
346
        $totalWarnings,
347
        $totalFixable,
348
        $showSources=false,
349
        $width=80,
350
        $interactive=false,
351
        $toScreen=true
352
    ) {
353
        if ($cachedData === '') {
×
354
            return;
×
355
        }
356

357
        echo $cachedData;
×
358

359
    }//end generate()
360

361

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