From dfe6a2cc084a985325d30f1b863bd9c00d458451 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 23 Jul 2025 02:25:12 +0200 Subject: [PATCH 1/3] Classes/RestrictedExtendClasses: improve the tests Add tests with: * Variations of namespaced class names. * Anonymous classes extending. * Group exclusion, as supported via the extended abstract. --- .../RestrictedExtendClassesUnitTest.inc | 18 ++++++++++++++---- .../RestrictedExtendClassesUnitTest.php | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.inc b/WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.inc index 49b4aff1..dc302c63 100644 --- a/WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.inc @@ -2,11 +2,21 @@ class OkTestClass extends WPCOM_VIP_CLI_Command { } // Ok - correct class. class AnotherOkTestClass extends WPC__CLI_Command { } // Ok - spacing + similar class. -class Ok_TestClass extends wpcom_vip_cli_command { } // Ok - lowercase correct class. -class Ok_Test_Class extends WpCoM_VIP_cli_command { } // Ok - mixed case correct class. +class Ok_TestClass extends \wpcom_vip_cli_command { } // Ok - lowercase correct class. +$ok = new class extends WpCoM_VIP_cli_command { }; // Ok - mixed case correct class. class WP_CLI_Command { } // Ok - not extending. class BadTestClass extends WP_CLI_Command { } // Warning - wrong class. -class AnotherBadTestClass extends wp_CLI_comMand { } // Warning - mixed case wrong class. -class AnotherBad_TestClass extends WP_CLI_comMand { } // Warning - spacing + mixed case wrong class. +class AnotherBadTestClass extends \wp_CLI_comMand { } // Warning - mixed case wrong class. +$bad = new class() extends WP_CLI_comMand { }; // Warning - spacing + mixed case wrong class. class Bad_Test_Class extends wp_cli_command { } // Warning - lowercase wrong class. + +// phpcs:set WordPressVIPMinimum.Classes.RestrictedExtendClasses exclude[] wp_cli +class IgnoredClass extends WP_CLI_Command { } // OK, group is excluded. + +// Reset to the default value. +// phpcs:set WordPressVIPMinimum.Classes.RestrictedExtendClasses exclude[] + +class Ok_NotTheTargetFQN extends \Fully\Qualified\WP_CLI_Command { } // Ok - not the right namespace. +class Ok_NotTheTargetPQN extends Partially\Qualified\WP_CLI_Command { } // Ok - not the right namespace. +class Bad_NamespaceRelative extends namespace\WP_CLI_Command { } // Warning - this is not a namespaced file, so "namespace" resolves to the global namespace. diff --git a/WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.php b/WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.php index 51c1cf00..f5605082 100644 --- a/WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.php +++ b/WordPressVIPMinimum/Tests/Classes/RestrictedExtendClassesUnitTest.php @@ -36,6 +36,7 @@ public function getWarningList() { 10 => 1, 11 => 1, 12 => 1, + 22 => 1, ]; } } From 73f70809cea277cad367e88b666591c37559870c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 23 Jul 2025 02:32:24 +0200 Subject: [PATCH 2/3] Classes/RestrictedExtendClasses: efficiency fix The `AbstractClassRestrictionsSniff` class does quite a lot of processing before passing off to the `process_matched_token()` method, which can cause quite some overhead for a sniff like this, which is only interested in a class being extended, so let's make it so this sniff only listens to the `T_EXTENDS` token. This should make the sniff significantly faster. --- .../Classes/RestrictedExtendClassesSniff.php | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php b/WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php index bb99d4fa..a395031d 100644 --- a/WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php +++ b/WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php @@ -35,6 +35,20 @@ public function getGroups() { ]; } + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + $targets = parent::register(); + if ( empty( $targets ) ) { + return $targets; + } + + return [ T_EXTENDS ]; + } + /** * Process a matched token. * @@ -46,13 +60,6 @@ public function getGroups() { * @return void */ public function process_matched_token( $stackPtr, $group_name, $matched_content ) { - $tokens = $this->phpcsFile->getTokens(); - - if ( $tokens[ $stackPtr ]['code'] !== T_EXTENDS ) { - // If not extending, bail. - return; - } - foreach ( $this->getGroups() as $group => $group_args ) { $this->phpcsFile->{ 'add' . $group_args['type'] }( $group_args['message'], $stackPtr, $group ); } From cb553b0c45e117473020e564caed7fec5ace96f8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 21 Jul 2025 03:31:55 +0200 Subject: [PATCH 3/3] Classes/RestrictedExtendClasses: minor tweak Not strictly necessary, but improves the code readability a little. --- .../Sniffs/Classes/RestrictedExtendClassesSniff.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php b/WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php index a395031d..5120bb9a 100644 --- a/WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php +++ b/WordPressVIPMinimum/Sniffs/Classes/RestrictedExtendClassesSniff.php @@ -9,6 +9,7 @@ namespace WordPressVIPMinimum\Sniffs\Classes; +use PHPCSUtils\Utils\MessageHelper; use WordPressCS\WordPress\AbstractClassRestrictionsSniff; /** @@ -61,7 +62,8 @@ public function register() { */ public function process_matched_token( $stackPtr, $group_name, $matched_content ) { foreach ( $this->getGroups() as $group => $group_args ) { - $this->phpcsFile->{ 'add' . $group_args['type'] }( $group_args['message'], $stackPtr, $group ); + $isError = ( $group_args['type'] === 'error' ); + MessageHelper::addMessage( $this->phpcsFile, $group_args['message'], $stackPtr, $isError, $group ); } } }