Skip to content

Commit 28c49d1

Browse files
committed
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.
1 parent ed879f1 commit 28c49d1

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class OperatorSpacingSniff extends SquizOperatorSpacingSniff
2424
*/
2525
public function register()
2626
{
27+
parent::register();
28+
2729
$targets = Tokens::$comparisonTokens;
2830
$targets += Tokens::$operators;
2931
$targets += Tokens::$assignmentTokens;

src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ $foo = $foo?:'bar';
4646
try {
4747
} catch (ExceptionType1|ExceptionType2 $e) {
4848
}
49+
50+
if (strpos($tokenContent, 'b"') === 0 && substr($tokenContent, -1) === '"') {}
51+
52+
$oldConstructorPos = +1;
53+
return -$content;
54+
55+
function name($a = -1) {}
56+
57+
$a =& $ref;
58+
$a = [ 'a' => &$something ];

src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ $foo = $foo ?: 'bar';
4646
try {
4747
} catch (ExceptionType1 | ExceptionType2 $e) {
4848
}
49+
50+
if (strpos($tokenContent, 'b"') === 0 && substr($tokenContent, -1) === '"') {}
51+
52+
$oldConstructorPos = +1;
53+
return -$content;
54+
55+
function name($a = -1) {}
56+
57+
$a =& $ref;
58+
$a = [ 'a' => &$something ];

0 commit comments

Comments
 (0)