Skip to content

Commit 52a5e7e

Browse files
committed
Common::getSniffCode(): throw exception on invalid input [1]
Previously, if an empty string was passed, the `Common::getSniffCode()` method would return `..`, which is just confusing (and incorrect). This commit changes the behaviour to throw an `InvalidArgumentException` instead. Includes making a potentially superfluous function call to the method conditionally (as it could hit the new exception). Includes test.
1 parent 70a5ae5 commit 52a5e7e

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/Files/File.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,13 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
820820
$parts = explode('.', $code);
821821
if ($parts[0] === 'Internal') {
822822
// An internal message.
823-
$listenerCode = Common::getSniffCode($this->activeListener);
824-
$sniffCode = $code;
825-
$checkCodes = [$sniffCode];
823+
$listenerCode = '';
824+
if ($this->activeListener !== '') {
825+
$listenerCode = Common::getSniffCode($this->activeListener);
826+
}
827+
828+
$sniffCode = $code;
829+
$checkCodes = [$sniffCode];
826830
} else {
827831
if ($parts[0] !== $code) {
828832
// The full message code has been passed in.

src/Util/Common.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Util;
1111

12+
use InvalidArgumentException;
1213
use Phar;
1314

1415
class Common
@@ -613,9 +614,15 @@ public static function suggestType($varType)
613614
* @param string $sniffClass The fully qualified sniff class name.
614615
*
615616
* @return string
617+
*
618+
* @throws \InvalidArgumentException When $sniffClass is not a non-empty string.
616619
*/
617620
public static function getSniffCode($sniffClass)
618621
{
622+
if (is_string($sniffClass) === false || $sniffClass === '') {
623+
throw new InvalidArgumentException('The $sniffClass parameter must be a non-empty string');
624+
}
625+
619626
$parts = explode('\\', $sniffClass);
620627
$sniff = array_pop($parts);
621628

tests/Core/Util/Common/GetSniffCodeTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,51 @@ final class GetSniffCodeTest extends TestCase
2121
{
2222

2323

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+
2469
/**
2570
* Test transforming a sniff class name to a sniff code.
2671
*

0 commit comments

Comments
 (0)