Skip to content

Commit

Permalink
Fixed bug squizlabs#3530 : Line indented incorrectly false positive w…
Browse files Browse the repository at this point in the history
…hen using match-expression inside switch case
  • Loading branch information
gsherwood committed Jan 10, 2022
1 parent d57ddc2 commit 5672074
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #3503 : Squiz.Commenting.FunctionComment.ThrowsNoFullStop false positive when one line @throw
- Fixed bug #3526 : PSR12.Properties.ConstantVisibility false positive when using public final const syntax
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #3530 : Line indented incorrectly false positive when using match-expression inside switch case
</notes>
<contents>
<dir name="/">
Expand Down
7 changes: 5 additions & 2 deletions src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -1325,11 +1325,14 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}//end if

// Closing an anon class or function.
// Closing an anon class, closure, or match.
// Each may be returned, which can confuse control structures that
// use return as a closer, like CASE statements.
if (isset($tokens[$i]['scope_condition']) === true
&& $tokens[$i]['scope_closer'] === $i
&& ($tokens[$tokens[$i]['scope_condition']]['code'] === T_CLOSURE
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS)
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_ANON_CLASS
|| $tokens[$tokens[$i]['scope_condition']]['code'] === T_MATCH)
) {
if ($this->debug === true) {
$type = str_replace('_', ' ', strtolower(substr($tokens[$tokens[$i]['scope_condition']]['type'], 2)));
Expand Down
18 changes: 17 additions & 1 deletion src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,22 @@ $a = [
]
];

switch ($foo) {
case 'a':
$foo = match ($foo) {
'bar' => 'custom_1',
default => 'a'
};
return $foo;
case 'b':
return match ($foo) {
'bar' => 'custom_1',
default => 'b'
};
default:
return 'default';
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

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

<?php
if (true) {
if (true) { // first error after line above is here
}
else if (true) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,22 @@ $a = [
]
];

switch ($foo) {
case 'a':
$foo = match ($foo) {
'bar' => 'custom_1',
default => 'a'
};
return $foo;
case 'b':
return match ($foo) {
'bar' => 'custom_1',
default => 'b'
};
default:
return 'default';
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

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

<?php
if (true) {
if (true) { // first error after line above is here
}
else if (true) {
}
Expand Down
18 changes: 17 additions & 1 deletion src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,22 @@ $a = [
]
];

switch ($foo) {
case 'a':
$foo = match ($foo) {
'bar' => 'custom_1',
default => 'a'
};
return $foo;
case 'b':
return match ($foo) {
'bar' => 'custom_1',
default => 'b'
};
default:
return 'default';
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

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

<?php
if (true) {
if (true) { // first error after line above is here
}
else if (true) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,22 @@ $a = [
]
];

switch ($foo) {
case 'a':
$foo = match ($foo) {
'bar' => 'custom_1',
default => 'a'
};
return $foo;
case 'b':
return match ($foo) {
'bar' => 'custom_1',
default => 'b'
};
default:
return 'default';
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

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

<?php
if (true) {
if (true) { // first error after line above is here
}
else if (true) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
1527 => 1,
1529 => 1,
1530 => 1,
1567 => 1,
1568 => 1,
1569 => 1,
1570 => 1,
1583 => 1,
1584 => 1,
1585 => 1,
1586 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 5672074

Please sign in to comment.