Skip to content

Commit 02d60fe

Browse files
committed
Ruleset: use MsgCollector for "invalid type" message
Includes updating the message to mention the reference for which the `type` was (incorrectly) being changed. This should make it more straight forward for ruleset maintainers to find the problem in their ruleset. It also makes the message more unique, as this message could occur in multiple places in a ruleset and there was no indication of that in the message previously. This commit will be easiest to review while ignoring whitespace differences. Potential future scope It could be considered to downgrade this message from an `ERROR` to a `NOTICE` as an invalid type is not blocking for running the sniffs, though this could lead to results not being as expected if, for instance, the `-n` flag is being used, which is why I've not changed this at this time.
1 parent e73e562 commit 02d60fe

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/Ruleset.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,18 +1130,19 @@ private function processRule($rule, $newSniffs, $depth=0)
11301130

11311131
$type = strtolower((string) $rule->type);
11321132
if ($type !== 'error' && $type !== 'warning') {
1133-
throw new RuntimeException("ERROR: Message type \"$type\" is invalid; must be \"error\" or \"warning\"");
1134-
}
1133+
$message = "Message type \"$type\" for \"$code\" is invalid; must be \"error\" or \"warning\".";
1134+
$this->msgCache->add($message, MsgCollector::ERROR);
1135+
} else {
1136+
$this->ruleset[$code]['type'] = $type;
1137+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1138+
echo str_repeat("\t", $depth);
1139+
echo "\t\t=> message type set to ".(string) $rule->type;
1140+
if ($code !== $ref) {
1141+
echo " for $code";
1142+
}
11351143

1136-
$this->ruleset[$code]['type'] = $type;
1137-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1138-
echo str_repeat("\t", $depth);
1139-
echo "\t\t=> message type set to ".(string) $rule->type;
1140-
if ($code !== $ref) {
1141-
echo " for $code";
1144+
echo PHP_EOL;
11421145
}
1143-
1144-
echo PHP_EOL;
11451146
}
11461147
}//end if
11471148

tests/Core/Ruleset/ProcessRuleInvalidTypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class ProcessRuleInvalidTypeTest extends AbstractRulesetTestCase
2323

2424

2525
/**
26-
* Test displaying an informative error message when an invalid type is given.
26+
* Test displaying an error when an invalid type is given.
2727
*
2828
* @return void
2929
*/
@@ -32,7 +32,7 @@ public function testInvalidTypeHandling()
3232
$standard = __DIR__.'/ProcessRuleInvalidTypeTest.xml';
3333
$config = new ConfigDouble(["--standard=$standard"]);
3434

35-
$message = 'ERROR: Message type "notice" is invalid; must be "error" or "warning"';
35+
$message = 'ERROR: Message type "notice" for "Generic.Files.ByteOrderMark" is invalid; must be "error" or "warning".'.PHP_EOL.PHP_EOL;
3636
$this->expectRuntimeExceptionMessage($message);
3737

3838
new Ruleset($config);

0 commit comments

Comments
 (0)