Skip to content

Commit 90b47ed

Browse files
committed
Remove some redundant code
With the `MessageCollector` being used in the `Ruleset` class since PR 857, some guard code as introduced in 99 is no longer needed. The guard code was in place to prevent the following situation: * File under test contained an invalid `// phpcs:set` annotation which tried to set a non-existent property on a sniff. * The `File::process()` method would see this annotation and call the `Ruleset::setSniffProperty()` method to set the property. * The `Ruleset::setSniffProperty()` method would throw an exception for the non-existent property. * The exception would cause the `File` class to stop scanning the file, potentially leading to missing errors/warnings. As the error for setting a non-existent property now no longer leads to an exception on a direct call to the `Ruleset::setSniffProperty()` method, we also no longer need to catch the exception and handle it from within the `File::process()` method. In practice, this means that setting a non-existent property on a sniff via an inline `// phpcs:set` annotation will now be silently ignored (again) and will not affect the scan of the rest of the file. This commit reverts 99.
1 parent cd71c88 commit 90b47ed

File tree

3 files changed

+1
-26
lines changed

3 files changed

+1
-26
lines changed

src/Files/File.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ public function process()
331331
$listenerIgnoreTo = [];
332332
$inTests = defined('PHP_CODESNIFFER_IN_TESTS');
333333
$checkAnnotations = $this->config->annotations;
334-
$annotationErrors = [];
335334

336335
// Foreach of the listeners that have registered to listen for this
337336
// token, get them to process it.
@@ -369,15 +368,7 @@ public function process()
369368
'scope' => 'sniff',
370369
];
371370
$listenerClass = $this->ruleset->sniffCodes[$listenerCode];
372-
try {
373-
$this->ruleset->setSniffProperty($listenerClass, $propertyCode, $settings);
374-
} catch (RuntimeException $e) {
375-
// Non-existant property being set via an inline annotation.
376-
// This is typically a PHPCS test case file, but we can't throw an error on the annotation
377-
// line as it would get ignored. We also don't want this error to block
378-
// the scan of the current file, so collect these and throw later.
379-
$annotationErrors[] = 'Line '.$token['line'].': '.str_replace('Ruleset invalid. ', '', $e->getMessage());
380-
}
371+
$this->ruleset->setSniffProperty($listenerClass, $propertyCode, $settings);
381372
}
382373
}
383374
}//end if
@@ -501,13 +492,6 @@ public function process()
501492
}
502493
}
503494

504-
if ($annotationErrors !== []) {
505-
$error = 'Encountered invalid inline phpcs:set annotations. Found:'.PHP_EOL;
506-
$error .= implode(PHP_EOL, $annotationErrors);
507-
508-
$this->addWarning($error, null, 'Internal.PropertyDoesNotExist');
509-
}
510-
511495
if (PHP_CODESNIFFER_VERBOSITY > 2) {
512496
Common::printStatusMessage('*** END TOKEN PROCESSING ***', 1);
513497
Common::printStatusMessage('*** START SNIFF PROCESSING REPORT ***', 1);
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
<?php
22
$output = `ls -al`;
3-
4-
// Testing an invalid phpcs:set annotations.
5-
// This test is unrelated to this sniff, but the issue needs a sniff to be tested.
6-
// phpcs:set Generic.PHP.BacktickOperator unknown 10
7-
8-
// Make sure errors after an incorrect annotation are still being thrown.
9-
$output = `ls -al`;

src/Standards/Generic/Tests/PHP/BacktickOperatorUnitTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function getErrorList()
3232
{
3333
return [
3434
2 => 2,
35-
9 => 2,
3635
];
3736

3837
}//end getErrorList()
@@ -48,7 +47,6 @@ public function getErrorList()
4847
*/
4948
public function getWarningList()
5049
{
51-
// Warning about incorrect annotation will be shown on line 1 once PR #3915 would be merged.
5250
return [];
5351

5452
}//end getWarningList()

0 commit comments

Comments
 (0)