3
* Squiz_Sniffs_Formatting_OperationBracketSniff.
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,v 1.10 2008/03/17 03:52:43 squiz Exp $
14
* @link http://pear.php.net/package/PHP_CodeSniffer
18
* Squiz_Sniffs_Formatting_OperationBracketSniff.
20
* Tests that all arithmetic operations are bracketed.
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.1.0
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Squiz_Sniffs_Formatting_OperatorBracketSniff implements PHP_CodeSniffer_Sniff
35
* A list of tokenizers this sniff supports.
39
public $supportedTokenizers = array(
46
* Returns an array of tokens this test wants to listen for.
50
public function register()
52
return PHP_CodeSniffer_Tokens::$operators;
58
* Processes this test, when one of its tokens is encountered.
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.
66
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
68
$tokens = $phpcsFile->getTokens();
70
// If the & is a reference, then we don't want to check for brackets.
71
if ($tokens[$stackPtr]['code'] === T_BITWISE_AND && $phpcsFile->isReference($stackPtr) === true) {
75
// There is one instance where brackets aren't needed, which involves
76
// the minus sign being used to assign a negative number to a variable.
77
if ($tokens[$stackPtr]['code'] === T_MINUS) {
78
// Check to see if we are trying to return -n.
79
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
80
if ($tokens[$prev]['code'] === T_RETURN) {
84
$number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
85
if ($tokens[$number]['code'] === T_LNUMBER || $tokens[$number]['code'] === T_DNUMBER) {
86
$previous = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
87
if ($previous !== false) {
88
$isAssignment = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens);
89
$isEquality = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$equalityTokens);
90
$isComparison = in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$comparisonTokens);
91
if ($isAssignment === true || $isEquality === true || $isComparison === true) {
92
// This is a negative assignment or comparion.
93
// We need to check that the minus and the number are
95
if (($number - $stackPtr) !== 1) {
96
$error = 'No space allowed between minus sign and number';
97
$phpcsFile->addError($error, $stackPtr);
106
$lastBracket = false;
107
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
108
$parenthesis = array_reverse($tokens[$stackPtr]['nested_parenthesis'], true);
109
foreach ($parenthesis as $bracket => $endBracket) {
110
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($bracket - 1), null, true);
111
$prevCode = $tokens[$prevToken]['code'];
113
if ($prevCode === T_ISSET) {
114
// This operation is inside an isset() call, but has
115
// no bracket of it's own.
119
if ($prevCode === T_STRING) {
120
// We allow very simple operations to not be bracketed:
121
// ceil($one / $two);
130
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
131
if (in_array($tokens[$prev]['code'], $allowed) === true) {
135
if ($tokens[$prev]['code'] === T_CLOSE_PARENTHESIS) {
136
$prev = $tokens[$prev]['parenthesis_opener'];
142
if ($prev !== $bracket) {
146
for ($next = ($stackPtr + 1); $next < $endBracket; $next++) {
147
if (in_array($tokens[$next]['code'], $allowed) === true) {
151
if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
152
$next = $tokens[$next]['parenthesis_closer'];
158
if ($next !== $endBracket) {
163
if (in_array($prevCode, PHP_CodeSniffer_Tokens::$scopeOpeners) === true) {
164
// This operation is inside an a control structure like FOREACH
165
// or IF, but has no bracket of it's own.
169
if ($prevCode === T_OPEN_PARENTHESIS) {
170
// These are two open parenthesis in a row. If the current
171
// one doesn't enclose the operator, go to the previous one.
172
if ($endBracket < $stackPtr) {
177
$lastBracket = $bracket;
182
if ($lastBracket === false) {
183
// It is not in a bracketed statement at all.
184
$previousToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true, null, true);
185
if ($previousToken !== false) {
186
// A list of tokens that indicate that the token is not
187
// part of an arithmetic operation.
188
$invalidTokens = array(
191
T_OPEN_SQUARE_BRACKET,
194
if (in_array($tokens[$previousToken]['code'], $invalidTokens) === false) {
195
$error = 'Arithmetic operation must be bracketed';
196
$phpcsFile->addError($error, $stackPtr);
201
} else if ($tokens[$lastBracket]['parenthesis_closer'] < $stackPtr) {
202
// There are a set of brackets in front of it that don't include it.
203
$error = 'Arithmetic operation must be bracketed';
204
$phpcsFile->addError($error, $stackPtr);
207
// We are enclosed in a set of bracket, so the last thing to
208
// check is that we are not also enclosed in square brackets
209
// like this: ($array[$index + 1]), which is invalid.
211
T_OPEN_SQUARE_BRACKET,
212
T_CLOSE_SQUARE_BRACKET,
215
$squareBracket = $phpcsFile->findPrevious($brackets, ($stackPtr - 1), $lastBracket);
216
if ($squareBracket !== false && $tokens[$squareBracket]['code'] === T_OPEN_SQUARE_BRACKET) {
217
$closeSquareBracket = $phpcsFile->findNext($brackets, ($stackPtr + 1));
218
if ($closeSquareBracket !== false && $tokens[$closeSquareBracket]['code'] === T_CLOSE_SQUARE_BRACKET) {
219
$error = 'Arithmetic operation must be bracketed';
220
$phpcsFile->addError($error, $stackPtr);
227
$lastAssignment = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$assignmentTokens, $stackPtr, null, false, null, true);
228
if ($lastAssignment !== false && $lastAssignment > $lastBracket) {
229
$error = 'Arithmetic operation must be bracketed';
230
$phpcsFile->addError($error, $stackPtr);