Skip to content

Commit 494703c

Browse files
committed
Runner: don't prefix Ruleset error notices
As things are, the `Runner` class catches any exception thrown by the Ruleset, enhances the message with an `ERROR` prefix, adds new lines and short help usage info and then re-throws this as a `DeepExitException`. In an upcoming PR, a new `MsgCollector` class will be introduced, which will allow for both errors, as well as warnings, notices and deprecations. This means that the `ERROR` prefix won't always be appropriate anymore. This commit moves the `ERROR` prefix addition from the `Runner` class to the individual messages for the Exceptions being thrown in the `Ruleset` class. Additionally, it changes the "trailing new lines" handling as the `MsgCollector` may also add new lines at the end of a message, as messages will not necessarily always be displayed via an exception (think: non-blocking deprecation notices). So to prevent a duplicate set of new lines, any new lines which are included in the exception are trimmed off before adding the new lines desired for the Exception display. _This is a preliminary step before introducing the `MsgCollector` to the `Ruleset` class._
1 parent 33a3d89 commit 494703c

11 files changed

+25
-25
lines changed

src/Ruleset.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public function __construct(Config $config)
246246
}
247247

248248
if ($numSniffs === 0) {
249-
throw new RuntimeException('No sniffs were registered');
249+
throw new RuntimeException('ERROR: No sniffs were registered');
250250
}
251251

252252
}//end __construct()
@@ -385,7 +385,7 @@ public function showSniffDeprecations()
385385

386386
$messages = [];
387387
$messageTemplate = 'This sniff has been deprecated since %s and will be removed in %s. %s';
388-
$errorTemplate = 'The %s::%s() method must return a %sstring, received %s';
388+
$errorTemplate = 'ERROR: The %s::%s() method must return a %sstring, received %s';
389389

