Skip to content

Commit 7ba0a48

Browse files
committed
Use new exit codes for DeepExitExceptions
Includes updating the documentation of the `DeepExitException` class.
1 parent cfd45b1 commit 7ba0a48

File tree

8 files changed

+55
-42
lines changed

8 files changed

+55
-42
lines changed

src/Config.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHP_CodeSniffer\Exceptions\DeepExitException;
1818
use PHP_CodeSniffer\Exceptions\RuntimeException;
1919
use PHP_CodeSniffer\Util\Common;
20+
use PHP_CodeSniffer\Util\ExitCode;
2021
use PHP_CodeSniffer\Util\Help;
2122
use PHP_CodeSniffer\Util\Standards;
2223

@@ -678,10 +679,10 @@ public function processShortArgument($arg, $pos)
678679
case 'h':
679680
case '?':
680681
$this->printUsage();
681-
throw new DeepExitException('', 0);
682+
throw new DeepExitException('', ExitCode::OKAY);
682683
case 'i' :
683684
$output = Standards::prepareInstalledStandardsForDisplay().PHP_EOL;
684-
throw new DeepExitException($output, 0);
685+
throw new DeepExitException($output, ExitCode::OKAY);
685686
case 'v' :
686687
if ($this->quiet === true) {
687688
// Ignore when quiet mode is enabled.
@@ -747,7 +748,7 @@ public function processShortArgument($arg, $pos)
747748
if ($changed === false && ini_get($ini[0]) !== $ini[1]) {
748749
$error = sprintf('ERROR: Ini option "%s" cannot be changed at runtime.', $ini[0]).PHP_EOL;
749750
$error .= $this->printShortUsage(true);
750-
throw new DeepExitException($error, 3);
751+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
751752
}
752753
break;
753754
case 'n' :
@@ -789,11 +790,11 @@ public function processLongArgument($arg, $pos)
789790
switch ($arg) {
790791
case 'help':
791792
$this->printUsage();
792-
throw new DeepExitException('', 0);
793+
throw new DeepExitException('', ExitCode::OKAY);
793794
case 'version':
794795
$output = 'PHP_CodeSniffer version '.self::VERSION.' ('.self::STABILITY.') ';
795796
$output .= 'by Squiz and PHPCSStandards'.PHP_EOL;
796-
throw new DeepExitException($output, 0);
797+
throw new DeepExitException($output, ExitCode::OKAY);
797798
case 'colors':
798799
if (isset($this->overriddenDefaults['colors']) === true) {
799800
break;
@@ -840,7 +841,7 @@ public function processLongArgument($arg, $pos)
840841
) {
841842
$error = 'ERROR: Setting a config option requires a name and value'.PHP_EOL.PHP_EOL;
842843
$error .= $this->printShortUsage(true);
843-
throw new DeepExitException($error, 3);
844+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
844845
}
845846

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

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

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

884885
$output .= "Config value \"$key\" removed successfully; old value was \"$current\"".PHP_EOL;
885886
}
886-
throw new DeepExitException($output, 0);
887+
throw new DeepExitException($output, ExitCode::OKAY);
887888
case 'config-show':
888889
$data = self::getAllConfigData();
889890
$output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
890891
$output .= $this->prepareConfigDataForDisplay($data);
891-
throw new DeepExitException($output, 0);
892+
throw new DeepExitException($output, ExitCode::OKAY);
892893
case 'runtime-set':
893894
if (isset($this->cliArgs[($pos + 1)]) === false
894895
|| isset($this->cliArgs[($pos + 2)]) === false
895896
) {
896897
$error = 'ERROR: Setting a runtime config option requires a name and value'.PHP_EOL.PHP_EOL;
897898
$error .= $this->printShortUsage(true);
898-
throw new DeepExitException($error, 3);
899+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
899900
}
900901

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

952953
if ($dir === '.') {
@@ -972,7 +973,7 @@ public function processLongArgument($arg, $pos)
972973
if (is_dir($this->cacheFile) === true) {
973974
$error = 'ERROR: The specified cache file path "'.$this->cacheFile.'" is a directory'.PHP_EOL.PHP_EOL;
974975
$error .= $this->printShortUsage(true);
975-
throw new DeepExitException($error, 3);
976+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
976977
}
977978
} else if (substr($arg, 0, 10) === 'bootstrap=') {
978979
$files = explode(',', substr($arg, 10));
@@ -982,7 +983,7 @@ public function processLongArgument($arg, $pos)
982983
if ($path === false) {
983984
$error = 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL;
984985
$error .= $this->printShortUsage(true);
985-
throw new DeepExitException($error, 3);
986+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
986987
}
987988

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

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

10441045
$this->reportFile = $dir.'/'.basename($this->reportFile);
@@ -1049,7 +1050,7 @@ public function processLongArgument($arg, $pos)
10491050
if (is_dir($this->reportFile) === true) {
10501051
$error = 'ERROR: The specified report file path "'.$this->reportFile.'" is a directory'.PHP_EOL.PHP_EOL;
10511052
$error .= $this->printShortUsage(true);
1052-
throw new DeepExitException($error, 3);
1053+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
10531054
}
10541055
} else if (substr($arg, 0, 13) === 'report-width=') {
10551056
if (isset($this->overriddenDefaults['reportWidth']) === true) {
@@ -1080,7 +1081,7 @@ public function processLongArgument($arg, $pos)
10801081
if (is_dir($this->basepath) === false) {
10811082
$error = 'ERROR: The specified basepath "'.$this->basepath.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
10821083
$error .= $this->printShortUsage(true);
1083-
throw new DeepExitException($error, 3);
1084+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
10841085
}
10851086
} else if ((substr($arg, 0, 7) === 'report=' || substr($arg, 0, 7) === 'report-')) {
10861087
$reports = [];
@@ -1101,15 +1102,15 @@ public function processLongArgument($arg, $pos)
11011102
if (is_dir($dir) === false) {
11021103
$error = 'ERROR: The specified '.$report.' report file path "'.$output.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
11031104
$error .= $this->printShortUsage(true);
1104-
throw new DeepExitException($error, 3);
1105+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
11051106
}
11061107

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

11091110
if (is_dir($output) === true) {
11101111
$error = 'ERROR: The specified '.$report.' report file path "'.$output.'" is a directory'.PHP_EOL.PHP_EOL;
11111112
$error .= $this->printShortUsage(true);
1112-
throw new DeepExitException($error, 3);
1113+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
11131114
}
11141115
}//end if
11151116
}//end if
@@ -1167,7 +1168,7 @@ public function processLongArgument($arg, $pos)
11671168
$error .= 'PHP_CodeSniffer >= 4.0 only supports scanning PHP files.'.PHP_EOL;
11681169
$error .= 'Received: '.substr($arg, 11).PHP_EOL.PHP_EOL;
11691170
$error .= $this->printShortUsage(true);
1170-
throw new DeepExitException($error, 3);
1171+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
11711172
}
11721173
}
11731174

