Skip to content

Commit 5fa0d0e

Browse files
committed
Improve error message for an invalid sniff code
1 parent 6f7c596 commit 5fa0d0e

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

src/Config.php

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -882,14 +882,7 @@ public function processLongArgument($arg, $pos)
882882
}
883883

884884
$sniffs = explode(',', substr($arg, 7));
885-
foreach ($sniffs as $sniff) {
886-
if (substr_count($sniff, '.') !== 2) {
887-
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
888-
$error .= $this->printShortUsage(true);
889-
throw new DeepExitException($error, 3);
890-
}
891-
}
892-
885+
$this->validateSniffCodes($sniffs, 'sniffs');
893886
$this->sniffs = $sniffs;
894887
self::$overriddenDefaults['sniffs'] = true;
895888
} else if (substr($arg, 0, 8) === 'exclude=') {
@@ -898,14 +891,7 @@ public function processLongArgument($arg, $pos)
898891
}
899892

900893
$sniffs = explode(',', substr($arg, 8));
901-
foreach ($sniffs as $sniff) {
902-
if (substr_count($sniff, '.') !== 2) {
903-
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
904-
$error .= $this->printShortUsage(true);
905-
throw new DeepExitException($error, 3);
906-
}
907-
}
908-
894+
$this->validateSniffCodes($sniffs, 'exclude');
909895
$this->exclude = $sniffs;
910896
self::$overriddenDefaults['exclude'] = true;
911897
} else if (defined('PHP_CODESNIFFER_IN_TESTS') === false
@@ -1742,4 +1728,47 @@ public function printConfigData($data)
17421728
}//end printConfigData()
17431729

17441730

1731+
/**
1732+
* Assert that all supplied sniff codes have the correct number of parts
1733+
*
1734+
* @param string[] $sniffs A list of sniffs supplied by the user, to be validated.
1735+
* @param string $argument The name of the argument which is being validated.
1736+
*
1737+
* @return void
1738+
* @throws DeepExitException
1739+
*/
1740+
private function validateSniffCodes($sniffs, $argument)
1741+
{
1742+
foreach ($sniffs as $sniff) {
1743+
$partCount = substr_count($sniff, '.');
1744+
if ($partCount === 2) {
1745+
// Correct number of parts.
1746+
continue;
1747+
}
1748+
1749+
$error = 'ERROR: The specified sniff code "'.$sniff.'" is invalid'.PHP_EOL.PHP_EOL;
1750+
1751+
if ($partCount === 0) {
1752+
$error .= 'This appears to be a Standard code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1753+
} else if ($partCount === 1) {
1754+
$error .= 'This appears to be a Category code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1755+
} else if ($partCount === 3) {
1756+
$error .= 'This appears to be a Message code, but the '.$argument.' option only supports sniff codes.'.PHP_EOL;
1757+
}
1758+
1759+
$error .= 'Sniff codes are in the form "Standard.Category.Sniff"'.PHP_EOL.PHP_EOL;
1760+
1761+
if ($partCount > 2) {
1762+
$parts = explode('.', $sniff, 4);
1763+
$error .= 'Perhaps try "'.$parts[0].'.'.$parts[1].'.'.$parts[2].'" instead.'.PHP_EOL.PHP_EOL;
1764+
}
1765+
1766+
$error .= $this->printShortUsage(true);
1767+
1768+
throw new DeepExitException($error, 3);
1769+
}//end foreach
1770+
1771+
}//end validateSniffCodes()
1772+
1773+
17451774
}//end class

0 commit comments

Comments
 (0)