Skip to content

Commit

Permalink
PSR12/OperatorSpacing: hot fix
Browse files Browse the repository at this point in the history
PR squizlabs#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.
  • Loading branch information
jrfnl committed Nov 14, 2019
1 parent ed879f1 commit 28c49d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php
Expand Up @@ -24,6 +24,8 @@ class OperatorSpacingSniff extends SquizOperatorSpacingSniff
*/
public function register()
{
parent::register();

$targets = Tokens::$comparisonTokens;
$targets += Tokens::$operators;
$targets += Tokens::$assignmentTokens;
Expand Down
10 changes: 10 additions & 0 deletions src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.inc
Expand Up @@ -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 ];
Expand Up @@ -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 ];

0 comments on commit 28c49d1

Please sign in to comment.