Skip to content

Commit 5529947

Browse files
committed
Squiz/OperatorBracket: stability tweak
The `$allowed` tokens array is joined after declaration with the `Tokens::$operators` array. This is only reliable if the keys in both arrays are unique (or intended to be overwritten). However, with the `$allowed` array being numerically indexed and PHP native tokens also being integers, this was not the case and could lead to one of the tokens in the original array (unintentionally) being disregarded/overwritten by one of the tokens in the `Tokens::$operators` array. Fixed now, by making setting the tokens constants both as the key as well as the value, making the entries in the array suitable for an array join operation.
1 parent e8b10e0 commit 5529947

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/Standards/Squiz/Sniffs/Formatting/OperatorBracketSniff.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,25 @@ public function process(File $phpcsFile, $stackPtr)
116116

117117
// Tokens that are allowed inside a bracketed operation.
118118
$allowed = [
119-
T_VARIABLE,
120-
T_LNUMBER,
121-
T_DNUMBER,
122-
T_STRING,
123-
T_NAME_QUALIFIED,
124-
T_NAME_FULLY_QUALIFIED,
125-
T_NAME_RELATIVE,
126-
T_WHITESPACE,
127-
T_THIS,
128-
T_SELF,
129-
T_STATIC,
130-
T_PARENT,
131-
T_OBJECT_OPERATOR,
132-
T_NULLSAFE_OBJECT_OPERATOR,
133-
T_DOUBLE_COLON,
134-
T_OPEN_SQUARE_BRACKET,
135-
T_CLOSE_SQUARE_BRACKET,
136-
T_NONE,
137-
T_BITWISE_NOT,
119+
T_VARIABLE => T_VARIABLE,
120+
T_LNUMBER => T_LNUMBER,
121+
T_DNUMBER => T_DNUMBER,
122+
T_STRING => T_STRING,
123+
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
124+
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED,
125+
T_NAME_RELATIVE => T_NAME_RELATIVE,
126+
T_WHITESPACE => T_WHITESPACE,
127+
T_THIS => T_THIS,
128+
T_SELF => T_SELF,
129+
T_STATIC => T_STATIC,
130+
T_PARENT => T_PARENT,
131+
T_OBJECT_OPERATOR => T_OBJECT_OPERATOR,
132+
T_NULLSAFE_OBJECT_OPERATOR => T_NULLSAFE_OBJECT_OPERATOR,
133+
T_DOUBLE_COLON => T_DOUBLE_COLON,
134+
T_OPEN_SQUARE_BRACKET => T_OPEN_SQUARE_BRACKET,
135+
T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET,
136+
T_NONE => T_NONE,
137+
T_BITWISE_NOT => T_BITWISE_NOT,
138138
];
139139

140140
$allowed += Tokens::$operators;
@@ -162,7 +162,7 @@ public function process(File $phpcsFile, $stackPtr)
162162
// We allow simple operations to not be bracketed.
163163
// For example, ceil($one / $two).
164164
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
165-
if (in_array($tokens[$prev]['code'], $allowed, true) === true) {
165+
if (isset($allowed[$tokens[$prev]['code']]) === true) {
166166
continue;
167167
}
168168

@@ -178,7 +178,7 @@ public function process(File $phpcsFile, $stackPtr)
178178
}
179179

180180
for ($next = ($stackPtr + 1); $next < $endBracket; $next++) {
181-
if (in_array($tokens[$next]['code'], $allowed, true) === true) {
181+
if (isset($allowed[$tokens[$next]['code']]) === true) {
182182
continue;
183183
}
184184

0 commit comments

Comments
 (0)