Skip to content

Commit 5fc6ba4

Browse files
committed
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()`.
1 parent 3bc61bd commit 5fc6ba4

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

WordPressVIPMinimum/Sniffs/Constants/RestrictedConstantsSniff.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,39 @@ class RestrictedConstantsSniff extends Sniff {
3636
'WP_CRON_CONTROL_SECRET',
3737
];
3838

39+
/**
40+
* List of (global) constant names, which should not be referenced in userland code, nor (re-)declared.
41+
*
42+
* {@internal The `public` versions of these properties can't be removed until the next major,
43+
* though a decision is still needed whether they should be removed at all.
44+
* Also see: Automattic/VIP-Coding-Standards#234 for more context and discussion about this.}
45+
*
46+
* @var array<string, int> Key is the constant name, value is irrelevant.
47+
*/
48+
private $restrictedConstants = [];
49+
50+
/**
51+
* List of (global) constants, which should not be (re-)declared, but may be referenced.
52+
*
53+
* {@internal The `public` versions of these properties can't be removed until the next major,
54+
* though a decision is still needed whether they should be removed at all.
55+
* Also see: Automattic/VIP-Coding-Standards#234 for more context and discussion about this.}
56+
*
57+
* @var array<string, int> Key is the constant name, value is irrelevant.
58+
*/
59+
private $restrictedRedeclaration = [];
60+
3961
/**
4062
* Returns an array of tokens this test wants to listen for.
4163
*
4264
* @return array<int|string>
4365
*/
4466
public function register() {
67+
// For now, set the `private` properties based on the values of the `public` properties.
68+
// This should be revisited when Automattic/VIP-Coding-Standards#234 gets actioned.
69+
$this->restrictedConstants = array_flip($this->restrictedConstantNames);
70+
$this->restrictedRedeclaration = array_flip($this->restrictedConstantDeclaration);
71+
4572
return [
4673
T_CONSTANT_ENCAPSED_STRING,
4774
T_STRING,
@@ -63,12 +90,14 @@ public function process_token( $stackPtr ) {
6390
$constantName = trim( $this->tokens[ $stackPtr ]['content'], "\"'" );
6491
}
6592

66-
if ( in_array( $constantName, $this->restrictedConstantNames, true ) === false && in_array( $constantName, $this->restrictedConstantDeclaration, true ) === false ) {
93+
if ( isset( $this->restrictedConstants[ $constantName ] ) === false
94+
&& isset( $this->restrictedRedeclaration[ $constantName ] ) === false
95+
) {
6796
// Not the constant we are looking for.
6897
return;
6998
}
7099

71-
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING && in_array( $constantName, $this->restrictedConstantNames, true ) === true ) {
100+
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING && isset( $this->restrictedConstants[ $constantName ] ) === true ) {
72101
$message = 'Code is touching the `%s` constant. Make sure it\'s used appropriately.';
73102
$data = [ $constantName ];
74103
$this->phpcsFile->addWarning( $message, $stackPtr, 'UsingRestrictedConstant', $data );
@@ -102,7 +131,7 @@ public function process_token( $stackPtr ) {
102131
if ( $this->tokens[ $previous ]['content'] === 'define' ) {
103132
$message = 'The definition of `%s` constant is prohibited. Please use a different name.';
104133
$this->phpcsFile->addError( $message, $previous, 'DefiningRestrictedConstant', $data );
105-
} elseif ( in_array( $constantName, $this->restrictedConstantNames, true ) === true ) {
134+
} elseif ( isset( $this->restrictedConstants[ $constantName ] ) === true ) {
106135
$message = 'Code is touching the `%s` constant. Make sure it\'s used appropriately.';
107136
$this->phpcsFile->addWarning( $message, $previous, 'UsingRestrictedConstant', $data );
108137
}

0 commit comments

Comments
 (0)