From 28c49d1a250a03326b1211781fb04e39353b97d5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 14 Nov 2019 16:10:49 +0100 Subject: [PATCH] PSR12/OperatorSpacing: hot fix PR #2640 made it so the `Squiz.WhiteSpace.OperatorSpacing` sniff now sets a property with the `$nonOperandTokens` from the `register()` method. However, the PSR12 sniff for the same extends the Squiz sniff, but overloads the `register()` method to set different targets. This means that the `$nonOperandTokens` array was empty and therefore the `isOperator()` method would nearly always return `true`, which was often incorrect. Fixed by calling the `parent::register()` method before setting the `$targets` for the PSR12 sniff. Includes adding unit tests to the PSR12 sniff related to the exclusions handled via the `isOperator()` method, to prevent breakage like this from being able to be merged in the future. --- .../PSR12/Sniffs/Operators/OperatorSpacingSniff.php | 2 ++ .../PSR12/Tests/Operators/OperatorSpacingUnitTest.inc | 10 ++++++++++ .../Tests/Operators/OperatorSpacingUnitTest.inc.fixed | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php index 90966e6b23..c5d0e6bc80 100644 --- a/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php @@ -24,6 +24,8 @@ class OperatorSpacingSniff extends SquizOperatorSpacingSniff */ public function register() { + parent::register(); + $targets = Tokens::$comparisonTokens; $targets += Tokens::$operators; $targets += Tokens::$assignmentTokens; diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc index 0ccc074a7f..82c68f2585 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc @@ -46,3 +46,13 @@ $foo = $foo?:'bar'; try { } catch (ExceptionType1|ExceptionType2 $e) { } + +if (strpos($tokenContent, 'b"') === 0 && substr($tokenContent, -1) === '"') {} + +$oldConstructorPos = +1; +return -$content; + +function name($a = -1) {} + +$a =& $ref; +$a = [ 'a' => &$something ]; diff --git a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed index ac5c2107a3..abab60279d 100644 --- a/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed @@ -46,3 +46,13 @@ $foo = $foo ?: 'bar'; try { } catch (ExceptionType1 | ExceptionType2 $e) { } + +if (strpos($tokenContent, 'b"') === 0 && substr($tokenContent, -1) === '"') {} + +$oldConstructorPos = +1; +return -$content; + +function name($a = -1) {} + +$a =& $ref; +$a = [ 'a' => &$something ];