| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_Formatting_OperationBracketSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: OperatorBracketSniff.php 276658 2009-03-02 04:27:30Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Squiz_Sniffs_Formatting_OperationBracketSniff.
|
|
|
19 |
*
|
|
|
20 |
* Tests that all arithmetic operations are bracketed.
|
|
|
21 |
*
|
|
|
22 |
* @category PHP
|
|
|
23 |
* @package PHP_CodeSniffer
|
|
|
24 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
25 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
26 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
27 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
28 |
* @version Release: 1.2.1
|
|
|
29 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
30 |
*/
|
|
|
31 |
class Squiz_Sniffs_Formatting_OperatorBracketSniff implements PHP_CodeSniffer_Sniff
|
|
|
32 |
{
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* A list of tokenizers this sniff supports.
|
|
|
36 |
*
|
|
|
37 |
* @var array
|
|
|
38 |
*/
|
|
|
39 |
public $supportedTokenizers = array(
|
|
|
40 |
'PHP',
|
|
|
41 |
'JS',
|
|
|
42 |
);
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Returns an array of tokens this test wants to listen for.
|
|
|
47 |
*
|
|
|
48 |
* @return array
|
|
|
49 |
*/
|
|
|
50 |
public function register()
|
|
|
51 |
{
|
|
|
52 |
return PHP_CodeSniffer_Tokens::$operators;
|
|
|
53 |
|
|
|
54 |
}//end register()
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Processes this test, when one of its tokens is encountered.
|
|
|
59 |
*
|
|
|
60 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
61 |
* @param int $stackPtr The position of the current token in the
|
|
|
62 |
* stack passed in $tokens.
|
|
|
63 |
*
|
|
|
64 |
* @return void
|
|
|
65 |
*/
|
|
|
66 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
67 |
{
|
|
|
68 |
$tokens = $phpcsFile->getTokens();
|
|
|
69 |
|
|
|
70 |
if ($phpcsFile->tokenizerType === 'JS' && $tokens[$stackPtr]['code'] === T_PLUS) {
|
|
|
71 |
// JavaScript uses the plus operator for string concatenation as well
|
|
|
72 |
// so we cannot accurately determine if it is a string concat or addition.
|
|
|
73 |
// So just ignore it.
|
|
|
74 |
return;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// If the & is a reference, then we don't want to check for brackets.
|
|
|
78 |
if ($tokens[$stackPtr]['code'] === T_BITWISE_AND && $phpcsFile->isReference($stackPtr) === true) {
|
|
|
79 |
return;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// There is one instance where brackets aren't needed, which involves
|
|
|
83 |
// the minus sign being used to assign a negative number to a variable.
|
|
|
84 |
if ($tokens[$stackPtr]['code'] === T_MINUS) {
|
|
|
85 |
// Check to see if we are trying to return -n.
|
|
|
86 |
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
|
|
|
87 |
if ($tokens[$prev]['code'] === T_RETURN) {
|
|
|
88 |
return;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
|
|
92 |
if ($tokens[$number]['code'] === T_LNUMBER || $tokens[$number]['code'] === T_DNUMBER) {
|
|
|
93 |
$previous = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
|
|
|
94 |
if ($previous !== false) {
|
|
|
95 |
$isAssignment = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens);
|
|
|
96 |
$isEquality = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$equalityTokens);
|
|
|
97 |
$isComparison = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$comparisonTokens);
|
|
|
98 |
if ($isAssignment === true || $isEquality === true || $isComparison === true) {
|
|
|
99 |
// This is a negative assignment or comparion.
|
|
|
100 |
// We need to check that the minus and the number are
|
|
|
101 |
// adjacent.
|
|
|
102 |
if (($number - $stackPtr) !== 1) {
|
|
|
103 |
$error = 'No space allowed between minus sign and number';
|
|
|
104 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
return;
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
}//end if
|
|
|
112 |
|
|
|
113 |
$lastBracket = false;
|
|
|
114 |
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
|
|
|
115 |
$parenthesis = array_reverse($tokens[$stackPtr]['nested_parenthesis'], true);
|
|
|
116 |
foreach ($parenthesis as $bracket => $endBracket) {
|
|
|
117 |
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($bracket - 1), null, true);
|
|
|
118 |
$prevCode = $tokens[$prevToken]['code'];
|
|
|
119 |
|
|
|
120 |
if ($prevCode === T_ISSET) {
|
|
|
121 |
// This operation is inside an isset() call, but has
|
|
|
122 |
// no bracket of it's own.
|
|
|
123 |
break;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
if ($prevCode === T_STRING) {
|
|
|
127 |
// We allow very simple operations to not be bracketed:
|
|
|
128 |
// ceil($one / $two);
|
|
|
129 |
$allowed = array(
|
|
|
130 |
T_VARIABLE,
|
|
|
131 |
T_LNUMBER,
|
|
|
132 |
T_DNUMBER,
|
|
|
133 |
T_STRING,
|
|
|
134 |
T_WHITESPACE,
|
|
|
135 |
T_THIS,
|
|
|
136 |
T_OBJECT_OPERATOR,
|
|
|
137 |
T_OPEN_SQUARE_BRACKET,
|
|
|
138 |
T_CLOSE_SQUARE_BRACKET,
|
|
|
139 |
);
|
|
|
140 |
|
|
|
141 |
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
|
|
|
142 |
if (in_array($tokens[$prev]['code'], $allowed) === true) {
|
|
|
143 |
continue;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
if ($tokens[$prev]['code'] === T_CLOSE_PARENTHESIS) {
|
|
|
147 |
$prev = $tokens[$prev]['parenthesis_opener'];
|
|
|
148 |
} else {
|
|
|
149 |
break;
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
if ($prev !== $bracket) {
|
|
|
154 |
break;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
for ($next = ($stackPtr + 1); $next < $endBracket; $next++) {
|
|
|
158 |
if (in_array($tokens[$next]['code'], $allowed) === true) {
|
|
|
159 |
continue;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
|
|
|
163 |
$next = $tokens[$next]['parenthesis_closer'];
|
|
|
164 |
} else {
|
|
|
165 |
break;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
if ($next !== $endBracket) {
|
|
|
170 |
break;
|
|
|
171 |
}
|
|
|
172 |
}//end if
|
|
|
173 |
|
|
|
174 |
if (in_array($prevCode, PHP_CodeSniffer_Tokens::$scopeOpeners) === true) {
|
|
|
175 |
// This operation is inside an a control structure like FOREACH
|
|
|
176 |
// or IF, but has no bracket of it's own.
|
|
|
177 |
break;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
if ($prevCode === T_OPEN_PARENTHESIS) {
|
|
|
181 |
// These are two open parenthesis in a row. If the current
|
|
|
182 |
// one doesn't enclose the operator, go to the previous one.
|
|
|
183 |
if ($endBracket < $stackPtr) {
|
|
|
184 |
continue;
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
$lastBracket = $bracket;
|
|
|
189 |
break;
|
|
|
190 |
}//end foreach
|
|
|
191 |
}//end if
|
|
|
192 |
|
|
|
193 |
if ($lastBracket === false) {
|
|
|
194 |
// It is not in a bracketed statement at all.
|
|
|
195 |
$previousToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true, null, true);
|
|
|
196 |
if ($previousToken !== false) {
|
|
|
197 |
// A list of tokens that indicate that the token is not
|
|
|
198 |
// part of an arithmetic operation.
|
|
|
199 |
$invalidTokens = array(
|
|
|
200 |
T_COMMA,
|
|
|
201 |
T_COLON,
|
|
|
202 |
T_OPEN_PARENTHESIS,
|
|
|
203 |
T_OPEN_SQUARE_BRACKET,
|
|
|
204 |
);
|
|
|
205 |
|
|
|
206 |
if (in_array($tokens[$previousToken]['code'], $invalidTokens) === false) {
|
|
|
207 |
$error = 'Arithmetic operation must be bracketed';
|
|
|
208 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
return;
|
|
|
212 |
}
|
|
|
213 |
} else if ($tokens[$lastBracket]['parenthesis_closer'] < $stackPtr) {
|
|
|
214 |
// There are a set of brackets in front of it that don't include it.
|
|
|
215 |
$error = 'Arithmetic operation must be bracketed';
|
|
|
216 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
217 |
return;
|
|
|
218 |
} else {
|
|
|
219 |
// We are enclosed in a set of bracket, so the last thing to
|
|
|
220 |
// check is that we are not also enclosed in square brackets
|
|
|
221 |
// like this: ($array[$index + 1]), which is invalid.
|
|
|
222 |
$brackets = array(
|
|
|
223 |
T_OPEN_SQUARE_BRACKET,
|
|
|
224 |
T_CLOSE_SQUARE_BRACKET,
|
|
|
225 |
);
|
|
|
226 |
|
|
|
227 |
$squareBracket = $phpcsFile->findPrevious($brackets, ($stackPtr - 1), $lastBracket);
|
|
|
228 |
if ($squareBracket !== false && $tokens[$squareBracket]['code'] === T_OPEN_SQUARE_BRACKET) {
|
|
|
229 |
$closeSquareBracket = $phpcsFile->findNext($brackets, ($stackPtr + 1));
|
|
|
230 |
if ($closeSquareBracket !== false && $tokens[$closeSquareBracket]['code'] === T_CLOSE_SQUARE_BRACKET) {
|
|
|
231 |
$error = 'Arithmetic operation must be bracketed';
|
|
|
232 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
return;
|
|
|
237 |
}//end if
|
|
|
238 |
|
|
|
239 |
$lastAssignment = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$assignmentTokens, $stackPtr, null, false, null, true);
|
|
|
240 |
if ($lastAssignment !== false && $lastAssignment > $lastBracket) {
|
|
|
241 |
$error = 'Arithmetic operation must be bracketed';
|
|
|
242 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
}//end process()
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
}//end class
|
|
|
249 |
|
|
|
250 |
?>
|