@@ -886,14 +886,7 @@ public function processLongArgument($arg, $pos)
886
886
}
887
887
888
888
$ 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 ' );
897
890
$ this ->sniffs = $ sniffs ;
898
891
self ::$ overriddenDefaults ['sniffs ' ] = true ;
899
892
} else if (substr ($ arg , 0 , 8 ) === 'exclude= ' ) {
@@ -902,14 +895,7 @@ public function processLongArgument($arg, $pos)
902
895
}
903
896
904
897
$ 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 ' );
913
899
$ this ->exclude = $ sniffs ;
914
900
self ::$ overriddenDefaults ['exclude ' ] = true ;
915
901
} else if (defined ('PHP_CODESNIFFER_IN_TESTS ' ) === false
@@ -1658,4 +1644,47 @@ public function printConfigData($data)
1658
1644
}//end printConfigData()
1659
1645
1660
1646
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
+
1661
1690
}//end class
0 commit comments