Skip to content

Commit

Permalink
bug: Fix indentation of comment at end of switch case (#6493)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque authored Aug 13, 2022
1 parent 366dea2 commit b81e40f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/Fixer/Whitespace/StatementIndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
foreach ($tokens as $index => $token) {
$currentScope = \count($scopes) - 1;

if ($token->isComment()) {
continue;
}

if (
$token->equalsAny($blockFirstTokens)
|| ($token->equals('(') && !$tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_ARRAY))
Expand Down Expand Up @@ -271,20 +267,20 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$indent = false;

if ($scopes[$currentScope]['is_indented_block']) {
$firstMeaningFulTokenIndex = null;
$firstNonWhitespaceTokenIndex = null;
$nextNewlineIndex = null;
for ($searchIndex = $index + 1, $max = \count($tokens); $searchIndex < $max; ++$searchIndex) {
$searchToken = $tokens[$searchIndex];

if (!$searchToken->isWhitespace() && !$searchToken->isComment()) {
if (null === $firstMeaningFulTokenIndex) {
$firstMeaningFulTokenIndex = $searchIndex;
if (!$searchToken->isWhitespace()) {
if (null === $firstNonWhitespaceTokenIndex) {
$firstNonWhitespaceTokenIndex = $searchIndex;
}

continue;
}

if ($searchToken->isWhitespace() && Preg::match('/\R/', $searchToken->getContent())) {
if (Preg::match('/\R/', $searchToken->getContent())) {
$nextNewlineIndex = $searchIndex;

break;
Expand All @@ -299,7 +295,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

if (
(null !== $firstMeaningFulTokenIndex && $firstMeaningFulTokenIndex < $endIndex)
(null !== $firstNonWhitespaceTokenIndex && $firstNonWhitespaceTokenIndex < $endIndex)
|| (null !== $nextNewlineIndex && $nextNewlineIndex < $endIndex)
) {
$indent = true;
Expand Down Expand Up @@ -372,7 +368,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
--$currentScope;
}

if ($token->equalsAny([';', ',', '}', [T_OPEN_TAG], [T_CLOSE_TAG], [CT::T_ATTRIBUTE_CLOSE]])) {
if ($token->isComment() || $token->equalsAny([';', ',', '}', [T_OPEN_TAG], [T_CLOSE_TAG], [CT::T_ATTRIBUTE_CLOSE]])) {
continue;
}

Expand Down Expand Up @@ -460,7 +456,7 @@ private function findCaseBlockEnd(Tokens $tokens, int $index): array
}

if ($tokens[$index]->equalsAny(['}', [T_ENDSWITCH]])) {
return [$tokens->getPrevMeaningfulToken($index), false];
return [$tokens->getPrevNonWhitespace($index), false];
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Fixer/Whitespace/StatementIndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,26 @@ class Foo
Baz {
public function foo() {
}
}',
];

yield 'comment at end of switch case' => [
'<?php
switch ($foo) {
case 1:
// Nothing to do
}',
];

yield 'comment at end of switch default' => [
'<?php
switch ($foo) {
case 1:
break;
case 2:
break;
default:
// Nothing to do
}',
];
}
Expand Down

0 comments on commit b81e40f

Please sign in to comment.