Skip to content

Commit

Permalink
Improve error message for an invalid sniff code
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Feb 16, 2024
1 parent 6f7c596 commit 5fa0d0e
Showing 1 changed file with 45 additions and 16 deletions.
61 changes: 45 additions & 16 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,14 +882,7 @@ public function processLongArgument($arg, $pos)
}

$sniffs = explode(',', substr($arg, 7));
foreach ($sniffs as $sniff) {
if (substr_count($sniff, '.') !== 2) {
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
}
}

$this->validateSniffCodes($sniffs, 'sniffs');
$this->sniffs = $sniffs;
self::$overriddenDefaults['sniffs'] = true;
} else if (substr($arg, 0, 8) === 'exclude=') {
Expand All @@ -898,14 +891,7 @@ public function processLongArgument($arg, $pos)
}

$sniffs = explode(',', substr($arg, 8));
foreach ($sniffs as $sniff) {
if (substr_count($sniff, '.') !== 2) {
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
}
}

$this->validateSniffCodes($sniffs, 'exclude');
$this->exclude = $sniffs;
self::$overriddenDefaults['exclude'] = true;
} else if (defined('PHP_CODESNIFFER_IN_TESTS') === false
Expand Down Expand Up @@ -1742,4 +1728,47 @@ public function printConfigData($data)
}//end printConfigData()


/**
* Assert that all supplied sniff codes have the correct number of parts
*
* @param string[] $sniffs A list of sniffs supplied by the user, to be validated.
* @param string $argument The name of the argument which is being validated.
*
* @return void
* @throws DeepExitException
*/
private function validateSniffCodes($sniffs, $argument)
{
foreach ($sniffs as $sniff) {
$partCount = substr_count($sniff, '.');
if ($partCount === 2) {
// Correct number of parts.
continue;
}

$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;

if ($partCount === 0) {
$error .= 'This appears to be a Standard code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
} else if ($partCount === 1) {
$error .= 'This appears to be a Category code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
} else if ($partCount === 3) {
$error .= 'This appears to be a Message code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
}

$error .= 'Sniff codes are in the form "Standard.Category.Sniff"'.PHP_EOL.PHP_EOL;

if ($partCount > 2) {
$parts = explode('.', $sniff, 4);
$error .= 'Perhaps try "'.$parts[0].'.'.$parts[1].'.'.$parts[2].'" instead.'.PHP_EOL.PHP_EOL;
}

$error .= $this->printShortUsage(true);

throw new DeepExitException($error, 3);
}//end foreach

}//end validateSniffCodes()


}//end class

0 comments on commit 5fa0d0e

Please sign in to comment.