@@ -1258,7 +1259,7 @@ public function processLongArgument($arg, $pos)
12581259
$validOptions
12591260
);
12601261
$error .= $this->printShortUsage(true);
1261-
throw new DeepExitException($error, 3);
1262+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
12621263
}
12631264

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

13851386
$error .= PHP_EOL;
13861387
$error .= $this->printShortUsage(true);
1387-
throw new DeepExitException(ltrim($error), 3);
1388+
throw new DeepExitException(ltrim($error), ExitCode::PROCESS_ERROR);
13881389
}
13891390

13901391
return $sniffs;
@@ -1413,7 +1414,7 @@ public function processUnknownArgument($arg, $pos)
14131414

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

14191420
$this->processFilePath($arg);
@@ -1444,7 +1445,7 @@ public function processFilePath($path)
14441445

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

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

16741675
if (file_put_contents($configFile, $output) === false) {
16751676
$error = 'ERROR: Config file '.$configFile.' could not be written'.PHP_EOL.PHP_EOL;
1676-
throw new DeepExitException($error, 3);
1677+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
16771678
}
16781679

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

17321733
if (Common::isReadable($configFile) === false) {
17331734
$error = 'ERROR: Config file '.$configFile.' is not readable'.PHP_EOL.PHP_EOL;
1734-
throw new DeepExitException($error, 3);
1735+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
17351736
}
17361737

17371738
include $configFile;

