Skip to content

Commit

Permalink
Merge pull request #335 from PHPCSStandards/operators/isunaryplusminu…
Browse files Browse the repository at this point in the history
…s-allow-for-match-arrows

Operators::isUnaryPlusMinus(): allow for match control structures
  • Loading branch information
jrfnl authored Aug 27, 2022
2 parents b65fbd4 + 45e2364 commit 262736b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions PHPCSUtils/Utils/Operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ public static function isUnaryPlusMinus(File $phpcsFile, $stackPtr)
|| isset(Tokens::$castTokens[$tokens[$prev]['code']]) === true
|| isset(self::$extraUnaryIndicators[$tokens[$prev]['code']]) === true
|| $tokens[$prev]['type'] === 'T_FN_ARROW'
|| $tokens[$prev]['type'] === 'T_MATCH_ARROW'
) {
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/Utils/Operators/IsUnaryPlusMinusTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ switch ($a) {
/* testUnaryMinusArrowFunction */
$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());

$matcher = match ($a) {
/* testUnaryPlusMatchArrow */
'a' => +1,
/* testUnaryMinusMatchArrow */
'b', 'c', 'd' => -2,
/* testUnaryMinusMatchDefault */
default => -3,
};

// Testing `$a = -+-+10`;
$a =
/* testSequenceNonUnary1 */
Expand Down
12 changes: 12 additions & 0 deletions Tests/Utils/Operators/IsUnaryPlusMinusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ public function dataIsUnaryPlusMinus()
'testMarker' => '/* testUnaryMinusArrowFunction */',
'expected' => true,
],
'unary-plus-match-arrow' => [
'testMarker' => '/* testUnaryPlusMatchArrow */',
'expected' => true,
],
'unary-minus-match-arrow' => [
'testMarker' => '/* testUnaryMinusMatchArrow */',
'expected' => true,
],
'unary-minus-match-default' => [
'testMarker' => '/* testUnaryMinusMatchDefault */',
'expected' => true,
],
'operator-sequence-non-unary-1' => [
'testMarker' => '/* testSequenceNonUnary1 */',
'expected' => false,
Expand Down

0 comments on commit 262736b

Please sign in to comment.