Skip to content

Commit 45baf5c

Browse files
committed
Ruleset: use MessageCollector for pre-existing errors
Notes: **For the "invalid type" message** Includes updating the message for the "invalid type" 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. Potential future scope for "invalid type" message 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. **For the "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. **For the "setting non-existent property" error Includes minor adjustment to the error message (removal of "Ruleset invalid" and punctuation).
1 parent c1db8c8 commit 45baf5c

8 files changed

+45
-31
lines changed

src/Ruleset.php

+28-23
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ public function __construct(Config $config)
202202

203203
if (defined('PHP_CODESNIFFER_IN_TESTS') === true && empty($restrictions) === false) {
204204
// In unit tests, only register the sniffs that the test wants and not the entire standard.
205-
try {
206-
foreach ($restrictions as $restriction) {
207-
$sniffs = array_merge($sniffs, $this->expandRulesetReference($restriction, dirname($standard)));
208-
}
209-
} catch (RuntimeException $e) {
205+
foreach ($restrictions as $restriction) {
206+
$sniffs = array_merge($sniffs, $this->expandRulesetReference($restriction, dirname($standard)));
207+
}
208+
209+
if (empty($sniffs) === true) {
210210
// Sniff reference could not be expanded, which probably means this
211211
// is an installed standard. Let the unit test system take care of
212212
// setting the correct sniff for testing.
@@ -255,7 +255,7 @@ public function __construct(Config $config)
255255
}
256256

257257
if ($numSniffs === 0) {
258-
throw new RuntimeException('ERROR: No sniffs were registered');
258+
$this->msgCache->add('No sniffs were registered.', MessageCollector::ERROR);
259259
}
260260

261261
$this->displayCachedMessages();
@@ -1040,8 +1040,8 @@ private function expandRulesetReference($ref, $rulesetDir, $depth=0)
10401040
}
10411041
} else {
10421042
if (is_file($ref) === false) {
1043-
$error = "ERROR: Referenced sniff \"$ref\" does not exist";
1044-
throw new RuntimeException($error);
1043+
$this->msgCache->add("Referenced sniff \"$ref\" does not exist.", MessageCollector::ERROR);
1044+
return [];
10451045
}
10461046

10471047
if (substr($ref, -9) === 'Sniff.php') {
@@ -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, MessageCollector::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

@@ -1459,8 +1460,12 @@ public function populateTokenListeners()
14591460

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

14661471
$ignorePatterns = [];
@@ -1570,9 +1575,9 @@ public function setSniffProperty($sniffClass, $name, $settings)
15701575

15711576
if ($isSettable === false) {
15721577
if ($settings['scope'] === 'sniff') {
1573-
$notice = "ERROR: Ruleset invalid. Property \"$propertyName\" does not exist on sniff ";
1574-
$notice .= array_search($sniffClass, $this->sniffCodes, true);
1575-
throw new RuntimeException($notice);
1578+
$notice = "Property \"$propertyName\" does not exist on sniff ";
1579+
$notice .= array_search($sniffClass, $this->sniffCodes, true).'.';
1580+
$this->msgCache->add($notice, MessageCollector::ERROR);
15761581
}
15771582

15781583
return;

tests/Core/Ruleset/ConstructorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public function testNoSniffsRegisteredException()
282282
$standard = __DIR__.'/ConstructorNoSniffsTest.xml';
283283
$config = new ConfigDouble(["--standard=$standard"]);
284284

285-
$message = 'ERROR: No sniffs were registered';
285+
$message = 'ERROR: No sniffs were registered.'.PHP_EOL.PHP_EOL;
286286
$this->expectRuntimeExceptionMessage($message);
287287

288288
new Ruleset($config);

tests/Core/Ruleset/ExpandRulesetReferenceHomePathTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public function testHomePathRefGetsExpandedAndThrowsExceptionWhenPathIsInvalid()
109109
$standard = __DIR__.'/ExpandRulesetReferenceHomePathFailTest.xml';
110110
$config = new ConfigDouble(["--standard=$standard"]);
111111

112-
$exceptionMessage = 'ERROR: Referenced sniff "~/src/MyStandard/Sniffs/DoesntExist/" does not exist';
112+
$exceptionMessage = 'ERROR: Referenced sniff "~/src/MyStandard/Sniffs/DoesntExist/" does not exist.'.PHP_EOL;
113+
$exceptionMessage .= 'ERROR: No sniffs were registered.'.PHP_EOL.PHP_EOL;
113114
$this->expectRuntimeExceptionMessage($exceptionMessage);
114115

115116
new Ruleset($config);

tests/Core/Ruleset/ExpandRulesetReferenceTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public function testUnresolvableReferenceThrowsException($standard, $replacement
6161
$standard = __DIR__.'/'.$standard;
6262
$config = new ConfigDouble(["--standard=$standard"]);
6363

64-
$exceptionMessage = 'ERROR: Referenced sniff "%s" does not exist';
64+
$exceptionMessage = 'ERROR: Referenced sniff "%s" does not exist.'.PHP_EOL;
65+
$exceptionMessage .= 'ERROR: No sniffs were registered.'.PHP_EOL.PHP_EOL;
6566
$this->expectRuntimeExceptionMessage(sprintf($exceptionMessage, $replacement));
6667

6768
new Ruleset($config);

tests/Core/Ruleset/PopulateTokenListenersRegisterNoArrayTest.xml

+2
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

+6-1
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

tests/Core/Ruleset/ProcessRuleInvalidTypeTest.php

+2-2
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);

tests/Core/Ruleset/SetSniffPropertyTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
135135
*/
136136
public function testSetPropertyThrowsErrorOnInvalidProperty()
137137
{
138-
$exceptionMsg = 'ERROR: Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
138+
$exceptionMsg = 'ERROR: Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent.'.PHP_EOL.PHP_EOL;
139139
$this->expectRuntimeExceptionMessage($exceptionMsg);
140140

141141
// Set up the ruleset.
@@ -155,7 +155,7 @@ public function testSetPropertyThrowsErrorOnInvalidProperty()
155155
*/
156156
public function testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
157157
{
158-
$exceptionMsg = 'ERROR: Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
158+
$exceptionMsg = 'ERROR: Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute.'.PHP_EOL.PHP_EOL;
159159
$this->expectRuntimeExceptionMessage($exceptionMsg);
160160

161161
// Set up the ruleset.

0 commit comments

Comments
 (0)