From 848b8397afbb87206438b16bfa76e310a1f099f5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 8 Aug 2025 19:46:03 +0100 Subject: [PATCH 1/2] Use the new exit codes when scanning code from STDIN --- src/Reports/Cbf.php | 46 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/Reports/Cbf.php b/src/Reports/Cbf.php index 823ed14a57..5a157b6e77 100644 --- a/src/Reports/Cbf.php +++ b/src/Reports/Cbf.php @@ -7,7 +7,9 @@ * report from the command line. * * @author Greg Sherwood + * @author Juliette Reinders Folmer * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) + * @copyright 2025 PHPCSStandards and contributors * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence */ @@ -15,6 +17,7 @@ use PHP_CodeSniffer\Exceptions\DeepExitException; use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Reporter; use PHP_CodeSniffer\Util\ExitCode; use PHP_CodeSniffer\Util\Timing; use PHP_CodeSniffer\Util\Writers\StatusWriter; @@ -60,8 +63,12 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false, // Replacing STDIN, so output current file to STDOUT // even if nothing was fixed. Exit here because we // can't process any more than 1 file in this setup. - $fixedContent = $phpcsFile->fixer->getContents(); - throw new DeepExitException($fixedContent, ExitCode::OKAY); + echo $phpcsFile->fixer->getContents(); + + // Fake a Reporter instance to allow for getting a proper exit code. + $reporter = $this->createReporterInstance($phpcsFile); + + throw new DeepExitException('', ExitCode::calculate($reporter)); } if ($errors === 0) { @@ -246,4 +253,39 @@ public function generate( }//end generate() + /** + * Create a "fake" Reporter instance to allow for getting a proper exit code when scanning code provided via STDIN. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being reported on. + * + * @return \PHP_CodeSniffer\Reporter + */ + private function createReporterInstance(File $phpcsFile) + { + $reporter = new class extends Reporter { + + + /** + * Overload the constructor as we don't need it. + */ + public function __construct() + { + }//end __construct() + + + }; + + $reporter->totalFiles = 1; + $reporter->totalErrors = $phpcsFile->getErrorCount(); + $reporter->totalWarnings = $phpcsFile->getWarningCount(); + $reporter->totalFixableErrors = $phpcsFile->getFixableErrorCount(); + $reporter->totalFixableWarnings = $phpcsFile->getFixableWarningCount(); + $reporter->totalFixedErrors = $phpcsFile->getFixedErrorCount(); + $reporter->totalFixedWarnings = $phpcsFile->getFixedWarningCount(); + + return $reporter; + + }//end createReporterInstance() + + }//end class From 869efc341ee38302d76494dabc12ae4c4b18df0c Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 8 Aug 2025 20:19:16 +0100 Subject: [PATCH 2/2] Fix exit code when phpcbf is processing STDIN --- src/Files/File.php | 45 ++++++++++++++++++++++++++++----------------- src/Reports/Cbf.php | 4 ++-- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/Files/File.php b/src/Files/File.php index e092c687de..b05f954afc 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -153,22 +153,13 @@ class File protected $warningCount = 0; /** - * The original total number of errors that can be fixed (first run on a file). + * The original total number of errors and warnings (first run on a file). * * {@internal This should be regarded as an immutable property.} * - * @var integer - */ - private $fixableErrorCountFirstRun; - - /** - * The original total number of warnings that can be fixed (first run on a file). - * - * {@internal This should be regarded as an immutable property.} - * - * @var integer + * @var array */ - private $fixableWarningCountFirstRun; + private $firstRunCounts; /** * The current total number of errors that can be fixed. @@ -555,14 +546,18 @@ public function process() StatusWriter::write('*** END SNIFF PROCESSING REPORT ***', 1); } - if (isset($this->fixableErrorCountFirstRun, $this->fixableWarningCountFirstRun) === false) { - $this->fixableErrorCountFirstRun = $this->fixableErrorCount; - $this->fixableWarningCountFirstRun = $this->fixableWarningCount; + if (isset($this->firstRunCounts) === false) { + $this->firstRunCounts = [ + 'error' => $this->errorCount, + 'warning' => $this->warningCount, + 'fixableError' => $this->fixableErrorCount, + 'fixableWarning' => $this->fixableWarningCount, + ]; } $this->fixedCount += $this->fixer->getFixCount(); - $this->fixedErrorCount = ($this->fixableErrorCountFirstRun - $this->fixableErrorCount); - $this->fixedWarningCount = ($this->fixableWarningCountFirstRun - $this->fixableWarningCount); + $this->fixedErrorCount = ($this->firstRunCounts['fixableError'] - $this->fixableErrorCount); + $this->fixedWarningCount = ($this->firstRunCounts['fixableWarning'] - $this->fixableWarningCount); }//end process() @@ -1217,6 +1212,22 @@ public function getFixedWarningCount() }//end getFixedWarningCount() + /** + * Retrieve information about the first run. + * + * @param $type string + * + * @internal This method does not form part of any public API nor backwards compatibility guarantee. + * + * @return int + */ + public function getFirstRunCount(string $type):int + { + return $this->firstRunCounts[$type]; + + }//end getFirstRunCount() + + /** * Returns the list of ignored lines. * diff --git a/src/Reports/Cbf.php b/src/Reports/Cbf.php index 5a157b6e77..e3369dd898 100644 --- a/src/Reports/Cbf.php +++ b/src/Reports/Cbf.php @@ -276,8 +276,8 @@ public function __construct() }; $reporter->totalFiles = 1; - $reporter->totalErrors = $phpcsFile->getErrorCount(); - $reporter->totalWarnings = $phpcsFile->getWarningCount(); + $reporter->totalErrors = $phpcsFile->getFirstRunCount('error'); + $reporter->totalWarnings = $phpcsFile->getFirstRunCount('warning'); $reporter->totalFixableErrors = $phpcsFile->getFixableErrorCount(); $reporter->totalFixableWarnings = $phpcsFile->getFixableWarningCount(); $reporter->totalFixedErrors = $phpcsFile->getFixedErrorCount();