Skip to content

Commit 3992a5f

Browse files
committed
Common::getSniffCode(): throw exception on invalid input [2b]
Previously, if an invalid class name was passed, which didn't end on `Sniff` or `UnitTest`, the `Common::getSniffCode()` method would return a garbled name, like `Fully.Qualified.C`, which is just confusing. This commit changes the behaviour to throw an `InvalidArgumentException` instead. Includes test.
1 parent 1df48df commit 3992a5f

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/Util/Common.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ public static function getSniffCode($sniffClass)
627627
$parts = explode('\\', $sniffClass);
628628
if (count($parts) < 4) {
629629
throw new InvalidArgumentException(
630-
'The $sniffClass parameter was not passed a fully qualified sniff class name. Received: '.$sniffClass
630+
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
631631
);
632632
}
633633

@@ -636,9 +636,13 @@ public static function getSniffCode($sniffClass)
636636
if (substr($sniff, -5) === 'Sniff') {
637637
// Sniff class name.
638638
$sniff = substr($sniff, 0, -5);
639-
} else {
639+
} else if (substr($sniff, -8) === 'UnitTest') {
640640
// Unit test class name.
641641
$sniff = substr($sniff, 0, -8);
642+
} else {
643+
throw new InvalidArgumentException(
644+
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
645+
);
642646
}
643647

644648
$category = array_pop($parts);

tests/Core/Util/Common/GetSniffCodeTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function dataGetSniffCodeThrowsExceptionOnInvalidInput()
7979
public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass($input)
8080
{
8181
$exception = 'InvalidArgumentException';
82-
$message = 'The $sniffClass parameter was not passed a fully qualified sniff class name. Received:';
82+
$message = 'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received:';
8383

8484
if (method_exists($this, 'expectException') === true) {
8585
// PHPUnit 5+.
@@ -105,8 +105,9 @@ public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass(
105105
public static function dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
106106
{
107107
return [
108-
'Unqualified class name' => ['ClassName'],
109-
'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'],
108+
'Unqualified class name' => ['ClassName'],
109+
'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'],
110+
'Fully qualified class name, doesn\'t end on Sniff or UnitTest' => ['Fully\\Sniffs\\Qualified\\ClassName'],
110111
];
111112

112113
}//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()

0 commit comments

Comments
 (0)