1010namespace WordPressVIPMinimum \Sniffs \Constants ;
1111
1212use PHP_CodeSniffer \Util \Tokens ;
13+ use PHPCSUtils \Utils \TextStrings ;
1314use 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 }
0 commit comments