From a611742223ec0ce065b5cc12810b4b1d992b3515 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 16 Nov 2024 01:34:13 +0100 Subject: [PATCH] Ruleset: remove support for sniffs nor implementing `Sniff` interface Previously truly invalid sniffs would already be ignored/rejected, but sniffs which didn't implement the interface, but did implement the required methods would still be accepted. Now, both will be rejected. Includes removing the `RegisterSniffsRejectsInvalidSniffTest` class as these tests are now redundant (covered via the `RegisterSniffsMissingInterfaceTest` class). This commit executes step 4 to address issue 694.. Closes 694 Related to 6 --- src/Ruleset.php | 21 +---- .../NoImplementsNoProcessSniff.php | 17 ---- .../NoImplementsNoRegisterOrProcessSniff.php | 12 --- .../NoImplementsNoRegisterSniff.php | 19 ----- ...ssRulesetAutoExpandSniffsDirectoryTest.xml | 1 - .../RegisterSniffsMissingInterfaceTest.php | 18 ++--- ...sInvalidSniffNoImplementsNoProcessTest.xml | 8 -- ...iffNoImplementsNoRegisterOrProcessTest.xml | 8 -- ...InvalidSniffNoImplementsNoRegisterTest.xml | 8 -- .../RegisterSniffsRejectsInvalidSniffTest.php | 80 ------------------- .../Ruleset/ShowSniffDeprecationsTest.xml | 1 - 11 files changed, 12 insertions(+), 181 deletions(-) delete mode 100644 tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/InvalidSniffError/NoImplementsNoProcessSniff.php delete mode 100644 tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/InvalidSniffError/NoImplementsNoRegisterOrProcessSniff.php delete mode 100644 tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/InvalidSniffError/NoImplementsNoRegisterSniff.php delete mode 100644 tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoProcessTest.xml delete mode 100644 tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml delete mode 100644 tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterTest.xml delete mode 100644 tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffTest.php diff --git a/src/Ruleset.php b/src/Ruleset.php index 7a25bf31ca..26731e0002 100644 --- a/src/Ruleset.php +++ b/src/Ruleset.php @@ -1418,24 +1418,9 @@ public function registerSniffs($files, $restrictions, $exclusions) $message = 'All sniffs must implement the PHP_CodeSniffer\\Sniffs\\Sniff interface.'.PHP_EOL; $message .= "Interface not implemented for sniff $className.".PHP_EOL; $message .= 'Contact the sniff author to fix the sniff.'; - $this->msgCache->add($message, MessageCollector::DEPRECATED); - - // Skip classes which don't implement the register() or process() methods. - if (method_exists($className, 'register') === false - || method_exists($className, 'process') === false - ) { - $errorMsg = 'Sniff class %s is missing required method %s().'; - if (method_exists($className, 'register') === false) { - $this->msgCache->add(sprintf($errorMsg, $className, 'register'), MessageCollector::ERROR); - } - - if (method_exists($className, 'process') === false) { - $this->msgCache->add(sprintf($errorMsg, $className, 'process'), MessageCollector::ERROR); - } - - continue; - } - }//end if + $this->msgCache->add($message, MessageCollector::ERROR); + continue; + } $listeners[$className] = $className; diff --git a/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/InvalidSniffError/NoImplementsNoProcessSniff.php b/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/InvalidSniffError/NoImplementsNoProcessSniff.php deleted file mode 100644 index d6aa4ee6a8..0000000000 --- a/tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/InvalidSniffError/NoImplementsNoProcessSniff.php +++ /dev/null @@ -1,17 +0,0 @@ - - diff --git a/tests/Core/Ruleset/RegisterSniffsMissingInterfaceTest.php b/tests/Core/Ruleset/RegisterSniffsMissingInterfaceTest.php index 89c82c028a..7b99034738 100644 --- a/tests/Core/Ruleset/RegisterSniffsMissingInterfaceTest.php +++ b/tests/Core/Ruleset/RegisterSniffsMissingInterfaceTest.php @@ -1,6 +1,6 @@ * @copyright 2025 PHPCSStandards and contributors @@ -11,14 +11,14 @@ use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Tests\ConfigDouble; -use PHPUnit\Framework\TestCase; +use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase; /** - * Tests deprecation of support for sniffs not implementing the PHPCS `Sniff` interface. + * Tests removed support for sniffs not implementing the PHPCS `Sniff` interface. * * @covers \PHP_CodeSniffer\Ruleset::registerSniffs */ -final class RegisterSniffsMissingInterfaceTest extends TestCase +final class RegisterSniffsMissingInterfaceTest extends AbstractRulesetTestCase { @@ -41,25 +41,25 @@ public function testNoNoticesForSniffsImplementingInterface() /** - * Test that a deprecation notice is shown if a sniff doesn't implement the Sniff interface. + * Test that an error is shown if a sniff doesn't implement the Sniff interface. * * @return void */ - public function testDeprecationNoticeWhenSniffDoesntImplementInterface() + public function testErrorWhenSniffDoesntImplementInterface() { // Set up the ruleset. $standard = __DIR__.'/RegisterSniffsMissingInterfaceInvalidTest.xml'; $config = new ConfigDouble(["--standard=$standard"]); - $expected = 'DEPRECATED: All sniffs must implement the PHP_CodeSniffer\\Sniffs\\Sniff interface.'.PHP_EOL; + $expected = 'ERROR: All sniffs must implement the PHP_CodeSniffer\\Sniffs\\Sniff interface.'.PHP_EOL; $expected .= 'Interface not implemented for sniff Fixtures\\TestStandard\\Sniffs\\MissingInterface\\InvalidImplementsWithoutImplementSniff.'.PHP_EOL; $expected .= 'Contact the sniff author to fix the sniff.'.PHP_EOL.PHP_EOL; - $this->expectOutputString($expected); + $this->expectRuntimeExceptionMessage($expected); new Ruleset($config); - }//end testDeprecationNoticeWhenSniffDoesntImplementInterface() + }//end testErrorWhenSniffDoesntImplementInterface() }//end class diff --git a/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoProcessTest.xml b/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoProcessTest.xml deleted file mode 100644 index d12d435b64..0000000000 --- a/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoProcessTest.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml b/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml deleted file mode 100644 index 3f03d1a2ea..0000000000 --- a/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterTest.xml b/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterTest.xml deleted file mode 100644 index 699628e232..0000000000 --- a/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterTest.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffTest.php b/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffTest.php deleted file mode 100644 index 1efe5cdba0..0000000000 --- a/tests/Core/Ruleset/RegisterSniffsRejectsInvalidSniffTest.php +++ /dev/null @@ -1,80 +0,0 @@ - - * @copyright 2025 PHPCSStandards and contributors - * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence - */ - -namespace PHP_CodeSniffer\Tests\Core\Ruleset; - -use PHP_CodeSniffer\Ruleset; -use PHP_CodeSniffer\Tests\ConfigDouble; -use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase; - -/** - * Tests that invalid sniffs will be rejected with an informative error message. - * - * @covers \PHP_CodeSniffer\Ruleset::registerSniffs - */ -final class RegisterSniffsRejectsInvalidSniffTest extends AbstractRulesetTestCase -{ - - - /** - * Verify that an error is thrown if an invalid sniff class is loaded. - * - * @param string $standard The standard to use for the test. - * @param string $methodName The name of the missing method. - * - * @dataProvider dataExceptionIsThrownOnMissingInterfaceMethod - * - * @return void - */ - public function testExceptionIsThrownOnMissingInterfaceMethod($standard, $methodName) - { - // Set up the ruleset. - $standard = __DIR__.'/'.$standard; - $config = new ConfigDouble(["--standard=$standard"]); - - $regex = "`(^|\R)ERROR: Sniff class \S+Sniff is missing required method $methodName\(\)\.\R`"; - $this->expectRuntimeExceptionRegex($regex); - - new Ruleset($config); - - }//end testExceptionIsThrownOnMissingInterfaceMethod() - - - /** - * Data provider. - * - * @see testExceptionIsThrownOnMissingInterfaceMethod() - * - * @return array> - */ - public static function dataExceptionIsThrownOnMissingInterfaceMethod() - { - return [ - 'Missing register() method' => [ - 'standard' => 'RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterTest.xml', - 'methodName' => 'register', - ], - 'Missing process() method' => [ - 'standard' => 'RegisterSniffsRejectsInvalidSniffNoImplementsNoProcessTest.xml', - 'methodName' => 'process', - ], - 'Missing both, checking register() method' => [ - 'standard' => 'RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml', - 'methodName' => 'register', - ], - 'Missing both, checking process() method' => [ - 'standard' => 'RegisterSniffsRejectsInvalidSniffNoImplementsNoRegisterOrProcessTest.xml', - 'methodName' => 'process', - ], - ]; - - }//end dataExceptionIsThrownOnMissingInterfaceMethod() - - -}//end class diff --git a/tests/Core/Ruleset/ShowSniffDeprecationsTest.xml b/tests/Core/Ruleset/ShowSniffDeprecationsTest.xml index 3ae834c76c..20e4a1ef0f 100644 --- a/tests/Core/Ruleset/ShowSniffDeprecationsTest.xml +++ b/tests/Core/Ruleset/ShowSniffDeprecationsTest.xml @@ -7,7 +7,6 @@ -