Skip to content

Commit 779de9f

Browse files
committed
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 8b0137a commit 779de9f

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function process(File $phpcsFile, $stackPtr)
165165
break;
166166
}
167167

168-
if ($prevCode === T_STRING || $prevCode === T_SWITCH) {
168+
if ($prevCode === T_STRING || $prevCode === T_SWITCH || $prevCode === T_MATCH) {
169169
// We allow simple operations to not be bracketed.
170170
// For example, ceil($one / $two).
171171
for ($prev = ($stackPtr - 1); $prev > $bracket; $prev--) {
@@ -204,8 +204,8 @@ public function process(File $phpcsFile, $stackPtr)
204204
if (in_array($prevCode, Tokens::$scopeOpeners, true) === true) {
205205
// This operation is inside a control structure like FOREACH
206206
// or IF, but has no bracket of it's own.
207-
// The only control structure allowed to do this is SWITCH.
208-
if ($prevCode !== T_SWITCH) {
207+
// The only control structures allowed to do this are SWITCH and MATCH.
208+
if ($prevCode !== T_SWITCH && $prevCode !== T_MATCH) {
209209
break;
210210
}
211211
}

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
@@ -68,6 +68,8 @@ public function getErrorList($testFile='OperatorBracketUnitTest.inc')
6868
169 => 1,
6969
174 => 1,
7070
176 => 1,
71+
185 => 1,
72+
189 => 1,
7173
];
7274
break;
7375
case 'OperatorBracketUnitTest.js':

0 commit comments

Comments
 (0)