Skip to content

Commit

Permalink
Squiz/PSR12/OperatorSpacing: bug fix - unary plus/minus in arrow func…
Browse files Browse the repository at this point in the history
…tion return

A plus/minus directly after the arrow of an arrow function, will always be a unary plus/minus and should therefore be ignored by these sniffs.

Includes unit tests.

Includes two additional simplifications:
* `T_DOUBLE_ARROW` is already part of the `Tokens::$assignmentTokens` array, so doesn't need to be added separately.
* The cast tokens array was incomplete (missing `T_BINARY_CAST`) and can be replaced by the more complete `Tokens::$castTokens` array anyhow.

Fixes 3043
  • Loading branch information
jrfnl committed Sep 29, 2020
1 parent afca5ac commit 1b768c5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ $a = [ 'a' => &$something ];

$fn = fn(array &$one) => 1;
$fn = fn(array & $one) => 1;

$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ $a = [ 'a' => &$something ];

$fn = fn(array &$one) => 1;
$fn = fn(array & $one) => 1;

$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());
22 changes: 7 additions & 15 deletions src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ public function register()

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

// Trying to use a negative value; eg. myFunction($var, -2).
Expand All @@ -90,7 +91,6 @@ public function register()
T_OPEN_PARENTHESIS => T_OPEN_PARENTHESIS,
T_OPEN_SQUARE_BRACKET => T_OPEN_SQUARE_BRACKET,
T_OPEN_SHORT_ARRAY => T_OPEN_SHORT_ARRAY,
T_DOUBLE_ARROW => T_DOUBLE_ARROW,
T_COLON => T_COLON,
T_INLINE_THEN => T_INLINE_THEN,
T_INLINE_ELSE => T_INLINE_ELSE,
Expand All @@ -99,15 +99,7 @@ public function register()
];

// 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,
];
$this->nonOperandTokens += Tokens::$castTokens;

/*
These are the tokens the sniff is looking for.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,5 +470,7 @@ $cl = function ($boo =+1) {};
$fn = fn ($boo =-1) => $boo;
$fn = fn ($boo =+1) => $boo;

$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());

/* Intentional parse error. This has to be the last test in the file. */
$a = 10 +
Original file line number Diff line number Diff line change
Expand Up @@ -464,5 +464,7 @@ $cl = function ($boo =+1) {};
$fn = fn ($boo =-1) => $boo;
$fn = fn ($boo =+1) => $boo;

$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());

/* Intentional parse error. This has to be the last test in the file. */
$a = 10 +

0 comments on commit 1b768c5

Please sign in to comment.