src/Exceptions/DeepExitException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
* Allows the runner to return an exit code instead of putting exit codes elsewhere
66
* in the source code.
77
*
8+
* Exit codes passed to this exception (as the `$code` parameter) MUST be one of the
9+
* predefined exit code constants per the `PHP_CodeSniffer\Util\ExitCode` class; or a bitmask sum of those.
10+
*
811
* @author Greg Sherwood <[email protected]>
12+
* @author Juliette Reinders Folmer <[email protected]>
913
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
14+
* @copyright 2025 PHPCSStandards and contributors
1015
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
1116
*/
1217

src/Files/FileList.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHP_CodeSniffer\Exceptions\DeepExitException;
2020
use PHP_CodeSniffer\Ruleset;
2121
use PHP_CodeSniffer\Util\Common;
22+
use PHP_CodeSniffer\Util\ExitCode;
2223
use RecursiveArrayIterator;
2324
use RecursiveDirectoryIterator;
2425
use RecursiveIteratorIterator;
@@ -157,7 +158,7 @@ private function getFilterClass()
157158
$filename = realpath($filterType);
158159
if ($filename === false) {
159160
$error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL;
160-
throw new DeepExitException($error, 3);
161+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
161162
}
162163

163164
$filterClass = Autoload::loadFile($filename);

src/Reporter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHP_CodeSniffer\Files\File;
1515
use PHP_CodeSniffer\Reports\Report;
1616
use PHP_CodeSniffer\Util\Common;
17+
use PHP_CodeSniffer\Util\ExitCode;
1718

1819
class Reporter
1920
{
@@ -103,7 +104,7 @@ public function __construct(Config $config)
103104
$filename = realpath($type);
104105
if ($filename === false) {
105106
$error = "ERROR: Custom report \"$type\" not found".PHP_EOL;
106-
throw new DeepExitException($error, 3);
107+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
107108
}
108109

109110
$reportClassName = Autoload::loadFile($filename);
@@ -132,7 +133,7 @@ public function __construct(Config $config)
132133

133134
if ($reportClassName === '') {
134135
$error = "ERROR: Class file for report \"$type\" not found".PHP_EOL;
135-
throw new DeepExitException($error, 3);
136+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
136137
}
137138

138139
$reportClass = new $reportClassName();

src/Reports/Gitblame.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHP_CodeSniffer\Reports;
1212

1313
use PHP_CodeSniffer\Exceptions\DeepExitException;
14+
use PHP_CodeSniffer\Util\ExitCode;
1415

1516
class Gitblame extends VersionControl
1617
{
@@ -74,7 +75,7 @@ protected function getBlameContent($filename)
7475
$handle = popen($command, 'r');
7576
if ($handle === false) {
7677
$error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
77-
throw new DeepExitException($error, 3);
78+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
7879
}
7980

8081
$rawContent = stream_get_contents($handle);

src/Reports/Hgblame.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHP_CodeSniffer\Reports;
1212

1313
use PHP_CodeSniffer\Exceptions\DeepExitException;
14+
use PHP_CodeSniffer\Util\ExitCode;
1415

1516
class Hgblame extends VersionControl
1617
{
@@ -86,14 +87,14 @@ protected function getBlameContent($filename)
8687
chdir($location);
8788
} else {
8889
$error = 'ERROR: Could not locate .hg directory '.PHP_EOL.PHP_EOL;
89-
throw new DeepExitException($error, 3);
90+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
9091
}
9192

9293
$command = 'hg blame -u -d -v "'.$filename.'" 2>&1';
9394
$handle = popen($command, 'r');
9495
if ($handle === false) {
9596
$error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
96-
throw new DeepExitException($error, 3);
97+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
9798
}
9899

99100
$rawContent = stream_get_contents($handle);

src/Reports/Svnblame.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHP_CodeSniffer\Reports;
1111

1212
use PHP_CodeSniffer\Exceptions\DeepExitException;
13+
use PHP_CodeSniffer\Util\ExitCode;
1314

1415
class Svnblame extends VersionControl
1516
{
@@ -57,7 +58,7 @@ protected function getBlameContent($filename)
5758
$handle = popen($command, 'r');
5859
if ($handle === false) {
5960
$error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
60-
throw new DeepExitException($error, 3);
61+
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
6162
}
6263

6364
$rawContent = stream_get_contents($handle);

0 commit comments

Comments
 (0)