Skip to content

Commit

Permalink
Fixed bug #3530 : Line indented incorrectly false positive when using…
Browse files Browse the repository at this point in the history
… match-expression inside switch case
  • Loading branch information
gsherwood committed Jan 10, 2022
1 parent 137e739 commit c40a607
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
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 @@ -1165,11 +1165,14 @@ public function process(File $phpcsFile, $stackPtr)
}//end if
}//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 @@ -160,10 +160,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 c40a607

Please sign in to comment.