|
30 | 30 | use const T_MOD_EQUAL; |
31 | 31 | use const T_MUL_EQUAL; |
32 | 32 | use const T_OBJECT_OPERATOR; |
| 33 | +use const T_OPEN_SHORT_ARRAY; |
33 | 34 | use const T_OPEN_TAG; |
34 | 35 | use const T_OR_EQUAL; |
35 | 36 | use const T_PLUS_EQUAL; |
@@ -95,6 +96,10 @@ public function process(File $phpcsFile, $variablePointer): void |
95 | 96 | return; |
96 | 97 | } |
97 | 98 |
|
| 99 | + if ($this->isUsedAsKeyOrValueInArray($phpcsFile, $variablePointer)) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
98 | 103 | $scopeOwnerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_TAG, $variablePointer - 1); |
99 | 104 | foreach (array_reverse($tokens[$variablePointer]['conditions'], true) as $conditionPointer => $conditionTokenCode) { |
100 | 105 | if (in_array($conditionTokenCode, TokenHelper::$functionTokenCodes, true)) { |
@@ -316,6 +321,29 @@ private function isUsedInLoop(File $phpcsFile, int $variablePointer, string $var |
316 | 321 | return false; |
317 | 322 | } |
318 | 323 |
|
| 324 | + private function isUsedAsKeyOrValueInArray(File $phpcsFile, int $variablePointer): bool |
| 325 | + { |
| 326 | + $tokens = $phpcsFile->getTokens(); |
| 327 | + |
| 328 | + $arrayOpenerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_SHORT_ARRAY, $variablePointer - 1); |
| 329 | + if ($arrayOpenerPointer === null) { |
| 330 | + return false; |
| 331 | + } |
| 332 | + |
| 333 | + $arrayCloserPointer = $tokens[$arrayOpenerPointer]['bracket_closer']; |
| 334 | + if ($arrayCloserPointer < $variablePointer) { |
| 335 | + return false; |
| 336 | + } |
| 337 | + |
| 338 | + $pointerAfterArrayCloser = TokenHelper::findNextEffective($phpcsFile, $arrayCloserPointer + 1); |
| 339 | + if ($tokens[$pointerAfterArrayCloser]['code'] === T_EQUAL) { |
| 340 | + return false; |
| 341 | + } |
| 342 | + |
| 343 | + $pointerBeforeVariable = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1); |
| 344 | + return in_array($tokens[$pointerBeforeVariable]['code'], [T_OPEN_SHORT_ARRAY, T_COMMA, T_DOUBLE_ARROW], true); |
| 345 | + } |
| 346 | + |
319 | 347 | private function isStaticVariable(File $phpcsFile, int $functionPointer, string $variableName): bool |
320 | 348 | { |
321 | 349 | $tokens = $phpcsFile->getTokens(); |
|
0 commit comments