-
- Notifications
You must be signed in to change notification settings - Fork 89
Closed
Description
PR #1079 originally included a commit to also use the new ExitCode::calculate()
method when the fixer runs over code provided via STDIN.
Unfortunately, that didn't work as intended, so for now, phpcbf
will always return with a 0
exit code when ran over code provided via STDIN.
It should be investigated why the patch didn't work as expected and a fix to start using the proper exit code should be pulled.
Preferably such a patch should be accompanied by a set of tests covering the change, though creating these tests may not be that straight-forward as STDIN
would need to be manipulated.
Also see: #1079 (comment)
For reference, this is the patch file of the originally proposed change:
Patch file contents
From dc2352fe50bac97cc9a1648589619d1a4d64373e Mon Sep 17 00:00:00 2001 From: jrfnl <jrfnl@users.noreply.github.com> Date: Tue, 29 Apr 2025 01:57:34 +0200 Subject: [PATCH] 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 <gsherwood@squiz.net> + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> * @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 @@ namespace PHP_CodeSniffer\Reports; 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 @@ class Cbf implements Report // 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 @@ class Cbf implements Report }//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