Skip to content

Commit

Permalink
bug: Fix indentation for switch ending with empty case (#6538)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque authored Aug 13, 2022
1 parent b81e40f commit 304ee4d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Fixer/Whitespace/StatementIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
0,
$this->extractIndent($this->computeNewLineContent($tokens, 0)),
);

/**
* @var list<array{
* type: 'block'|'block_signature'|'statement',
* skip: bool,
* end_index: int,
* end_index_inclusive: bool,
* initial_indent: string,
* is_indented_block: bool,
* }> $scopes
*/
$scopes = [
[
'type' => 'block',
Expand Down Expand Up @@ -198,6 +209,13 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
'initial_indent' => $initialIndent,
'is_indented_block' => true,
];
++$currentScope;

while ($index >= $scopes[$currentScope]['end_index']) {
array_pop($scopes);

--$currentScope;
}

continue;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixer/Whitespace/StatementIndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,20 @@ public function foo() {
break;
default:
// Nothing to do
}',
];

yield 'switch ending with empty case' => [
'<?php
switch ($foo) {
case 1:
}',
];

yield 'switch ending with empty default' => [
'<?php
switch ($foo) {
default:
}',
];
}
Expand Down

0 comments on commit 304ee4d

Please sign in to comment.