From d5e5c27b2e40e90b208566ab21610d4f85806763 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 23 Jul 2025 23:54:51 +0200 Subject: [PATCH 1/3] Constants/RestrictedConstants: minor efficiency fix This commit introduces `private` property versions of the pre-existing `public` properties, where the array format is different. The `private` properties have the target constant names as the array key, not as the value. This allows for using `isset()` instead of `in_array()`. --- .../Constants/RestrictedConstantsSniff.php | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php b/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php index ac833cce..1b0a7294 100644 --- a/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php @@ -36,12 +36,39 @@ class RestrictedConstantsSniff extends Sniff { 'WP_CRON_CONTROL_SECRET', ]; + /** + * List of (global) constant names, which should not be referenced in userland code, nor (re-)declared. + * + * {@internal The `public` versions of these properties can't be removed until the next major, + * though a decision is still needed whether they should be removed at all. + * Also see: Automattic/VIP-Coding-Standards#234 for more context and discussion about this.} + * + * @var array Key is the constant name, value is irrelevant. + */ + private $restrictedConstants = []; + + /** + * List of (global) constants, which should not be (re-)declared, but may be referenced. + * + * {@internal The `public` versions of these properties can't be removed until the next major, + * though a decision is still needed whether they should be removed at all. + * Also see: Automattic/VIP-Coding-Standards#234 for more context and discussion about this.} + * + * @var array Key is the constant name, value is irrelevant. + */ + private $restrictedRedeclaration = []; + /** * Returns an array of tokens this test wants to listen for. * * @return array */ public function register() { + // For now, set the `private` properties based on the values of the `public` properties. + // This should be revisited when Automattic/VIP-Coding-Standards#234 gets actioned. + $this->restrictedConstants = array_flip( $this->restrictedConstantNames ); + $this->restrictedRedeclaration = array_flip( $this->restrictedConstantDeclaration ); + return [ T_CONSTANT_ENCAPSED_STRING, T_STRING, @@ -63,12 +90,14 @@ public function process_token( $stackPtr ) { $constantName = trim( $this->tokens[ $stackPtr ]['content'], "\"'" ); } - if ( in_array( $constantName, $this->restrictedConstantNames, true ) === false && in_array( $constantName, $this->restrictedConstantDeclaration, true ) === false ) { + if ( isset( $this->restrictedConstants[ $constantName ] ) === false + && isset( $this->restrictedRedeclaration[ $constantName ] ) === false + ) { // Not the constant we are looking for. return; } - if ( $this->tokens[ $stackPtr ]['code'] === T_STRING && in_array( $constantName, $this->restrictedConstantNames, true ) === true ) { + if ( $this->tokens[ $stackPtr ]['code'] === T_STRING && isset( $this->restrictedConstants[ $constantName ] ) === true ) { $message = 'Code is touching the `%s` constant. Make sure it\'s used appropriately.'; $data = [ $constantName ]; $this->phpcsFile->addWarning( $message, $stackPtr, 'UsingRestrictedConstant', $data ); @@ -102,7 +131,7 @@ public function process_token( $stackPtr ) { if ( $this->tokens[ $previous ]['content'] === 'define' ) { $message = 'The definition of `%s` constant is prohibited. Please use a different name.'; $this->phpcsFile->addError( $message, $previous, 'DefiningRestrictedConstant', $data ); - } elseif ( in_array( $constantName, $this->restrictedConstantNames, true ) === true ) { + } elseif ( isset( $this->restrictedConstants[ $constantName ] ) === true ) { $message = 'Code is touching the `%s` constant. Make sure it\'s used appropriately.'; $this->phpcsFile->addWarning( $message, $previous, 'UsingRestrictedConstant', $data ); } From 43e02ffa3fb0071015afcec561394f5068ed3c7e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 20 Jul 2025 04:53:43 +0200 Subject: [PATCH 2/3] Constants/RestrictedConstants: fix nonsensical comparison The only token this sniff needs to check for (at this time) is the `T_STRING` token, which is used for both constant names as well as function names. Any other token in the `Tokens::$functionNameTokens` token array will never match the subsequent contents comparison anyhow. --- .../Sniffs/Constants/RestrictedConstantsSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php b/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php index 1b0a7294..77ef9759 100644 --- a/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php @@ -126,7 +126,7 @@ public function process_token( $stackPtr ) { return; } - if ( in_array( $this->tokens[ $previous ]['code'], Tokens::$functionNameTokens, true ) === true ) { + if ( $this->tokens[ $previous ]['code'] === T_STRING ) { $data = [ $constantName ]; if ( $this->tokens[ $previous ]['content'] === 'define' ) { $message = 'The definition of `%s` constant is prohibited. Please use a different name.'; From 169a74e067f9e1dc488050413be257343feca615 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 23 Jul 2025 14:36:57 +0200 Subject: [PATCH 3/3] Constants/RestrictedConstants: use PHPCSUtils `TextStrings::stripQuotes()` ... to only strip the wrapping quotes and not remove any quotes inside the wrapping quotes, which are part of the actual text string. Includes test. --- .../Sniffs/Constants/RestrictedConstantsSniff.php | 3 ++- .../Tests/Constants/RestrictedConstantsUnitTest.inc | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php b/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php index 77ef9759..416c947f 100644 --- a/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php @@ -10,6 +10,7 @@ namespace WordPressVIPMinimum\Sniffs\Constants; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Utils\TextStrings; use WordPressVIPMinimum\Sniffs\Sniff; /** @@ -87,7 +88,7 @@ public function process_token( $stackPtr ) { if ( $this->tokens[ $stackPtr ]['code'] === T_STRING ) { $constantName = $this->tokens[ $stackPtr ]['content']; } else { - $constantName = trim( $this->tokens[ $stackPtr ]['content'], "\"'" ); + $constantName = TextStrings::stripQuotes( $this->tokens[ $stackPtr ]['content'] ); } if ( isset( $this->restrictedConstants[ $constantName ] ) === false diff --git a/WordPressVIPMinimum/Tests/Constants/RestrictedConstantsUnitTest.inc b/WordPressVIPMinimum/Tests/Constants/RestrictedConstantsUnitTest.inc index 3fd9cd23..ff0c678f 100644 --- a/WordPressVIPMinimum/Tests/Constants/RestrictedConstantsUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Constants/RestrictedConstantsUnitTest.inc @@ -20,4 +20,6 @@ if ( defined( 'JETPACK_DEV_DEBUG' ) ) { // Okay. Can touch. if ( constant( 'WP_CRON_CONTROL_SECRET' ) ) { // Okay. Can touch. -} \ No newline at end of file +} + +define( '"A8C_PROXIED_REQUEST"', false ); // Okay, well not really as the name is not a valid PHP constant name, but that's not our concern.