From 27fd29a14f1b0ed5c691a184f570952ecba39b48 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 17 Mar 2025 02:24:58 +0100 Subject: [PATCH] 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. --- src/Files/File.php | 18 +----------------- .../Tests/PHP/BacktickOperatorUnitTest.inc | 7 ------- .../Tests/PHP/BacktickOperatorUnitTest.php | 2 -- 3 files changed, 1 insertion(+), 26 deletions(-) diff --git a/src/Files/File.php b/src/Files/File.php index aabafd46b5..6b8379d1ac 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -346,7 +346,6 @@ public function process() $listenerIgnoreTo = []; $inTests = defined('PHP_CODESNIFFER_IN_TESTS'); $checkAnnotations = $this->config->annotations; - $annotationErrors = []; // Foreach of the listeners that have registered to listen for this // token, get them to process it. @@ -415,15 +414,7 @@ public function process() 'scope' => 'sniff', ]; $listenerClass = $this->ruleset->sniffCodes[$listenerCode]; - try { - $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $settings); - } catch (RuntimeException $e) { - // Non-existant property being set via an inline annotation. - // This is typically a PHPCS test case file, but we can't throw an error on the annotation - // line as it would get ignored. We also don't want this error to block - // the scan of the current file, so collect these and throw later. - $annotationErrors[] = 'Line '.$token['line'].': '.str_replace('Ruleset invalid. ', '', $e->getMessage()); - } + $this->ruleset->setSniffProperty($listenerClass, $propertyCode, $settings); } } }//end if @@ -553,13 +544,6 @@ public function process() } } - if ($annotationErrors !== []) { - $error = 'Encountered invalid inline phpcs:set annotations. Found:'.PHP_EOL; - $error .= implode(PHP_EOL, $annotationErrors); - - $this->addWarning($error, null, 'Internal.PropertyDoesNotExist'); - } - if (PHP_CODESNIFFER_VERBOSITY > 2) { echo "\t*** END TOKEN PROCESSING ***".PHP_EOL; echo "\t*** START SNIFF PROCESSING REPORT ***".PHP_EOL; diff --git a/src/Standards/Generic/Tests/PHP/BacktickOperatorUnitTest.inc b/src/Standards/Generic/Tests/PHP/BacktickOperatorUnitTest.inc index e2ca72d1f1..3355c2d32c 100644 --- a/src/Standards/Generic/Tests/PHP/BacktickOperatorUnitTest.inc +++ b/src/Standards/Generic/Tests/PHP/BacktickOperatorUnitTest.inc @@ -1,9 +1,2 @@ 2, - 9 => 2, ]; }//end getErrorList() @@ -48,7 +47,6 @@ public function getErrorList() */ public function getWarningList() { - // Warning about incorrect annotation will be shown on line 1 once PR #3915 would be merged. return []; }//end getWarningList()