Skip to content

Commit 1719a66

Browse files
committed
Common::getSniffCode(): throw exception on invalid input [2a]
Previously, if an invalid (incomplete) class name was passed, the `Common::getSniffCode()` method would return a garbled name, like `.Qualified.C`, which is just confusing. This commit changes the behaviour to throw an `InvalidArgumentException` instead. Includes test.
1 parent 5fac554 commit 1719a66

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/Util/Common.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ public static function suggestType($varType)
532532
* @return string
533533
*
534534
* @throws \InvalidArgumentException When $sniffClass is not a non-empty string.
535+
* @throws \InvalidArgumentException When $sniffClass is not a FQN for a sniff(test) class.
535536
*/
536537
public static function getSniffCode($sniffClass)
537538
{
@@ -540,6 +541,12 @@ public static function getSniffCode($sniffClass)
540541
}
541542

542543
$parts = explode('\\', $sniffClass);
544+
if (count($parts) < 4) {
545+
throw new InvalidArgumentException(
546+
'The $sniffClass parameter was not passed a fully qualified sniff class name. Received: '.$sniffClass
547+
);
548+
}
549+
543550
$sniff = array_pop($parts);
544551

545552
if (substr($sniff, -5) === 'Sniff') {

tests/Core/Util/Common/GetSniffCodeTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,52 @@ public static function dataGetSniffCodeThrowsExceptionOnInvalidInput()
6666
}//end dataGetSniffCodeThrowsExceptionOnInvalidInput()
6767

6868

69+
/**
70+
* Test receiving an expected exception when the $sniffClass parameter is not passed a value which
71+
* could be a fully qualified sniff(test) class name.
72+
*
73+
* @param string $input String input which can not be a fully qualified sniff(test) class name.
74+
*
75+
* @dataProvider dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass
76+
*
77+
* @return void
78+
*/
79+
public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass($input)
80+
{
81+
$exception = 'InvalidArgumentException';
82+
$message = 'The $sniffClass parameter was not passed a fully qualified sniff class name. Received:';
83+
84+
if (method_exists($this, 'expectException') === true) {
85+
// PHPUnit 5+.
86+
$this->expectException($exception);
87+
$this->expectExceptionMessage($message);
88+
} else {
89+
// PHPUnit 4.
90+
$this->setExpectedException($exception, $message);
91+
}
92+
93+
Common::getSniffCode($input);
94+
95+
}//end testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
96+
97+
98+
/**
99+
* Data provider.
100+
*
101+
* @see testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
102+
*
103+
* @return array<string, array<string>>
104+
*/
105+
public static function dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
106+
{
107+
return [
108+
'Unqualified class name' => ['ClassName'],
109+
'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'],
110+
];
111+
112+
}//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
113+
114+
69115
/**
70116
* Test transforming a sniff class name to a sniff code.
71117
*

0 commit comments

Comments
 (0)