Skip to content

[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

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, int>
*/
private $fixableWarningCountFirstRun;
private $firstRunCounts;

/**
* The current total number of errors that can be fixed.
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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.
*
Expand Down
46 changes: 44 additions & 2 deletions src/Reports/Cbf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Loading