Skip to content

Commit

Permalink
Squiz OperatorSpacingSniff no longer reports errors for the assignmen…
Browse files Browse the repository at this point in the history
…t of operations involving negative numbers
  • Loading branch information
gsherwood committed Nov 21, 2012
1 parent 24bc874 commit 4281aa1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// a minus value or returning one.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$prev]['code'] === T_RETURN) {
// Just returning a negative value; eg. return -1.
// Just returning a negative value; eg. (return -1).
return;
}

Expand All @@ -150,6 +150,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}

if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === true) {
// Just trying to assign a negative value; eg. ($var = -1).
return;
}

// A list of tokens that indicate that the token is not
// part of an arithmetic operation.
$invalidTokens = array(
Expand All @@ -167,17 +172,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// Just trying to use a negative value; eg. myFunction($var, -2).
return;
}

$number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if (in_array($tokens[$number]['code'], array(T_LNUMBER, T_VARIABLE)) === true) {
$semi = $phpcsFile->findNext(T_WHITESPACE, ($number + 1), null, true);
if ($tokens[$semi]['code'] === T_SEMICOLON) {
if ($prev !== false && (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === true)) {
// This is a negative assignment.
return;
}
}
}
}//end if

$operator = $tokens[$stackPtr]['content'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,14 @@ switch ($foo) {
case -1:
break;
}

$y = 1 * -1;
$y = -1 * 1;
$y = -1 * $var;
$y = 10 / -2;
$y = -10 / 2;
$y = (-10 / 2);
$y = (-10 / $var);
$y = 10 + -2;
$y = -10 + 2;
?>
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Marc Ypes for the patch
- Squiz OperatorSpacingSniff no longer reports errors for negative numbers inside inline THEN statements
-- Thanks to Klaus Purer for the patch
- Squiz OperatorSpacingSniff no longer reports errors for the assignment of operations involving negative numbers
- Fixed bug #19699 : Generic.Files.LineLength giving false positives when tab-width is used
</notes>
<contents>
Expand Down

0 comments on commit 4281aa1

Please sign in to comment.