Skip to content

Commit

Permalink
Common::getSniffCode(): throw exception on invalid input [2b]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Sep 29, 2024
1 parent 1719a66 commit 2aac9ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Util/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public static function getSniffCode($sniffClass)
$parts = explode('\\', $sniffClass);
if (count($parts) < 4) {
throw new InvalidArgumentException(
'The $sniffClass parameter was not passed a fully qualified sniff class name. Received: '.$sniffClass
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
);
}

Expand All @@ -552,9 +552,13 @@ public static function getSniffCode($sniffClass)
if (substr($sniff, -5) === 'Sniff') {
// Sniff class name.
$sniff = substr($sniff, 0, -5);
} else {
} else if (substr($sniff, -8) === 'UnitTest') {
// Unit test class name.
$sniff = substr($sniff, 0, -8);
} else {
throw new InvalidArgumentException(
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
);
}

$category = array_pop($parts);
Expand Down
7 changes: 4 additions & 3 deletions tests/Core/Util/Common/GetSniffCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function dataGetSniffCodeThrowsExceptionOnInvalidInput()
public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass($input)
{
$exception = 'InvalidArgumentException';
$message = 'The $sniffClass parameter was not passed a fully qualified sniff class name. Received:';
$message = 'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received:';

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

}//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
Expand Down

0 comments on commit 2aac9ad

Please sign in to comment.