Skip to content

Commit 5672074

Browse files
committed
Fixed bug #3530 : Line indented incorrectly false positive when using match-expression inside switch case
1 parent d57ddc2 commit 5672074

File tree

7 files changed

+78
-10
lines changed

7 files changed

+78
-10
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4343
- Fixed bug #3503 : Squiz.Commenting.FunctionComment.ThrowsNoFullStop false positive when one line @throw
4444
- Fixed bug #3526 : PSR12.Properties.ConstantVisibility false positive when using public final const syntax
4545
-- Thanks to Juliette Reinders Folmer for the patch
46+
- Fixed bug #3530 : Line indented incorrectly false positive when using match-expression inside switch case
4647
</notes>
4748
<contents>
4849
<dir name="/">

src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,11 +1325,14 @@ public function process(File $phpcsFile, $stackPtr)
13251325
continue;
13261326
}//end if
13271327

1328-
// Closing an anon class or function.
1328+
// Closing an anon class, closure, or match.
1329+
// Each may be returned, which can confuse control structures that
1330+
// use return as a closer, like CASE statements.
13291331
if (isset($tokens[$i]['scope_condition']) === true
13301332
&& $tokens[$i]['scope_closer'] === $i
13311333
&& ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE
1332-
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS)
1334+
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS
1335+
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_MATCH)
13331336
) {
13341337
if ($this->debug === true) {
13351338
$type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2)));

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,22 @@ $a = [
15561556
]
15571557
];
15581558

1559+
switch ($foo) {
1560+
case 'a':
1561+
$foo = match ($foo) {
1562+
'bar' => 'custom_1',
1563+
default => 'a'
1564+
};
1565+
return $foo;
1566+
case 'b':
1567+
return match ($foo) {
1568+
'bar' => 'custom_1',
1569+
default => 'b'
1570+
};
1571+
default:
1572+
return 'default';
1573+
}
1574+
15591575
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15601576
?>
15611577

@@ -1564,7 +1580,7 @@ $a = [
15641580
<?php endif ?>
15651581

15661582
<?php
1567-
if (true) {
1583+
if (true) { // first error after line above is here
15681584
}
15691585
else if (true) {
15701586
}

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,22 @@ $a = [
15561556
]
15571557
];
15581558

1559+
switch ($foo) {
1560+
case 'a':
1561+
$foo = match ($foo) {
1562+
'bar' => 'custom_1',
1563+
default => 'a'
1564+
};
1565+
return $foo;
1566+
case 'b':
1567+
return match ($foo) {
1568+
'bar' => 'custom_1',
1569+
default => 'b'
1570+
};
1571+
default:
1572+
return 'default';
1573+
}
1574+
15591575
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15601576
?>
15611577

@@ -1564,7 +1580,7 @@ $a = [
15641580
<?php endif ?>
15651581

15661582
<?php
1567-
if (true) {
1583+
if (true) { // first error after line above is here
15681584
}
15691585
else if (true) {
15701586
}

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,22 @@ $a = [
15561556
]
15571557
];
15581558

1559+
switch ($foo) {
1560+
case 'a':
1561+
$foo = match ($foo) {
1562+
'bar' => 'custom_1',
1563+
default => 'a'
1564+
};
1565+
return $foo;
1566+
case 'b':
1567+
return match ($foo) {
1568+
'bar' => 'custom_1',
1569+
default => 'b'
1570+
};
1571+
default:
1572+
return 'default';
1573+
}
1574+
15591575
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15601576
?>
15611577

@@ -1564,7 +1580,7 @@ $a = [
15641580
<?php endif ?>
15651581

15661582
<?php
1567-
if (true) {
1583+
if (true) { // first error after line above is here
15681584
}
15691585
else if (true) {
15701586
}

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,22 @@ $a = [
15561556
]
15571557
];
15581558

1559+
switch ($foo) {
1560+
case 'a':
1561+
$foo = match ($foo) {
1562+
'bar' => 'custom_1',
1563+
default => 'a'
1564+
};
1565+
return $foo;
1566+
case 'b':
1567+
return match ($foo) {
1568+
'bar' => 'custom_1',
1569+
default => 'b'
1570+
};
1571+
default:
1572+
return 'default';
1573+
}
1574+
15591575
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15601576
?>
15611577

@@ -1564,7 +1580,7 @@ $a = [
15641580
<?php endif ?>
15651581

15661582
<?php
1567-
if (true) {
1583+
if (true) { // first error after line above is here
15681584
}
15691585
else if (true) {
15701586
}

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
187187
1527 => 1,
188188
1529 => 1,
189189
1530 => 1,
190-
1567 => 1,
191-
1568 => 1,
192-
1569 => 1,
193-
1570 => 1,
190+
1583 => 1,
191+
1584 => 1,
192+
1585 => 1,
193+
1586 => 1,
194194
];
195195

196196
}//end getErrorList()

0 commit comments

Comments
 (0)