@@ -882,14 +882,7 @@ public function processLongArgument($arg, $pos)
882
882
}
883
883
884
884
$ 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 ' );
893
886
$ this ->sniffs = $ sniffs ;
894
887
self ::$ overriddenDefaults ['sniffs ' ] = true ;
895
888
} else if (substr ($ arg , 0 , 8 ) === 'exclude= ' ) {
@@ -898,14 +891,7 @@ public function processLongArgument($arg, $pos)
898
891
}
899
892
900
893
$ 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 ' );
909
895
$ this ->exclude = $ sniffs ;
910
896
self ::$ overriddenDefaults ['exclude ' ] = true ;
911
897
} else if (defined ('PHP_CODESNIFFER_IN_TESTS ' ) === false
@@ -1742,4 +1728,47 @@ public function printConfigData($data)
1742
1728
}//end printConfigData()
1743
1729
1744
1730
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
+
1745
1774
}//end class
0 commit comments