Skip to content

Commit eb9b206

Browse files
authored
Merge pull request #869 from Automattic/feature/constants-restrictedconstants-minor-improvements
2 parents 7f4c63d + 169a74e commit eb9b206

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace WordPressVIPMinimum\Sniffs\Constants;
1111

1212
use PHP_CodeSniffer\Util\Tokens;
13+
use PHPCSUtils\Utils\TextStrings;
1314
use WordPressVIPMinimum\Sniffs\Sniff;
1415

1516
/**
@@ -36,12 +37,39 @@ class RestrictedConstantsSniff extends Sniff {
3637
'WP_CRON_CONTROL_SECRET',
3738
];
3839

40+
/**
41+
* List of (global) constant names, which should not be referenced in userland code, nor (re-)declared.
42+
*
43+
* {@internal The `public` versions of these properties can't be removed until the next major,
44+
* though a decision is still needed whether they should be removed at all.
45+
* Also see: Automattic/VIP-Coding-Standards#234 for more context and discussion about this.}
46+
*
47+
* @var array<string, int> Key is the constant name, value is irrelevant.
48+
*/
49+
private $restrictedConstants = [];
50+
51+
/**
52+
* List of (global) constants, which should not be (re-)declared, but may be referenced.
53+
*
54+
* {@internal The `public` versions of these properties can't be removed until the next major,
55+
* though a decision is still needed whether they should be removed at all.
56+
* Also see: Automattic/VIP-Coding-Standards#234 for more context and discussion about this.}
57+
*
58+
* @var array<string, int> Key is the constant name, value is irrelevant.
59+
*/
60+
private $restrictedRedeclaration = [];
61+
3962
/**
4063
* Returns an array of tokens this test wants to listen for.
4164
*
4265
* @return array<int|string>
4366
*/
4467
public function register() {
68+
// For now, set the `private` properties based on the values of the `public` properties.
69+
// This should be revisited when Automattic/VIP-Coding-Standards#234 gets actioned.
70+
$this->restrictedConstants = array_flip( $this->restrictedConstantNames );
71+
$this->restrictedRedeclaration = array_flip( $this->restrictedConstantDeclaration );
72+
4573
return [
4674
T_CONSTANT_ENCAPSED_STRING,
4775
T_STRING,
@@ -60,15 +88,17 @@ public function process_token( $stackPtr ) {
6088
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING ) {
6189
$constantName = $this->tokens[ $stackPtr ]['content'];
6290
} else {
63-
$constantName = trim( $this->tokens[ $stackPtr ]['content'], "\"'" );
91+
$constantName = TextStrings::stripQuotes( $this->tokens[ $stackPtr ]['content'] );
6492
}
6593

66-
if ( in_array( $constantName, $this->restrictedConstantNames, true ) === false && in_array( $constantName, $this->restrictedConstantDeclaration, true ) === false ) {
94+
if ( isset( $this->restrictedConstants[ $constantName ] ) === false
95+
&& isset( $this->restrictedRedeclaration[ $constantName ] ) === false
96+
) {
6797
// Not the constant we are looking for.
6898
return;
6999
}
70100

71-
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING && in_array( $constantName, $this->restrictedConstantNames, true ) === true ) {
101+
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING && isset( $this->restrictedConstants[ $constantName ] ) === true ) {
72102
$message = 'Code is touching the `%s` constant. Make sure it\'s used appropriately.';
73103
$data = [ $constantName ];
74104
$this->phpcsFile->addWarning( $message, $stackPtr, 'UsingRestrictedConstant', $data );
@@ -97,12 +127,12 @@ public function process_token( $stackPtr ) {
97127
return;
98128
}
99129

100-
if ( in_array( $this->tokens[ $previous ]['code'], Tokens::$functionNameTokens, true ) === true ) {
130+
if ( $this->tokens[ $previous ]['code'] === T_STRING ) {
101131
$data = [ $constantName ];
102132
if ( $this->tokens[ $previous ]['content'] === 'define' ) {
103133
$message = 'The definition of `%s` constant is prohibited. Please use a different name.';
104134
$this->phpcsFile->addError( $message, $previous, 'DefiningRestrictedConstant', $data );
105-
} elseif ( in_array( $constantName, $this->restrictedConstantNames, true ) === true ) {
135+
} elseif ( isset( $this->restrictedConstants[ $constantName ] ) === true ) {
106136
$message = 'Code is touching the `%s` constant. Make sure it\'s used appropriately.';
107137
$this->phpcsFile->addWarning( $message, $previous, 'UsingRestrictedConstant', $data );
108138
}

WordPressVIPMinimum/Tests/Constants/RestrictedConstantsUnitTest.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ if ( defined( 'JETPACK_DEV_DEBUG' ) ) { // Okay. Can touch.
2020

2121
if ( constant( 'WP_CRON_CONTROL_SECRET' ) ) { // Okay. Can touch.
2222

23-
}
23+
}
24+
25+
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.

0 commit comments

Comments
 (0)