Skip to content

Commit 5fac554

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 9b837dd commit 5fac554

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
@@ -873,9 +873,13 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
873873
$parts = explode('.', $code);
874874
if ($parts[0] === 'Internal') {
875875
// An internal message.
876-
$listenerCode = Common::getSniffCode($this->activeListener);
877-
$sniffCode = $code;
878-
$checkCodes = [$sniffCode];
876+
$listenerCode = '';
877+
if ($this->activeListener !== '') {
878+
$listenerCode = Common::getSniffCode($this->activeListener);
879+
}
880+
881+
$sniffCode = $code;
882+
$checkCodes = [$sniffCode];
879883
} else {
880884
if ($parts[0] !== $code) {
881885
// 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
@@ -529,9 +530,15 @@ public static function suggestType($varType)
529530
* @param string $sniffClass The fully qualified sniff class name.
530531
*
531532
* @return string
533+
*
534+
* @throws \InvalidArgumentException When $sniffClass is not a non-empty string.
532535
*/
533536
public static function getSniffCode($sniffClass)
534537
{
538+
if (is_string($sniffClass) === false || $sniffClass === '') {
539+
throw new InvalidArgumentException('The $sniffClass parameter must be a non-empty string');
540+
}
541+
535542
$parts = explode('\\', $sniffClass);
536543
$sniff = array_pop($parts);
537544

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)