Skip to content

Change the exit codes #1079

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
merged 5 commits into from
May 10, 2025
Merged
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
34 changes: 32 additions & 2 deletions .github/workflows/test-requirements-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,14 @@ jobs:
- name: Run the test
id: check
continue-on-error: true
run: php "bin/${{ matrix.cmd }}" --version
shell: bash
run: |
set +e
php "bin/${{ matrix.cmd }}" --version
exitcode="$?"
echo "EXITCODE=$exitcode" >> "$GITHUB_OUTPUT"
echo "Exitcode is: $exitcode"
exit "$exitcode"

- name: Check the result of a successful test against expectation
if: ${{ steps.check.outcome == 'success' && matrix.expect == 'fail' }}
Expand All @@ -152,6 +159,14 @@ jobs:
if: ${{ steps.check.outcome != 'success' && matrix.expect == 'success' }}
run: exit 1

- name: Verify the exit code is 0 when requirements are met
if: ${{ matrix.expect == 'success' && steps.check.outputs.EXITCODE != 0 }}
run: exit 1

- name: Verify the exit code is 64 when requirements are not met
if: ${{ matrix.expect == 'fail' && steps.check.outputs.EXITCODE != 64 }}
run: exit 1

build-phars:
needs: lint

Expand Down Expand Up @@ -199,7 +214,14 @@ jobs:
- name: Run the test
id: check
continue-on-error: true
run: php ${{ matrix.cmd }}.phar --version
shell: bash
run: |
set +e
php ${{ matrix.cmd }}.phar --version
exitcode="$?"
echo "EXITCODE=$exitcode" >> "$GITHUB_OUTPUT"
echo "Exitcode is: $exitcode"
exit "$exitcode"

- name: Check the result of a successful test against expectation
if: ${{ steps.check.outcome == 'success' && matrix.expect == 'fail' }}
Expand All @@ -208,3 +230,11 @@ jobs:
- name: Check the result of a failed test against expectation
if: ${{ steps.check.outcome != 'success' && matrix.expect == 'success' }}
run: exit 1

- name: Verify the exit code is 0 when requirements are met
if: ${{ matrix.expect == 'success' && steps.check.outputs.EXITCODE != 0 }}
run: exit 1

