Skip to content
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

Add maxWarnings config option to allow zero-code exit if number of violations is below a threshold #3389

Open
wants to merge 1 commit into
base: master
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
13 changes: 13 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
* @property bool $stdin Read content from STDIN instead of supplied files.
* @property string $stdinContent Content passed directly to PHPCS on STDIN.
* @property string $stdinPath The path to use for content passed on STDIN.
* @property int $maxWarnings Maximum number of warnings or errors to forgive and allow zero-code exit.
* E.g. if maxWarnings is 50 and there are 50 errors or warnings, the exit
* code will be 0. Helpful for legacy projects.
*
* @property array<string, string> $extensions File extensions that should be checked, and what tokenizer to use.
* E.g., array('inc' => 'PHP');
Expand Down Expand Up @@ -135,6 +138,7 @@ class Config
'stdin' => null,
'stdinContent' => null,
'stdinPath' => null,
'maxWarnings' => null,
'unknown' => null,
];

Expand Down Expand Up @@ -502,6 +506,7 @@ public function restoreDefaults()
$this->stdin = false;
$this->stdinContent = null;
$this->stdinPath = null;
$this->maxWarnings = 0;
$this->unknown = [];

$standard = self::getConfigData('default_standard');
Expand Down Expand Up @@ -1200,6 +1205,13 @@ public function processLongArgument($arg, $pos)

$this->tabWidth = (int) substr($arg, 10);
self::$overriddenDefaults['tabWidth'] = true;
} else if (substr($arg, 0, 13) === 'max-warnings=') {
if (isset(self::$overriddenDefaults['maxWarnings']) === true) {
break;
}

$this->maxWarnings = (int) substr($arg, 13);
self::$overriddenDefaults['maxWarnings'] = true;
} else {
if ($this->dieOnUnknownArg === false) {
$eqPos = strpos($arg, '=');
Expand Down Expand Up @@ -1378,6 +1390,7 @@ public function printPHPCSUsage()
echo ' --cache Cache results between runs'.PHP_EOL;
echo ' --no-cache Do not cache results between runs (this is the default)'.PHP_EOL;
echo ' --ignore-annotations Ignore all phpcs: annotations in code comments'.PHP_EOL;
echo ' --max-warnings Maximum number of warnings or errors to forgive and allow zero-code exit'.PHP_EOL;
echo PHP_EOL;
echo ' <cacheFile> Use a specific file for caching (uses a temporary file by default)'.PHP_EOL;
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL;
Expand Down
7 changes: 5 additions & 2 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ public function runPHPCS()
return $e->getCode();
}//end try

if ($numErrors === 0) {
// No errors found.
echo "Total violations found: $numErrors".PHP_EOL.PHP_EOL;

if ($numErrors <= $this->config->maxWarnings) {
// Number of warnings or errors found is below or equal to the maxWarnings threshold
// (which is zero by default).
return 0;
} else if ($this->reporter->totalFixable === 0) {
// Errors found, but none of them can be fixed by PHPCBF.
Expand Down