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

Issue:
Comment identation inside `if`
Continuation of #3638

### Config:

```php
<?php

# .php-cs-fixer.php

return (new PhpCsFixer\Config())->setRules([
    '@PSR12' => true,
])
    ->setFinder([new SplFileInfo(__DIR__ . '/test.php')])
;
```

### Command & output:

```
php vendor/bin/php-cs-fixer fix -vvv
PHP CS Fixer 3.16.0 BoY42 by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.1.5
Loaded config default from "/var/www/.php-cs-fixer.php".
Using cache file ".php-cs-fixer.cache".
F                                                                                                                                                                                                                                                                                                     1 / 1 (100%)
Legend: .-no changes, F-fixed, S-skipped (cached or empty file), I-invalid file syntax (file ignored), E-error
   1) test.php (statement_indentation)

Fixed 1 of 1 files in 0.004 seconds, 20.000 MB memory used
```

### Input file:

```php
<?php

#test.php

if (true) {
    echo 'hello';
    //hello comment
} else {
    echo 'else hello';
}
```

### Result:

```php
<?php

#test.php

if (true) {
    echo 'hello';
//hello comment
} else {
    echo 'else hello';
}
```

### Expectation:

To keep the comment indented as it was in the input file. 

### Why:

1. The fixed version is not compatible with PHPStorm code formatter - and there's no existing PHPStorm configuration constellation that would **not** conflict with the above php-cs-fixer output.
2. The comment just looks better and more organic in the "input file".


Discussion:
Please see https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/discussions/6906 for what I suspect is another use case of the same or proximate bug. 



Patch:
diff --git a/src/Fixer/Whitespace/StatementIndentationFixer.php b/src/Fixer/Whitespace/StatementIndentationFixer.php
index dc7a21b214d..0d11db45425 100644
--- a/src/Fixer/Whitespace/StatementIndentationFixer.php
+++ b/src/Fixer/Whitespace/StatementIndentationFixer.php
@@ -304,19 +304,17 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
                             }
                         }
 
-                        if (!$this->isCommentForControlSructureContinuation($tokens, $index + 1)) {
-                            $endIndex = $scopes[$currentScope]['end_index'];
+                        $endIndex = $scopes[$currentScope]['end_index'];
 
-                            if (!$scopes[$currentScope]['end_index_inclusive']) {
-                                ++$endIndex;
-                            }
+                        if (!$scopes[$currentScope]['end_index_inclusive']) {
+                            ++$endIndex;
+                        }
 
-                            if (
-                                (null !== $firstNonWhitespaceTokenIndex && $firstNonWhitespaceTokenIndex < $endIndex)
-                                || (null !== $nextNewlineIndex && $nextNewlineIndex < $endIndex)
-                            ) {
-                                $indent = true;
-                            }
+                        if (
+                            (null !== $firstNonWhitespaceTokenIndex && $firstNonWhitespaceTokenIndex < $endIndex)
+                            || (null !== $nextNewlineIndex && $nextNewlineIndex < $endIndex)
+                        ) {
+                            $indent = true;
                         }
                     }
 
@@ -496,36 +494,6 @@ private function getLineIndentationWithBracesCompatibility(Tokens $tokens, int $
         return $regularIndent;
     }
 
-    private function isCommentForControlSructureContinuation(Tokens $tokens, int $index): bool
-    {
-        if (!isset($tokens[$index], $tokens[$index + 1])) {
-            return false;
-        }
-
-        if (!$tokens[$index]->isComment() || 1 !== Preg::match('~^(//|#)~', $tokens[$index]->getContent())) {
-            return false;
-        }
-
-        if (!$tokens[$index + 1]->isWhitespace() || 1 !== Preg::match('/\R/', $tokens[$index + 1]->getContent())) {
-            return false;
-        }
-
-        $prevIndex = $tokens->getPrevMeaningfulToken($index);
-        if (null !== $prevIndex && $tokens[$prevIndex]->equals('{')) {
-            return false;
-        }
-
-        $index = $tokens->getNextMeaningfulToken($index + 1);
-
-        if (null === $index || !$tokens[$index]->equals('}')) {
-            return false;
-        }
-
-        $index = $tokens->getNextMeaningfulToken($index);
-
-        return null !== $index && $tokens[$index]->isGivenKind([T_ELSE, T_ELSEIF]);
-    }
-
     /**
      * Returns whether the token at given index is a comment whose indentation
      * can be fixed.


Test patch:
diff --git a/tests/Fixer/Basic/BracesFixerTest.php b/tests/Fixer/Basic/BracesFixerTest.php
index 171bf29b0ab..10473acdb3e 100644
--- a/tests/Fixer/Basic/BracesFixerTest.php
+++ b/tests/Fixer/Basic/BracesFixerTest.php
@@ -1065,7 +1065,7 @@ public static function provideFixMissingBracesAndIndentCases(): iterable
         $a = "a";
     } elseif (3) {
         $b = "b";
-    // comment
+        // comment
     } else {
         $c = "c";
     }
@@ -1772,7 +1772,7 @@ public function __construct(
         $a = "a";
     } elseif (3) {
         $b = "b";
-    // comment
+        // comment
     } else {
         $c = "c";
     }
@@ -1788,10 +1788,10 @@ public function __construct(
         $a = "a";
     } elseif (3) {
         $b = "b";
-    // comment line 1
-    // comment line 2
-    // comment line 3
-    // comment line 4
+        // comment line 1
+        // comment line 2
+        // comment line 3
+        // comment line 4
     } else {
         $c = "c";
     }
@@ -3028,10 +3028,10 @@ public static function provideFixCommentBeforeBraceCases(): iterable
     // 2.5+ API
     if (isNewApi()) {
         echo "new API";
-    // 2.4- API
+        // 2.4- API
     } elseif (isOldApi()) {
         echo "old API";
-    // 2.4- API
+        // 2.4- API
     } else {
         echo "unknown API";
         // sth
diff --git a/tests/Fixer/Whitespace/StatementIndentationFixerTest.php b/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
index 80f1c3522fa..f279c5ebd89 100644
--- a/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
+++ b/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
@@ -800,6 +800,20 @@ public function foo() {
     3
 );',
         ];
+
+        yield 'comments at the end of if/elseif/else blocks' => [
+            '<?php
+if ($foo) {
+    echo "foo";
+    // foo
+} elseif ($bar) {
+    echo "bar";
+    // bar
+} else {
+    echo "baz";
+    // baz
+}',
+        ];
     }
 
     /**