- name: Verify the exit code is 64 when requirements are not met
if: ${{ matrix.expect == 'fail' && steps.check.outputs.EXITCODE != 64 }}
run: exit 1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ composer.lock
phpstan.neon
/node_modules/
/tests/Standards/sniffStnd.xml
/tests/Core/Util/ExitCode/Fixtures/ExitCodeTest/*.fixed
/tests/Core/Util/ExitCode/Fixtures/ExitCodeTest/phpcs.cache
3 changes: 2 additions & 1 deletion requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
*/
function checkRequirements()
{
$exitCode = 3;
// IMPORTANT: Must stay in sync with the value of the `PHP_CodeSniffer\Util\ExitCode::REQUIREMENTS_NOT_MET` constant!
$exitCode = 64;

// Check the PHP version.
if (PHP_VERSION_ID < 70200) {
Expand Down
61 changes: 31 additions & 30 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\ExitCode;
use PHP_CodeSniffer\Util\Help;
use PHP_CodeSniffer\Util\Standards;

Expand Down Expand Up @@ -678,10 +679,10 @@ public function processShortArgument($arg, $pos)
case 'h':
case '?':
$this->printUsage();
throw new DeepExitException('', 0);
throw new DeepExitException('', ExitCode::OKAY);
case 'i' :
$output = Standards::prepareInstalledStandardsForDisplay().PHP_EOL;
throw new DeepExitException($output, 0);
throw new DeepExitException($output, ExitCode::OKAY);
case 'v' :
if ($this->quiet === true) {
// Ignore when quiet mode is enabled.
Expand Down Expand Up @@ -747,7 +748,7 @@ public function processShortArgument($arg, $pos)
if ($changed === false && ini_get($ini[0]) !== $ini[1]) {
$error = sprintf('ERROR: Ini option "%s" cannot be changed at runtime.', $ini[0]).PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}
break;
case 'n' :
Expand Down Expand Up @@ -789,11 +790,11 @@ public function processLongArgument($arg, $pos)
switch ($arg) {
case 'help':
$this->printUsage();
throw new DeepExitException('', 0);
throw new DeepExitException('', ExitCode::OKAY);
case 'version':
$output = 'PHP_CodeSniffer version '.self::VERSION.' ('.self::STABILITY.') ';
$output .= 'by Squiz and PHPCSStandards'.PHP_EOL;
throw new DeepExitException($output, 0);
throw new DeepExitException($output, ExitCode::OKAY);
case 'colors':
if (isset($this->overriddenDefaults['colors']) === true) {
break;
Expand Down Expand Up @@ -840,7 +841,7 @@ public function processLongArgument($arg, $pos)
) {
$error = 'ERROR: Setting a config option requires a name and value'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$key = $this->cliArgs[($pos + 1)];
Expand All @@ -850,7 +851,7 @@ public function processLongArgument($arg, $pos)
try {
$this->setConfigData($key, $value);
} catch (Exception $e) {
throw new DeepExitException($e->getMessage().PHP_EOL, 3);
throw new DeepExitException($e->getMessage().PHP_EOL, ExitCode::PROCESS_ERROR);
}

$output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
Expand All @@ -860,12 +861,12 @@ public function processLongArgument($arg, $pos)
} else {
$output .= "Config value \"$key\" updated successfully; old value was \"$current\"".PHP_EOL;
}
throw new DeepExitException($output, 0);
throw new DeepExitException($output, ExitCode::OKAY);
case 'config-delete':
if (isset($this->cliArgs[($pos + 1)]) === false) {
$error = 'ERROR: Deleting a config option requires the name of the option'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
Expand All @@ -878,24 +879,24 @@ public function processLongArgument($arg, $pos)
try {
$this->setConfigData($key, null);
} catch (Exception $e) {
throw new DeepExitException($e->getMessage().PHP_EOL, 3);
throw new DeepExitException($e->getMessage().PHP_EOL, ExitCode::PROCESS_ERROR);
}

$output .= "Config value \"$key\" removed successfully; old value was \"$current\"".PHP_EOL;
}
throw new DeepExitException($output, 0);
throw new DeepExitException($output, ExitCode::OKAY);
case 'config-show':
$data = self::getAllConfigData();
$output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
$output .= $this->prepareConfigDataForDisplay($data);
throw new DeepExitException($output, 0);
throw new DeepExitException($output, ExitCode::OKAY);
case 'runtime-set':
if (isset($this->cliArgs[($pos + 1)]) === false
|| isset($this->cliArgs[($pos + 2)]) === false
) {
$error = 'ERROR: Setting a runtime config option requires a name and value'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$key = $this->cliArgs[($pos + 1)];
Expand Down Expand Up @@ -946,7 +947,7 @@ public function processLongArgument($arg, $pos)
if (is_dir($dir) === false) {
$error = 'ERROR: The specified cache file path "'.$this->cacheFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

if ($dir === '.') {
Expand All @@ -972,7 +973,7 @@ public function processLongArgument($arg, $pos)
if (is_dir($this->cacheFile) === true) {
$error = 'ERROR: The specified cache file path "'.$this->cacheFile.'" is a directory'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}
} else if (substr($arg, 0, 10) === 'bootstrap=') {
$files = explode(',', substr($arg, 10));
Expand All @@ -982,7 +983,7 @@ public function processLongArgument($arg, $pos)
if ($path === false) {
$error = 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$bootstrap[] = $path;
Expand All @@ -996,7 +997,7 @@ public function processLongArgument($arg, $pos)
if ($path === false) {
$error = 'ERROR: The specified file list "'.$fileList.'" does not exist'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$files = file($path);
Expand Down Expand Up @@ -1038,7 +1039,7 @@ public function processLongArgument($arg, $pos)
if (is_dir($dir) === false) {
$error = 'ERROR: The specified report file path "'.$this->reportFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$this->reportFile = $dir.'/'.basename($this->reportFile);
Expand All @@ -1049,7 +1050,7 @@ public function processLongArgument($arg, $pos)
if (is_dir($this->reportFile) === true) {
$error = 'ERROR: The specified report file path "'.$this->reportFile.'" is a directory'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}
} else if (substr($arg, 0, 13) === 'report-width=') {
if (isset($this->overriddenDefaults['reportWidth']) === true) {
Expand Down Expand Up @@ -1080,7 +1081,7 @@ public function processLongArgument($arg, $pos)
if (is_dir($this->basepath) === false) {
$error = 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}
} else if ((substr($arg, 0, 7) === 'report=' || substr($arg, 0, 7) === 'report-')) {
$reports = [];
Expand All @@ -1101,15 +1102,15 @@ public function processLongArgument($arg, $pos)
if (is_dir($dir) === false) {
$error = 'ERROR: The specified '.$report.' report file path "'.$output.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$output = $dir.'/'.basename($output);

if (is_dir($output) === true) {
$error = 'ERROR: The specified '.$report.' report file path "'.$output.'" is a directory'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}
}//end if
}//end if
Expand Down Expand Up @@ -1167,7 +1168,7 @@ public function processLongArgument($arg, $pos)
$error .= 'PHP_CodeSniffer >= 4.0 only supports scanning PHP files.'.PHP_EOL;
$error .= 'Received: '.substr($arg, 11).PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}
}

Expand Down Expand Up @@ -1258,7 +1259,7 @@ public function processLongArgument($arg, $pos)
$validOptions
);
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$this->generator = $this->validGenerators[$lowerCaseGeneratorName];
Expand Down Expand Up @@ -1384,7 +1385,7 @@ static function ($carry, $item) {

$error .= PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException(ltrim($error), 3);
throw new DeepExitException(ltrim($error), ExitCode::PROCESS_ERROR);
}

return $sniffs;
Expand Down Expand Up @@ -1413,7 +1414,7 @@ public function processUnknownArgument($arg, $pos)

$error = "ERROR: option \"$arg\" not known".PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

$this->processFilePath($arg);
Expand Down Expand Up @@ -1444,7 +1445,7 @@ public function processFilePath($path)

$error = 'ERROR: The file "'.$path.'" does not exist.'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
} else {
// Can't modify the files array directly because it's not a real
// class member, so need to use this little get/modify/set trick.
Expand Down Expand Up @@ -1652,7 +1653,7 @@ public function setConfigData($key, $value, $temp=false)
&& is_writable($configFile) === false
) {
$error = 'ERROR: Config file '.$configFile.' is not writable'.PHP_EOL.PHP_EOL;
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}
}//end if

Expand All @@ -1673,7 +1674,7 @@ public function setConfigData($key, $value, $temp=false)

if (file_put_contents($configFile, $output) === false) {
$error = 'ERROR: Config file '.$configFile.' could not be written'.PHP_EOL.PHP_EOL;
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

self::$configDataFile = $configFile;
Expand Down Expand Up @@ -1731,7 +1732,7 @@ public static function getAllConfigData()

if (Common::isReadable($configFile) === false) {
$error = 'ERROR: Config file '.$configFile.' is not readable'.PHP_EOL.PHP_EOL;
throw new DeepExitException($error, 3);
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
}

include $configFile;
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/DeepExitException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
* Allows the runner to return an exit code instead of putting exit codes elsewhere
* in the source code.
*
* Exit codes passed to this exception (as the `$code` parameter) MUST be one of the
* predefined exit code constants per the `PHP_CodeSniffer\Util\ExitCode` class; or a bitmask sum of those.
*
* @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
*/

Expand Down
22 changes: 13 additions & 9 deletions src/Files/DummyFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,23 @@ public function __construct($content, Ruleset $ruleset, Config $config)
/**
* Set the error, warning, and fixable counts for the file.
*
* @param int $errorCount The number of errors found.
* @param int $warningCount The number of warnings found.
* @param int $fixableCount The number of fixable errors found.
* @param int $fixedCount The number of errors that were fixed.
* @param int $errorCount The number of errors found.
* @param int $warningCount The number of warnings found.
* @param int $fixableErrorCount The number of fixable errors found.
* @param int $fixableWarningCount The number of fixable warning found.
* @param int $fixedErrorCount The number of errors that were fixed.
* @param int $fixedWarningCount The number of warning that were fixed.
*
* @return void
*/
public function setErrorCounts($errorCount, $warningCount, $fixableCount, $fixedCount)
public function setErrorCounts($errorCount, $warningCount, $fixableErrorCount, $fixableWarningCount, $fixedErrorCount, $fixedWarningCount)
{
$this->errorCount = $errorCount;
$this->warningCount = $warningCount;
$this->fixableCount = $fixableCount;
$this->fixedCount = $fixedCount;
$this->errorCount = $errorCount;
$this->warningCount = $warningCount;
$this->fixableErrorCount = $fixableErrorCount;
$this->fixableWarningCount = $fixableWarningCount;
$this->fixedErrorCount = $fixedErrorCount;
$this->fixedWarningCount = $fixedWarningCount;

}//end setErrorCounts()

Expand Down
Loading
Loading