Skip to content

Commit 9d5968b

Browse files
committed
Ruleset: use MsgCollector for "referenced sniff not found" error
1 parent ed866d4 commit 9d5968b

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/Ruleset.php

Lines changed: 7 additions & 7 deletions
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.
@@ -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.", MsgCollector::ERROR);
1044+
return [];
10451045
}
10461046

10471047
if (substr($ref, -9) === 'Sniff.php') {

tests/Core/Ruleset/ExpandRulesetReferenceHomePathTest.php

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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);

0 commit comments

Comments
 (0)