From 81898d629e734f1722f3b4621b3ba7a212612da8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 10 Apr 2025 15:31:10 +0200 Subject: [PATCH] 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 parse(); + + // Make sure the fixer is aware of the reparsed file to prevent a race-condition + // with the Diff report also re-parsing the file. + $phpcsFile->fixer->startFile($phpcsFile); } catch (Exception $e) { // This is a second parse, so ignore exceptions. // They would have been added to the file's error list already.