Skip to content

Commit

Permalink
Changelog + minor cleanup for #2640
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Nov 12, 2019
1 parent 6cf615d commit ed879f1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #2586 : Generic.WhiteSpace.ScopeIndent false positives when indenting open tags at a non tab-stop
- Fixed bug #2638 : Squiz.CSS.DuplicateClassDefinitionSniff sees comments as part of the class name
-- Thanks to Raphael Horber for the patch
- Fixed bug #2640 : Squiz.WhiteSpace.OperatorSpacing false positives for some negation operators
-- Thanks to Jakub Chábek for the patch
- Fixed bug #2674 : Squiz.Functions.FunctionDeclarationArgumentSpacing prints wrong argument name in error message
- Fixed bug #2676 : PSR12.Files.FileHeader locks up when file ends with multiple inline comments
- Fixed bug #2678 : PSR12.Classes.AnonClassDeclaration incorrectly enforcing that closing brace be on a line by itself
Expand Down
58 changes: 38 additions & 20 deletions src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,34 @@ class OperatorSpacingSniff implements Sniff
*/
public function register()
{
$targets = Tokens::$comparisonTokens;
$targets += Tokens::$operators;
$targets += Tokens::$assignmentTokens;
$targets[] = T_INLINE_THEN;
$targets[] = T_INLINE_ELSE;
$targets[] = T_INSTANCEOF;
/*
First we setup an array of all the tokens that can come before
a T_MINUS or T_PLUS token to indicate that the token is not being
used as an operator.
*/

// Trying to operate on a negative value; eg. ($var * -1).
$this->nonOperandTokens = Tokens::$operators;

// Trying to compare a negative value; eg. ($var === -1).
$this->nonOperandTokens += Tokens::$comparisonTokens;

// Trying to compare a negative value; eg. ($var || -1 === $b).
$this->nonOperandTokens += Tokens::$booleanOperators;

// Trying to assign a negative value; eg. ($var = -1).
$this->nonOperandTokens += Tokens::$assignmentTokens;

// Returning/printing a negative value; eg. (return -1).
$this->nonOperandTokens += [
// Returning/printing a negative value; eg. (return -1).
T_RETURN => T_RETURN,
T_ECHO => T_ECHO,
T_PRINT => T_PRINT,
T_YIELD => T_YIELD,
T_RETURN => T_RETURN,
T_ECHO => T_ECHO,
T_PRINT => T_PRINT,
T_YIELD => T_YIELD,
];

// Trying to use a negative value; eg. myFunction($var, -2).
// Trying to use a negative value; eg. myFunction($var, -2).
$this->nonOperandTokens += [
T_COMMA => T_COMMA,
T_OPEN_PARENTHESIS => T_OPEN_PARENTHESIS,
T_OPEN_SQUARE_BRACKET => T_OPEN_SQUARE_BRACKET,
Expand All @@ -90,17 +95,30 @@ public function register()
T_INLINE_ELSE => T_INLINE_ELSE,
T_CASE => T_CASE,
T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
];

// Casting a negative value; eg. (array) -$a.
T_ARRAY_CAST => T_ARRAY_CAST,
T_BOOL_CAST => T_BOOL_CAST,
T_DOUBLE_CAST => T_DOUBLE_CAST,
T_INT_CAST => T_INT_CAST,
T_OBJECT_CAST => T_OBJECT_CAST,
T_STRING_CAST => T_STRING_CAST,
T_UNSET_CAST => T_UNSET_CAST,
// Casting a negative value; eg. (array) -$a.
$this->nonOperandTokens += [
T_ARRAY_CAST => T_ARRAY_CAST,
T_BOOL_CAST => T_BOOL_CAST,
T_DOUBLE_CAST => T_DOUBLE_CAST,
T_INT_CAST => T_INT_CAST,
T_OBJECT_CAST => T_OBJECT_CAST,
T_STRING_CAST => T_STRING_CAST,
T_UNSET_CAST => T_UNSET_CAST,
];

/*
These are the tokens the sniff is looking for.
*/

$targets = Tokens::$comparisonTokens;
$targets += Tokens::$operators;
$targets += Tokens::$assignmentTokens;
$targets[] = T_INLINE_THEN;
$targets[] = T_INLINE_ELSE;
$targets[] = T_INSTANCEOF;

return $targets;

}//end register()
Expand Down

0 comments on commit ed879f1

Please sign in to comment.