Skip to content

Commit 4fa1d88

Browse files
jrfnlgsherwood
authored andcommitted
PHP 8.0 | Squiz/OperatorBracket: make exception for match expressions
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.
1 parent 451da42 commit 4fa1d88

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/Standards/Squiz/Sniffs/Formatting/OperatorBracketSniff.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public function process(File $phpcsFile, $stackPtr)
155155
|| $prevCode === T_NAME_FULLY_QUALIFIED
156156
|| $prevCode === T_NAME_RELATIVE
157157
|| $prevCode === T_SWITCH
158+
|| $prevCode === T_MATCH
158159
) {
159160
// We allow simple operations to not be bracketed.
160161
// For example, ceil($one / $two).
@@ -194,8 +195,8 @@ public function process(File $phpcsFile, $stackPtr)
194195
if (in_array($prevCode, Tokens::$scopeOpeners, true) === true) {
195196
// This operation is inside a control structure like FOREACH
196197
// or IF, but has no bracket of it's own.
197-
// The only control structure allowed to do this is SWITCH.
198-
if ($prevCode !== T_SWITCH) {
198+
// The only control structures allowed to do this are SWITCH and MATCH.
199+
if ($prevCode !== T_SWITCH && $prevCode !== T_MATCH) {
199200
break;
200201
}
201202
}

src/Standards/Squiz/Tests/Formatting/OperatorBracketUnitTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,15 @@ $errorPos = $params[$x]?->getLine() + $commentStart;
176176
$foo = $this->gmail ?? $this->gmail = new Google_Service_Gmail($this->google);
177177

178178
exit -1;
179+
180+
$expr = match ($number - 10) {
181+
-1 => 0,
182+
};
183+
184+
$expr = match ($number % 10) {
185+
1 => 2 * $num,
186+
};
187+
188+
$expr = match (true) {
189+
$num * 100 > 500 => 'expression in key',
190+
};

src/Standards/Squiz/Tests/Formatting/OperatorBracketUnitTest.inc.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,15 @@ $errorPos = ($params[$x]?->getLine() + $commentStart);
176176
$foo = ($this->gmail ?? $this->gmail = new Google_Service_Gmail($this->google));
177177

178178
exit -1;
179+
180+
$expr = match ($number - 10) {
181+
-1 => 0,
182+
};
183+
184+
$expr = match ($number % 10) {
185+
1 => (2 * $num),
186+
};
187+
188+
$expr = match (true) {
189+
($num * 100) > 500 => 'expression in key',
190+
};

src/Standards/Squiz/Tests/Formatting/OperatorBracketUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public function getErrorList()
6464
169 => 1,
6565
174 => 1,
6666
176 => 1,
67+
185 => 1,
68+
189 => 1,
6769
];
6870

6971
}//end getErrorList()

0 commit comments

Comments
 (0)