-
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathOperatorBracketSniff.php
More file actions
372 lines (323 loc) · 15.2 KB
/
OperatorBracketSniff.php
File metadata and controls
372 lines (323 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* Tests that all arithmetic operations are bracketed.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2023 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/HEAD/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Formatting;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class OperatorBracketSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return Tokens::OPERATORS;
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, int $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// If the & is a reference, then we don't want to check for brackets.
if ($tokens[$stackPtr]['code'] === T_BITWISE_AND && $phpcsFile->isReference($stackPtr) === true) {
return;
}
// There is one instance where brackets aren't needed, which involves
// the minus sign being used to assign a negative number to a variable.
if ($tokens[$stackPtr]['code'] === T_MINUS) {
// Check to see if we are trying to return -n.
$prev = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
if ($tokens[$prev]['code'] === T_RETURN) {
return;
}
$number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($tokens[$number]['code'] === T_LNUMBER || $tokens[$number]['code'] === T_DNUMBER) {
$previous = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($previous !== false) {
$isAssignment = isset(Tokens::ASSIGNMENT_TOKENS[$tokens[$previous]['code']]);
$isEquality = isset(Tokens::EQUALITY_TOKENS[$tokens[$previous]['code']]);
$isComparison = isset(Tokens::COMPARISON_TOKENS[$tokens[$previous]['code']]);
$isUnary = isset(Tokens::OPERATORS[$tokens[$previous]['code']]);
if ($isAssignment === true || $isEquality === true || $isComparison === true || $isUnary === true) {
// This is a negative assignment or comparison.
// We need to check that the minus and the number are
// adjacent.
if (($number - $stackPtr) !== 1) {
$error = 'No space allowed between minus sign and number';
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterMinus');
}
return;
}
}
}
}
$previousToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true, null, true);
if ($previousToken !== false) {
// A list of tokens that indicate that the token is not
// part of an arithmetic operation.
$invalidTokens = [
T_COMMA => true,
T_COLON => true,
T_OPEN_PARENTHESIS => true,
T_OPEN_SQUARE_BRACKET => true,
T_OPEN_CURLY_BRACKET => true,
T_OPEN_SHORT_ARRAY => true,
T_CASE => true,
T_EXIT => true,
T_MATCH_ARROW => true,
];
if (isset($invalidTokens[$tokens[$previousToken]['code']]) === true) {
return;
}
}
if ($tokens[$stackPtr]['code'] === T_BITWISE_OR
&& isset($tokens[$stackPtr]['nested_parenthesis']) === true
) {
$brackets = $tokens[$stackPtr]['nested_parenthesis'];
$lastBracket = array_pop($brackets);
if (isset($tokens[$lastBracket]['parenthesis_owner']) === true
&& $tokens[$tokens[$lastBracket]['parenthesis_owner']]['code'] === T_CATCH
) {
// This is a pipe character inside a catch statement, so it is acting
// as an exception type separator and not an arithmetic operation.
return;
}
}
// Tokens that are allowed inside a bracketed operation.
$allowed = Tokens::NAME_TOKENS;
$allowed += Tokens::OPERATORS;
$allowed += [
T_VARIABLE => T_VARIABLE,
T_LNUMBER => T_LNUMBER,
T_DNUMBER => T_DNUMBER,
T_WHITESPACE => T_WHITESPACE,
T_SELF => T_SELF,
T_STATIC => T_STATIC,
T_PARENT => T_PARENT,
T_OBJECT_OPERATOR => T_OBJECT_OPERATOR,
T_NULLSAFE_OBJECT_OPERATOR => T_NULLSAFE_OBJECT_OPERATOR,
T_DOUBLE_COLON => T_DOUBLE_COLON,
T_OPEN_SQUARE_BRACKET => T_OPEN_SQUARE_BRACKET,
T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET,
T_NONE => T_NONE,
T_BITWISE_NOT => T_BITWISE_NOT,
];
$lastBracket = false;
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
$parentheses = $tokens[$stackPtr]['nested_parenthesis'];
$parentheses = array_reverse($parentheses, true);
foreach ($parentheses as $bracket => $endBracket) {
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($bracket - 1), null, true);
$prevCode = $tokens[$prevToken]['code'];
if ($prevCode === T_ISSET) {
// This operation is inside an isset() call, but has
// no bracket of it's own.
break;
}
if (isset(Tokens::NAME_TOKENS[$prevCode]) === true
|| $prevCode === T_SWITCH
|| $prevCode === T_MATCH
) {
// We allow simple operations to not be bracketed.
// For example, ceil($one / $two).
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
if (isset($allowed[$tokens[$prev]['code']]) === true) {
continue;
}
if ($tokens[$prev]['code'] === T_CLOSE_PARENTHESIS) {
$prev = $tokens[$prev]['parenthesis_opener'];
} else {
break;
}
}
if ($prev !== $bracket) {
break;
}
for ($next = ($stackPtr + 1); $next < $endBracket; $next++) {
if (isset($allowed[$tokens[$next]['code']]) === true) {
continue;
}
if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
$next = $tokens[$next]['parenthesis_closer'];
} else {
break;
}
}
if ($next !== $endBracket) {
break;
}
}
if (in_array($prevCode, Tokens::SCOPE_OPENERS, true) === true) {
// This operation is inside a control structure like FOREACH
// or IF, but has no bracket of it's own.
// The only control structures allowed to do this are SWITCH and MATCH.
if ($prevCode !== T_SWITCH && $prevCode !== T_MATCH) {
break;
}
}
if ($prevCode === T_OPEN_PARENTHESIS) {
// These are two open parenthesis in a row. If the current
// one doesn't enclose the operator, go to the previous one.
if ($endBracket < $stackPtr) {
continue;
}
}
$lastBracket = $bracket;
break;
}
}
if ($lastBracket === false) {
// It is not in a bracketed statement at all.
$this->addMissingBracketsError($phpcsFile, $stackPtr);
return;
} elseif ($tokens[$lastBracket]['parenthesis_closer'] < $stackPtr) {
// There are a set of brackets in front of it that don't include it.
$this->addMissingBracketsError($phpcsFile, $stackPtr);
return;
} else {
// We are enclosed in a set of bracket, so the last thing to
// check is that we are not also enclosed in square brackets
// like this: ($array[$index + 1]), which is invalid.
$brackets = [
T_OPEN_SQUARE_BRACKET,
T_CLOSE_SQUARE_BRACKET,
];
$squareBracket = $phpcsFile->findPrevious($brackets, ($stackPtr - 1), $lastBracket);
if ($squareBracket !== false && $tokens[$squareBracket]['code'] === T_OPEN_SQUARE_BRACKET) {
$closeSquareBracket = $phpcsFile->findNext($brackets, ($stackPtr + 1));
if ($closeSquareBracket !== false && $tokens[$closeSquareBracket]['code'] === T_CLOSE_SQUARE_BRACKET) {
$this->addMissingBracketsError($phpcsFile, $stackPtr);
}
}
return;
}
$lastAssignment = $phpcsFile->findPrevious(Tokens::ASSIGNMENT_TOKENS, $stackPtr, null, false, null, true);
if ($lastAssignment !== false && $lastAssignment > $lastBracket) {
$this->addMissingBracketsError($phpcsFile, $stackPtr);
}
}
/**
* Add and fix the missing brackets error.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function addMissingBracketsError(File $phpcsFile, int $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$allowed = [
T_VARIABLE => true,
T_LNUMBER => true,
T_DNUMBER => true,
T_CONSTANT_ENCAPSED_STRING => true,
T_DOUBLE_QUOTED_STRING => true,
T_WHITESPACE => true,
T_SELF => true,
T_STATIC => true,
T_OBJECT_OPERATOR => true,
T_NULLSAFE_OBJECT_OPERATOR => true,
T_DOUBLE_COLON => true,
T_ISSET => true,
T_ARRAY => true,
T_NONE => true,
T_BITWISE_NOT => true,
];
// Find the first token in the expression.
for ($before = ($stackPtr - 1); $before > 0; $before--) {
if (isset(Tokens::EMPTY_TOKENS[$tokens[$before]['code']]) === true
|| isset(Tokens::OPERATORS[$tokens[$before]['code']]) === true
|| isset(Tokens::CAST_TOKENS[$tokens[$before]['code']]) === true
|| isset(Tokens::NAME_TOKENS[$tokens[$before]['code']]) === true
|| isset($allowed[$tokens[$before]['code']]) === true
) {
continue;
}
if ($tokens[$before]['code'] === T_CLOSE_PARENTHESIS) {
$before = $tokens[$before]['parenthesis_opener'];
continue;
}
if ($tokens[$before]['code'] === T_CLOSE_SQUARE_BRACKET) {
$before = $tokens[$before]['bracket_opener'];
continue;
}
if ($tokens[$before]['code'] === T_CLOSE_SHORT_ARRAY) {
$before = $tokens[$before]['bracket_opener'];
continue;
}
break;
}
$before = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($before + 1), null, true);
if ($tokens[$before]['code'] === T_VOID_CAST) {
// Don't throw an error if a (void) cast is the first token in the expression
// as adding parentheses would turn that into a parse error.
return;
}
// A few extra tokens are allowed to be on the right side of the expression.
$allowed[T_EQUAL] = true;
$allowed[T_NEW] = true;
// Find the last token in the expression.
for ($after = ($stackPtr + 1); $after < $phpcsFile->numTokens; $after++) {
if (isset(Tokens::EMPTY_TOKENS[$tokens[$after]['code']]) === true
|| isset(Tokens::OPERATORS[$tokens[$after]['code']]) === true
|| isset(Tokens::CAST_TOKENS[$tokens[$after]['code']]) === true
|| isset(Tokens::NAME_TOKENS[$tokens[$after]['code']]) === true
|| isset($allowed[$tokens[$after]['code']]) === true
) {
continue;
}
if ($tokens[$after]['code'] === T_OPEN_PARENTHESIS) {
if (isset($tokens[$after]['parenthesis_closer']) === false) {
// Live coding/parse error. Ignore.
return;
}
$after = $tokens[$after]['parenthesis_closer'];
continue;
}
if (($tokens[$after]['code'] === T_OPEN_SQUARE_BRACKET
|| $tokens[$after]['code'] === T_OPEN_SHORT_ARRAY)
) {
if (isset($tokens[$after]['bracket_closer']) === false) {
// Live coding/parse error. Ignore.
return;
}
$after = $tokens[$after]['bracket_closer'];
continue;
}
break;
}
$after = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($after - 1), null, true);
$error = 'Operation must be bracketed';
if ($before === $after || $before === $stackPtr || $after === $stackPtr) {
$phpcsFile->addError($error, $stackPtr, 'MissingBrackets');
return;
}
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingBrackets');
if ($fix === true) {
// Can only fix this error if both tokens are available for fixing.
// Adding one bracket without the other will create parse errors.
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($before, '(' . $tokens[$before]['content']);
$phpcsFile->fixer->replaceToken($after, $tokens[$after]['content'] . ')');
$phpcsFile->fixer->endChangeset();
}
}
}