From ed879f1e79dc6c4be4edbe2ff37f8a67eab47daf Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Wed, 13 Nov 2019 08:46:28 +1100 Subject: [PATCH] Changelog + minor cleanup for #2640 --- package.xml | 2 + .../WhiteSpace/OperatorSpacingSniff.php | 58 ++++++++++++------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/package.xml b/package.xml index ab5449a320..3ae013c0e2 100644 --- a/package.xml +++ b/package.xml @@ -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 diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php index 064a3e697b..c69b0b6433 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php @@ -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, @@ -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()