Skip to content

Commit 78089cb

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 d44b114 commit 78089cb

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

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

+19-19
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,23 @@ public function process(File $phpcsFile, $stackPtr)
133133

134134
// Tokens that are allowed inside a bracketed operation.
135135
$allowed = [
136-
T_VARIABLE,
137-
T_LNUMBER,
138-
T_DNUMBER,
139-
T_STRING,
140-
T_WHITESPACE,
141-
T_NS_SEPARATOR,
142-
T_THIS,
143-
T_SELF,
144-
T_STATIC,
145-
T_PARENT,
146-
T_OBJECT_OPERATOR,
147-
T_NULLSAFE_OBJECT_OPERATOR,
148-
T_DOUBLE_COLON,
149-
T_OPEN_SQUARE_BRACKET,
150-
T_CLOSE_SQUARE_BRACKET,
151-
T_NONE,
152-
T_BITWISE_NOT,
136+
T_VARIABLE => T_VARIABLE,
137+
T_LNUMBER => T_LNUMBER,
138+
T_DNUMBER => T_DNUMBER,
139+
T_STRING => T_STRING,
140+
T_WHITESPACE => T_WHITESPACE,
141+
T_NS_SEPARATOR => T_NS_SEPARATOR,
142+
T_THIS => T_THIS,
143+
T_SELF => T_SELF,
144+
T_STATIC => T_STATIC,
145+
T_PARENT => T_PARENT,
146+
T_OBJECT_OPERATOR => T_OBJECT_OPERATOR,
147+
T_NULLSAFE_OBJECT_OPERATOR => T_NULLSAFE_OBJECT_OPERATOR,
148+
T_DOUBLE_COLON => T_DOUBLE_COLON,
149+
T_OPEN_SQUARE_BRACKET => T_OPEN_SQUARE_BRACKET,
150+
T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET,
151+
T_NONE => T_NONE,
152+
T_BITWISE_NOT => T_BITWISE_NOT,
153153
];
154154

155155
$allowed += Tokens::$operators;
@@ -171,7 +171,7 @@ public function process(File $phpcsFile, $stackPtr)
171171
// We allow simple operations to not be bracketed.
172172
// For example, ceil($one / $two).
173173
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
174-
if (in_array($tokens[$prev]['code'], $allowed, true) === true) {
174+
if (isset($allowed[$tokens[$prev]['code']]) === true) {
175175
continue;
176176
}
177177

@@ -187,7 +187,7 @@ public function process(File $phpcsFile, $stackPtr)
187187
}
188188

189189
for ($next = ($stackPtr + 1); $next < $endBracket; $next++) {
190-
if (in_array($tokens[$next]['code'], $allowed, true) === true) {
190+
if (isset($allowed[$tokens[$next]['code']]) === true) {
191191
continue;
192192
}
193193

0 commit comments

Comments
 (0)