Skip to content

Commit

Permalink
PHP 8.0 | Squiz/OperatorBracket: make exception for match expressions
Browse files Browse the repository at this point in the history
Make the same exception for match expressions as was already in place for switch control structures.

While at the same time safeguarding that the sniff will still apply itself correctly for code within a `match` expression, whether in a "case condition" or when in the value.

Includes unit tests.
  • Loading branch information
jrfnl committed Feb 23, 2021
1 parent 8b0137a commit 779de9f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function process(File $phpcsFile, $stackPtr)
break;
}

if ($prevCode === T_STRING || $prevCode === T_SWITCH) {
if ($prevCode === T_STRING || $prevCode === T_SWITCH || $prevCode === T_MATCH) {
// We allow simple operations to not be bracketed.
// For example, ceil($one / $two).
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
Expand Down Expand Up @@ -204,8 +204,8 @@ public function process(File $phpcsFile, $stackPtr)
if (in_array($prevCode, Tokens::$scopeOpeners, true) === true) {
// This operation is inside a control structure like FOREACH
// or IF, but has no bracket of it's own.
// The only control structure allowed to do this is SWITCH.
if ($prevCode !== T_SWITCH) {
// The only control structures allowed to do this are SWITCH and MATCH.
if ($prevCode !== T_SWITCH && $prevCode !== T_MATCH) {
break;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Standards/Squiz/Tests/Formatting/OperatorBracketUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,15 @@ $errorPos = $params[$x]?->getLine() + $commentStart;
$foo = $this->gmail ?? $this->gmail = new Google_Service_Gmail($this->google);

exit -1;

$expr = match ($number - 10) {
-1 => 0,
};

$expr = match ($number % 10) {
1 => 2 * $num,
};

$expr = match (true) {
$num * 100 > 500 => 'expression in key',
};
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,15 @@ $errorPos = ($params[$x]?->getLine() + $commentStart);
$foo = ($this->gmail ?? $this->gmail = new Google_Service_Gmail($this->google));

exit -1;

$expr = match ($number - 10) {
-1 => 0,
};

$expr = match ($number % 10) {
1 => (2 * $num),
};

$expr = match (true) {
($num * 100) > 500 => 'expression in key',
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function getErrorList($testFile='OperatorBracketUnitTest.inc')
169 => 1,
174 => 1,
176 => 1,
185 => 1,
189 => 1,
];
break;
case 'OperatorBracketUnitTest.js':
Expand Down

0 comments on commit 779de9f

Please sign in to comment.