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 May 14, 2024
1 parent 05f8b21 commit 1665e40
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 @@ -886,14 +886,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 @@ -902,14 +895,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 @@ -1658,4 +1644,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 1665e40

Please sign in to comment.