File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 9
9
10
10
namespace PHP_CodeSniffer \Util ;
11
11
12
+ use InvalidArgumentException ;
12
13
use Phar ;
13
14
14
15
class Common
@@ -529,9 +530,15 @@ public static function suggestType($varType)
529
530
* @param string $sniffClass The fully qualified sniff class name.
530
531
*
531
532
* @return string
533
+ *
534
+ * @throws \InvalidArgumentException When $sniffClass is not a non-empty string.
532
535
*/
533
536
public static function getSniffCode ($ sniffClass )
534
537
{
538
+ if (is_string ($ sniffClass ) === false || $ sniffClass === '' ) {
539
+ throw new InvalidArgumentException ('The $sniffClass parameter must be a non-empty string ' );
540
+ }
541
+
535
542
$ parts = explode ('\\' , $ sniffClass );
536
543
$ sniff = array_pop ($ parts );
537
544
Original file line number Diff line number Diff line change @@ -21,6 +21,51 @@ final class GetSniffCodeTest extends TestCase
21
21
{
22
22
23
23
24
+ /**
25
+ * Test receiving an expected exception when the $sniffClass parameter is not passed a string value or is passed an empty string.
26
+ *
27
+ * @param string $input NOT a fully qualified sniff class name.
28
+ *
29
+ * @dataProvider dataGetSniffCodeThrowsExceptionOnInvalidInput
30
+ *
31
+ * @return void
32
+ */
33
+ public function testGetSniffCodeThrowsExceptionOnInvalidInput ($ input )
34
+ {
35
+ $ exception = 'InvalidArgumentException ' ;
36
+ $ message = 'The $sniffClass parameter must be a non-empty string ' ;
37
+
38
+ if (method_exists ($ this , 'expectException ' ) === true ) {
39
+ // PHPUnit 5+.
40
+ $ this ->expectException ($ exception );
41
+ $ this ->expectExceptionMessage ($ message );
42
+ } else {
43
+ // PHPUnit 4.
44
+ $ this ->setExpectedException ($ exception , $ message );
45
+ }
46
+
47
+ Common::getSniffCode ($ input );
48
+
49
+ }//end testGetSniffCodeThrowsExceptionOnInvalidInput()
50
+
51
+
52
+ /**
53
+ * Data provider.
54
+ *
55
+ * @see testGetSniffCodeThrowsExceptionOnInvalidInput()
56
+ *
57
+ * @return array<string, array<string>>
58
+ */
59
+ public static function dataGetSniffCodeThrowsExceptionOnInvalidInput ()
60
+ {
61
+ return [
62
+ 'Class name is not a string ' => [true ],
63
+ 'Class name is empty ' => ['' ],
64
+ ];
65
+
66
+ }//end dataGetSniffCodeThrowsExceptionOnInvalidInput()
67
+
68
+
24
69
/**
25
70
* Test transforming a sniff class name to a sniff code.
26
71
*
You can’t perform that action at this time.
0 commit comments