Skip to content

Commit a28f133

Browse files
committed
Ruleset: use MsgCollector for "register() method must return an array" error
Includes some new assertions which won't run until the test suite supports PHPUnit 10+ (PHPCS 4.0). These tests belong with this commit though, so adding them now anyway.
1 parent b100764 commit a28f133

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/Ruleset.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,8 +1460,12 @@ public function populateTokenListeners()
14601460

14611461
$tokens = $this->sniffs[$sniffClass]->register();
14621462
if (is_array($tokens) === false) {
1463-
$msg = "ERROR: Sniff $sniffClass register() method must return an array";
1464-
throw new RuntimeException($msg);
1463+
$msg = "The sniff $sniffClass register() method must return an array.";
1464+
$this->msgCache->add($msg, MsgCollector::ERROR);
1465+
1466+
// Unregister the sniff.
1467+
unset($this->sniffs[$sniffClass], $this->sniffCodes[$sniffCode], $this->deprecatedSniffs[$sniffCode]);
1468+
continue;
14651469
}
14661470

14671471
$ignorePatterns = [];

tests/Core/Ruleset/PopulateTokenListenersRegisterNoArrayTest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55

66
<rule ref="TestStandard.InvalidSniffs.RegisterNoArray"/>
77

8+
<!-- Prevent a "no sniff were registered" error. -->
9+
<rule ref="Generic.PHP.BacktickOperator"/>
810
</ruleset>

tests/Core/Ruleset/PopulateTokenListenersTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ public function testSniffWhereRegisterDoesNotReturnAnArrayThrowsException()
6262
$config = new ConfigDouble(["--standard=$standard"]);
6363

6464
$sniffClass = 'Fixtures\\TestStandard\\Sniffs\\InvalidSniffs\\RegisterNoArraySniff';
65-
$message = "ERROR: Sniff $sniffClass register() method must return an array";
65+
$message = "ERROR: The sniff $sniffClass register() method must return an array.".PHP_EOL.PHP_EOL;
6666
$this->expectRuntimeExceptionMessage($message);
6767

6868
new Ruleset($config);
6969

70+
// Verify that the sniff has not been registered/has been unregistered.
71+
// These assertions will only take effect for PHPUnit 10+.
72+
$this->assertArrayNotHasKey($sniffClass, self::$ruleset->sniffs, "Sniff class $sniffClass is listed in registered sniffs");
73+
$this->assertArrayNotHasKey('TestStandard.InvalidSniffs.RegisterNoArray', self::$ruleset->sniffCodes, 'Sniff code is registered');
74+
7075
}//end testSniffWhereRegisterDoesNotReturnAnArrayThrowsException()
7176

7277

0 commit comments

Comments
 (0)