390390
foreach ($this->deprecatedSniffs as $sniffCode => $className) {
391391
if (isset($this->sniffs[$className]) === false) {
@@ -504,7 +504,7 @@ public function processRuleset($rulesetPath, $depth=0)
504504
libxml_use_internal_errors(true);
505505
$ruleset = simplexml_load_string(file_get_contents($rulesetPath));
506506
if ($ruleset === false) {
507-
$errorMsg = "Ruleset $rulesetPath is not valid".PHP_EOL;
507+
$errorMsg = "ERROR: Ruleset $rulesetPath is not valid".PHP_EOL;
508508
$errors = libxml_get_errors();
509509
foreach ($errors as $error) {
510510
$errorMsg .= '- On line '.$error->line.', column '.$error->column.': '.$error->message;
@@ -566,7 +566,7 @@ public function processRuleset($rulesetPath, $depth=0)
566566
if ($relativePath !== false && is_file($relativePath) === true) {
567567
$autoloadPath = $relativePath;
568568
} else if (is_file($autoloadPath) === false) {
569-
throw new RuntimeException('The specified autoload file "'.$autoloadPath.'" does not exist');
569+
throw new RuntimeException('ERROR: The specified autoload file "'.$autoloadPath.'" does not exist');
570570
}
571571

572572
include_once $autoloadPath;
@@ -968,7 +968,7 @@ private function expandRulesetReference($ref, $rulesetDir, $depth=0)
968968
}
969969
} else {
970970
if (is_file($ref) === false) {
971-
$error = "Referenced sniff \"$ref\" does not exist";
971+
$error = "ERROR: Referenced sniff \"$ref\" does not exist";
972972
throw new RuntimeException($error);
973973
}
974974

@@ -1056,7 +1056,7 @@ private function processRule($rule, $newSniffs, $depth=0)
10561056

10571057
$type = strtolower((string) $rule->type);
10581058
if ($type !== 'error' && $type !== 'warning') {
1059-
throw new RuntimeException("Message type \"$type\" is invalid; must be \"error\" or \"warning\"");
1059+
throw new RuntimeException("ERROR: Message type \"$type\" is invalid; must be \"error\" or \"warning\"");
10601060
}
10611061

10621062
$this->ruleset[$code]['type'] = $type;
@@ -1360,7 +1360,7 @@ public function populateTokenListeners()
13601360

13611361
$tokens = $this->sniffs[$sniffClass]->register();
13621362
if (is_array($tokens) === false) {
1363-
$msg = "Sniff $sniffClass register() method must return an array";
1363+
$msg = "ERROR: Sniff $sniffClass register() method must return an array";
13641364
throw new RuntimeException($msg);
13651365
}
13661366

@@ -1442,7 +1442,7 @@ public function setSniffProperty($sniffClass, $name, $settings)
14421442

14431443
if ($isSettable === false) {
14441444
if ($settings['scope'] === 'sniff') {
1445-
$notice = "Ruleset invalid. Property \"$propertyName\" does not exist on sniff ";
1445+
$notice = "ERROR: Ruleset invalid. Property \"$propertyName\" does not exist on sniff ";
14461446
$notice .= array_search($sniffClass, $this->sniffCodes, true);
14471447
throw new RuntimeException($notice);
14481448
}

src/Runner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function init()
337337
$this->ruleset->showSniffDeprecations();
338338
}
339339
} catch (RuntimeException $e) {
340-
$error = 'ERROR: '.$e->getMessage().PHP_EOL.PHP_EOL;
340+
$error = rtrim($e->getMessage(), "\r\n").PHP_EOL.PHP_EOL;
341341
$error .= $this->config->printShortUsage(true);
342342
throw new DeepExitException($error, 3);
343343
}

tests/Core/Ruleset/ConstructorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public function testNoSniffsRegisteredException()
281281
$standard = __DIR__.'/ConstructorNoSniffsTest.xml';
282282
$config = new ConfigDouble(["--standard=$standard"]);
283283

284-
$message = 'No sniffs were registered';
284+
$message = 'ERROR: No sniffs were registered';
285285
$this->expectRuntimeExceptionMessage($message);
286286

287287
new Ruleset($config);

tests/Core/Ruleset/ExpandRulesetReferenceHomePathTest.php

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

112-
$exceptionMessage = 'Referenced sniff "~/src/MyStandard/Sniffs/DoesntExist/" does not exist';
112+
$exceptionMessage = 'ERROR: Referenced sniff "~/src/MyStandard/Sniffs/DoesntExist/" does not exist';
113113
$this->expectRuntimeExceptionMessage($exceptionMessage);
114114

115115
new Ruleset($config);

tests/Core/Ruleset/ExpandRulesetReferenceTest.php

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

64-
$exceptionMessage = 'Referenced sniff "%s" does not exist';
64+
$exceptionMessage = 'ERROR: Referenced sniff "%s" does not exist';
6565
$this->expectRuntimeExceptionMessage(sprintf($exceptionMessage, $replacement));
6666

6767
new Ruleset($config);

tests/Core/Ruleset/PopulateTokenListenersTest.php

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

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

6868
new Ruleset($config);

tests/Core/Ruleset/ProcessRuleInvalidTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testInvalidTypeHandling()
3232
$standard = __DIR__.'/ProcessRuleInvalidTypeTest.xml';
3333
$config = new ConfigDouble(["--standard=$standard"]);
3434

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

3838
new Ruleset($config);

tests/Core/Ruleset/ProcessRulesetAutoloadTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function testShouldProcessAutoloadCbfonly()
150150
*/
151151
public function testFileNotFoundException()
152152
{
153-
$exceptionMsg = 'The specified autoload file "./tests/Core/Ruleset/Fixtures/ThisFileDoesNotExist.php" does not exist';
153+
$exceptionMsg = 'ERROR: The specified autoload file "./tests/Core/Ruleset/Fixtures/ThisFileDoesNotExist.php" does not exist';
154154
$this->expectRuntimeExceptionMessage($exceptionMsg);
155155

156156
// Set up the ruleset.

tests/Core/Ruleset/ProcessRulesetBrokenRulesetTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testBrokenRulesetEmptyFile()
3939
$standard = __DIR__.'/ProcessRulesetBrokenRulesetEmptyFileTest.xml';
4040
$config = new ConfigDouble(["--standard=$standard"]);
4141

42-
$regex = '`^Ruleset \S+ProcessRulesetBrokenRulesetEmptyFileTest\.xml is not valid\R';
42+
$regex = '`^ERROR: Ruleset \S+ProcessRulesetBrokenRulesetEmptyFileTest\.xml is not valid\R';
4343
$regex .= '(- On line 1, column 1: Document is empty\R)?$`';
4444

4545
$this->expectRuntimeExceptionRegex($regex);
@@ -59,7 +59,7 @@ public function testBrokenRulesetSingleError()
5959
$standard = __DIR__.'/ProcessRulesetBrokenRulesetSingleErrorTest.xml';
6060
$config = new ConfigDouble(["--standard=$standard"]);
6161

62-
$regex = '`^Ruleset \S+ProcessRulesetBrokenRulesetSingleErrorTest\.xml is not valid\R';
62+
$regex = '`^ERROR: Ruleset \S+ProcessRulesetBrokenRulesetSingleErrorTest\.xml is not valid\R';
6363
$regex .= '- On line 3, column 1: Premature end of data in tag ruleset line 2\R$`';
6464

6565
$this->expectRuntimeExceptionRegex($regex);
@@ -79,7 +79,7 @@ public function testBrokenRulesetMultiError()
7979
$standard = __DIR__.'/ProcessRulesetBrokenRulesetMultiErrorTest.xml';
8080
$config = new ConfigDouble(["--standard=$standard"]);
8181

82-
$regex = '`^Ruleset \S+ProcessRulesetBrokenRulesetMultiErrorTest\.xml is not valid\R';
82+
$regex = '`^ERROR: Ruleset \S+ProcessRulesetBrokenRulesetMultiErrorTest\.xml is not valid\R';
8383
$regex .= '- On line 8, column 12: Opening and ending tag mismatch: property line 7 and rule\R';
8484
$regex .= '- On line 10, column 11: Opening and ending tag mismatch: properties line 5 and ruleset\R';
8585
$regex .= '(- On line 11, column 1: Premature end of data in tag rule line 4\R)?$`';

tests/Core/Ruleset/SetSniffPropertyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
135135
*/
136136
public function testSetPropertyThrowsErrorOnInvalidProperty()
137137
{
138-
$exceptionMsg = 'Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
138+
$exceptionMsg = 'ERROR: Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
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 = 'Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
158+
$exceptionMsg = 'ERROR: Ruleset invalid. Property "arbitrarystring" does not exist on sniff TestStandard.SetProperty.NotAllowedViaAttribute';
159159
$this->expectRuntimeExceptionMessage($exceptionMsg);
160160

161161
// Set up the ruleset.

0 commit comments

Comments
 (0)