Skip to content

Commit 1665e40

Browse files
committed
Improve error message for an invalid sniff code
1 parent 05f8b21 commit 1665e40

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

src/Config.php

+45-16
Original file line numberDiff line numberDiff line change
@@ -886,14 +886,7 @@ public function processLongArgument($arg, $pos)
886886
}
887887

888888
$sniffs = explode(',', substr($arg, 7));
889-
foreach ($sniffs as $sniff) {
890-
if (substr_count($sniff, '.') !== 2) {
891-
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
892-
$error .= $this->printShortUsage(true);
893-
throw new DeepExitException($error, 3);
894-
}
895-
}
896-
889+
$this->validateSniffCodes($sniffs, 'sniffs');
897890
$this->sniffs = $sniffs;
898891
self::$overriddenDefaults['sniffs'] = true;
899892
} else if (substr($arg, 0, 8) === 'exclude=') {
@@ -902,14 +895,7 @@ public function processLongArgument($arg, $pos)
902895
}
903896

904897
$sniffs = explode(',', substr($arg, 8));
905-
foreach ($sniffs as $sniff) {
906-
if (substr_count($sniff, '.') !== 2) {
907-
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
908-
$error .= $this->printShortUsage(true);
909-
throw new DeepExitException($error, 3);
910-
}
911-
}
912-
898+
$this->validateSniffCodes($sniffs, 'exclude');
913899
$this->exclude = $sniffs;
914900
self::$overriddenDefaults['exclude'] = true;
915901
} else if (defined('PHP_CODESNIFFER_IN_TESTS') === false
@@ -1658,4 +1644,47 @@ public function printConfigData($data)
16581644
}//end printConfigData()
16591645

16601646

1647+
/**
1648+
* Assert that all supplied sniff codes have the correct number of parts
1649+
*
1650+
* @param string[] $sniffs A list of sniffs supplied by the user, to be validated.
1651+
* @param string $argument The name of the argument which is being validated.
1652+
*
1653+
* @return void
1654+
* @throws DeepExitException
1655+
*/
1656+
private function validateSniffCodes($sniffs, $argument)
1657+
{
1658+
foreach ($sniffs as $sniff) {
1659+
$partCount = substr_count($sniff, '.');
1660+
if ($partCount === 2) {
1661+
// Correct number of parts.
1662+
continue;
1663+
}
1664+
1665+
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
1666+
1667+
if ($partCount === 0) {
1668+
$error .= 'This appears to be a Standard code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1669+
} else if ($partCount === 1) {
1670+
$error .= 'This appears to be a Category code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1671+
} else if ($partCount === 3) {
1672+
$error .= 'This appears to be a Message code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1673+
}
1674+
1675+
$error .= 'Sniff codes are in the form "Standard.Category.Sniff"'.PHP_EOL.PHP_EOL;
1676+
1677+
if ($partCount > 2) {
1678+
$parts = explode('.', $sniff, 4);
1679+
$error .= 'Perhaps try "'.$parts[0].'.'.$parts[1].'.'.$parts[2].'" instead.'.PHP_EOL.PHP_EOL;
1680+
}
1681+
1682+
$error .= $this->printShortUsage(true);
1683+
1684+
throw new DeepExitException($error, 3);
1685+
}//end foreach
1686+
1687+
}//end validateSniffCodes()
1688+
1689+
16611690
}//end class

0 commit comments

Comments
 (0)