Repository: PHP-CS-Fixer/PHP-CS-Fixer (PHP)

Issue:
braces: Incorrect closing brace for switch where last case contains only a comment
## Bug report

PHP-CS-Fixer incorrectly unindents the comment and indents the closing brace of a switch when the last case only contains a comment.

```
PHP CS Fixer 3.9.4 (093466b) Grand Awaiting by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.0.21
Loaded config default.
Using cache file ".php-cs-fixer.cache".
F                                                                                                                                                                                                      1 / 1 (100%)
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error
```
```diff
   1) test.php (braces)
      ---------- begin diff ----------
--- /pwd/test.php
+++ /pwd/test.php
@@ -5,5 +5,5 @@
         echo "Okay", PHP_EOL;
         break;
     case 'baz':
-        // Nothing to do
-}
+    // Nothing to do
+    }

      ----------- end diff -----------


Checked all files in 0.011 seconds, 14.000 MB memory used
```

### Code snippet that reproduces the problem

```php
<?php

switch ($foo) {
    case 'bar':
        echo "Okay", PHP_EOL;
        break;
    case 'baz':
        // Nothing to do
}
```


Discussion:
#6490 might or might not be related.
One more code snippet for tests, where last case ends with comment

```php
<?php

switch ($gender) {
    case 'male':
        echo "Man", PHP_EOL;
        break;
    case 'female':
        echo "Woman", PHP_EOL;
        break;
    default:
        // @codeCoverageIgnoreStart
        throw new LogicException('This should never happen');
        // @codeCoverageIgnoreEnd
}
```



Patch:
diff --git a/src/Fixer/Whitespace/StatementIndentationFixer.php b/src/Fixer/Whitespace/StatementIndentationFixer.php
index 3d7899ac66d..4cc8fc7e67b 100644
--- a/src/Fixer/Whitespace/StatementIndentationFixer.php
+++ b/src/Fixer/Whitespace/StatementIndentationFixer.php
@@ -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))
@@ -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;
@@ -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;
@@ -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;
             }
 
@@ -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];
             }
         }
 


Test patch:
diff --git a/tests/Fixer/Whitespace/StatementIndentationFixerTest.php b/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
index f82bd7eb336..f794935c670 100644
--- a/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
+++ b/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
@@ -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
 }',
         ];
     }