-
-
Notifications
You must be signed in to change notification settings - Fork 89
[4.0] Fix exit code when phpcbf is processing STDIN #1175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jrfnl
merged 6 commits into
PHPCSStandards:4.x
from
fredden:issue-1082/exit-code-when-fixing-stdin
Aug 29, 2025
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b872aeb
Use the new exit codes when scanning code from STDIN
jrfnl 4f225c4
Fix exit code when phpcbf is processing STDIN
fredden 7bfebcc
Add tests to cover exit codes for phpcs & phpcbf
fredden ea68ece
Move exit code tests to a file of their own
fredden 87c0b23
Change comment flavour from '#' to '//'
fredden 35098d7
Merge remote-tracking branch 'upstream/4.x' into issue-1082/exit-code…
fredden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,14 +7,17 @@ | |
* report from the command line. | ||
* | ||
* @author Greg Sherwood <[email protected]> | ||
* @author Juliette Reinders Folmer <[email protected]> | ||
* @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 | ||
*/ | ||
|
||
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 @@ 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->getFirstRunCount('error'); | ||
$reporter->totalWarnings = $phpcsFile->getFirstRunCount('warning'); | ||
$reporter->totalFixableErrors = $phpcsFile->getFixableErrorCount(); | ||
$reporter->totalFixableWarnings = $phpcsFile->getFixableWarningCount(); | ||
$reporter->totalFixedErrors = $phpcsFile->getFixedErrorCount(); | ||
$reporter->totalFixedWarnings = $phpcsFile->getFixedWarningCount(); | ||
|
||
return $reporter; | ||
|
||
}//end createReporterInstance() | ||
|
||
|
||
}//end class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* Class containing a simple style error that phpcbf cannot fix, and another that it can fix. | ||
* | ||
* @copyright 2025 PHPCSStandards and contributors | ||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Tests\EndToEnd\Fixtures; | ||
|
||
# The brace on the following line should be on a line by itself. This can be fixed with phpcbf. | ||
class ClassWithTwoStyleErrors { | ||
} # This comment does not belong here, according to PSR12. This cannot be fixed with phpcbf. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* Class containing a simple style error that phpcbf cannot fix. | ||
* | ||
* @copyright 2025 PHPCSStandards and contributors | ||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Tests\EndToEnd\Fixtures; | ||
|
||
class ClassWithUnfixableStyleError | ||
{ | ||
} # This comment does not belong here, according to PSR12. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.