|
6 | 6 | use PHP_CodeSniffer\Sniffs\Sniff; |
7 | 7 | use SlevomatCodingStandard\Helpers\ScopeHelper; |
8 | 8 | use SlevomatCodingStandard\Helpers\TokenHelper; |
9 | | -use const T_ARRAY; |
10 | 9 | use const T_CLOSURE; |
11 | 10 | use const T_DOUBLE_COLON; |
12 | 11 | use const T_EQUAL; |
| 12 | +use const T_FOREACH; |
| 13 | +use const T_LIST; |
13 | 14 | use const T_OBJECT_OPERATOR; |
| 15 | +use const T_OPEN_PARENTHESIS; |
14 | 16 | use const T_OPEN_SHORT_ARRAY; |
15 | 17 | use const T_OPEN_SQUARE_BRACKET; |
16 | 18 | use const T_OPEN_TAG; |
| 19 | +use const T_STRING; |
17 | 20 | use const T_USE; |
18 | 21 | use const T_VARIABLE; |
| 22 | +use function array_key_exists; |
19 | 23 | use function array_reverse; |
20 | 24 | use function count; |
21 | 25 | use function in_array; |
@@ -55,6 +59,20 @@ public function process(File $phpcsFile, $bracketOpenerPointer): void |
55 | 59 | return; |
56 | 60 | } |
57 | 61 |
|
| 62 | + if (in_array($tokens[$variablePointer]['content'], [ |
| 63 | + '$GLOBALS', |
| 64 | + '$_SERVER', |
| 65 | + '$_REQUEST', |
| 66 | + '$_POST', |
| 67 | + '$_GET', |
| 68 | + '$_FILES', |
| 69 | + '$_ENV', |
| 70 | + '$_COOKIE', |
| 71 | + '$_SESSION', |
| 72 | + ], true)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
58 | 76 | $pointerBeforeVariable = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1); |
59 | 77 | if (in_array($tokens[$pointerBeforeVariable]['code'], [T_OBJECT_OPERATOR, T_DOUBLE_COLON], true)) { |
60 | 78 | return; |
@@ -134,22 +152,78 @@ private function hasExplicitCreation(File $phpcsFile, int $scopeOpenerPointer, i |
134 | 152 | continue; |
135 | 153 | } |
136 | 154 |
|
137 | | - $assigmentPointer = TokenHelper::findNextEffective($phpcsFile, $i + 1); |
138 | | - if ($tokens[$assigmentPointer]['code'] !== T_EQUAL) { |
| 155 | + if (!ScopeHelper::isInSameScope($phpcsFile, $variablePointer, $i)) { |
139 | 156 | continue; |
140 | 157 | } |
141 | 158 |
|
142 | | - $arrayPointer = TokenHelper::findNextEffective($phpcsFile, $assigmentPointer + 1); |
143 | | - if (!in_array($tokens[$arrayPointer]['code'], [T_ARRAY, T_OPEN_SHORT_ARRAY], true)) { |
144 | | - continue; |
| 159 | + $assigmentPointer = TokenHelper::findNextEffective($phpcsFile, $i + 1); |
| 160 | + if ($tokens[$assigmentPointer]['code'] === T_EQUAL) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + if ($this->isCreatedInList($phpcsFile, $i, $scopeOpenerPointer)) { |
| 165 | + return true; |
145 | 166 | } |
146 | 167 |
|
147 | | - if (ScopeHelper::isInSameScope($phpcsFile, $variablePointer, $i)) { |
| 168 | + if ($this->isCreatedInForeach($phpcsFile, $i, $scopeOpenerPointer)) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + if ($this->isCreatedByReferencedParameterInFunctionCall($phpcsFile, $i, $scopeOpenerPointer)) { |
148 | 173 | return true; |
149 | 174 | } |
150 | 175 | } |
151 | 176 |
|
152 | 177 | return false; |
153 | 178 | } |
154 | 179 |
|
| 180 | + private function isCreatedInList(File $phpcsFile, int $variablePointer, int $scopeOpenerPointer): bool |
| 181 | + { |
| 182 | + $tokens = $phpcsFile->getTokens(); |
| 183 | + |
| 184 | + $parenthesisOpenerPointer = TokenHelper::findPrevious($phpcsFile, [T_OPEN_PARENTHESIS, T_OPEN_SHORT_ARRAY, T_OPEN_SQUARE_BRACKET], $variablePointer - 1, $scopeOpenerPointer); |
| 185 | + if ($parenthesisOpenerPointer === null) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + if ($tokens[$parenthesisOpenerPointer]['code'] === T_OPEN_PARENTHESIS) { |
| 190 | + if ($tokens[$parenthesisOpenerPointer]['parenthesis_closer'] < $variablePointer) { |
| 191 | + return false; |
| 192 | + } |
| 193 | + |
| 194 | + $pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1); |
| 195 | + return $tokens[$pointerBeforeParenthesisOpener]['code'] === T_LIST; |
| 196 | + } |
| 197 | + |
| 198 | + return $tokens[$parenthesisOpenerPointer]['bracket_closer'] > $variablePointer; |
| 199 | + } |
| 200 | + |
| 201 | + private function isCreatedInForeach(File $phpcsFile, int $variablePointer, int $scopeOpenerPointer): bool |
| 202 | + { |
| 203 | + $tokens = $phpcsFile->getTokens(); |
| 204 | + |
| 205 | + $parenthesisOpenerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_PARENTHESIS, $variablePointer - 1, $scopeOpenerPointer); |
| 206 | + return $parenthesisOpenerPointer !== null |
| 207 | + && array_key_exists('parenthesis_owner', $tokens[$parenthesisOpenerPointer]) |
| 208 | + && $tokens[$tokens[$parenthesisOpenerPointer]['parenthesis_owner']]['code'] === T_FOREACH |
| 209 | + && $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] > $variablePointer; |
| 210 | + } |
| 211 | + |
| 212 | + private function isCreatedByReferencedParameterInFunctionCall(File $phpcsFile, int $variablePointer, int $scopeOpenerPointer): bool |
| 213 | + { |
| 214 | + $tokens = $phpcsFile->getTokens(); |
| 215 | + |
| 216 | + $parenthesisOpenerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_PARENTHESIS, $variablePointer - 1, $scopeOpenerPointer); |
| 217 | + |
| 218 | + if ( |
| 219 | + $parenthesisOpenerPointer === null |
| 220 | + || $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] < $variablePointer |
| 221 | + ) { |
| 222 | + return false; |
| 223 | + } |
| 224 | + |
| 225 | + $pointerBeforeParenthesisOpener = TokenHelper::findPreviousEffective($phpcsFile, $parenthesisOpenerPointer - 1); |
| 226 | + return $tokens[$pointerBeforeParenthesisOpener]['code'] === T_STRING; |
| 227 | + } |
| 228 | + |
155 | 229 | } |
0 commit comments