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

Issue:
Nested alternative syntax if-else conditions
## Bug report

```
PHP CS Fixer 3.9.4-DEV Grand Awaiting by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.1.8
```

### Code snippet that reproduces the problem

```php
<?php if (1): ?>
    <div></div>
<?php else: ?>
    <?php if (2): ?>
        <div></div>
    <?php else: ?>
        <div></div>
    <?php endif; ?>
<?php endif; ?>
```

```
[TypeError]                                                                                                                                                                                                               
PhpCsFixer\Tokenizer\Tokens::findBlockEnd(): Argument #2 ($searchIndex) must be of type int, null given, called in /app/vendor/friendsofphp/php-cs-fixer/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php on line 93

PhpCsFixer\Tokenizer\Tokens->findBlockEnd()
  in /app/vendor/friendsofphp/php-cs-fixer/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php at line 93
PhpCsFixer\Tokenizer\Analyzer\AlternativeSyntaxAnalyzer->findAlternativeSyntaxBlockEnd()
  in /app/vendor/friendsofphp/php-cs-fixer/src/Fixer/Whitespace/StatementIndentationFixer.php at line 160
PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer->applyFix()
  in /app/vendor/friendsofphp/php-cs-fixer/src/AbstractFixer.php at line 75
PhpCsFixer\AbstractFixer->fix()
  in /app/vendor/friendsofphp/php-cs-fixer/src/Fixer/Basic/BracesFixer.php at line 195
PhpCsFixer\Fixer\Basic\BracesFixer->applyFix()
  in /app/vendor/friendsofphp/php-cs-fixer/src/AbstractFixer.php at line 75
PhpCsFixer\AbstractFixer->fix()
  in /app/vendor/friendsofphp/php-cs-fixer/src/Runner/Runner.php at line 164
PhpCsFixer\Runner\Runner->fixFile()
  in /app/vendor/friendsofphp/php-cs-fixer/src/Runner/Runner.php at line 108
PhpCsFixer\Runner\Runner->fix()
  in /app/vendor/friendsofphp/php-cs-fixer/src/Console/Command/FixCommand.php at line 307
PhpCsFixer\Console\Command\FixCommand->execute()
  in /app/vendor/symfony/console/Command/Command.php at line 298
[ ... ]
```


Discussion:
Thanks for reporting @ElisDN !
This issue should now be fixed and the fix will be shipped within the next version(s) :)



Patch:
diff --git a/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php b/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php
index ac4ff73d1c1..6f63076598c 100644
--- a/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php
+++ b/src/Tokenizer/Analyzer/AlternativeSyntaxAnalyzer.php
@@ -87,21 +87,9 @@ public function findAlternativeSyntaxBlockEnd(Tokens $tokens, int $index): int
                 return $index;
             }
 
-            $blockOpeningIndex = $tokens->getNextTokenOfKind(
-                $tokens->findBlockEnd(
-                    Tokens::BLOCK_TYPE_PARENTHESIS_BRACE,
-                    $tokens->getNextTokenOfKind($index + 1, ['(']),
-                ),
-                [':', '{'],
-            );
-
-            if (!$tokens[$blockOpeningIndex]->equals(':')) {
-                $index = $blockOpeningIndex;
-
-                continue;
+            if ($this->isStartOfAlternativeSyntaxBlock($tokens, $index)) {
+                $index = $this->findAlternativeSyntaxBlockEnd($tokens, $index);
             }
-
-            $index = $this->findAlternativeSyntaxBlockEnd($tokens, $index);
         }
     }
 


Test patch:
diff --git a/tests/Fixer/Whitespace/StatementIndentationFixerTest.php b/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
index 17e643cd972..fcd1549e4f2 100644
--- a/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
+++ b/tests/Fixer/Whitespace/StatementIndentationFixerTest.php
@@ -691,6 +691,19 @@ public function foo(
         ;
 }',
         ];
+
+        yield 'alternative syntax' => [
+            '<?php if (1): ?>
+    <div></div>
+<?php else: ?>
+    <?php if (2): ?>
+        <div></div>
+    <?php else: ?>
+        <div></div>
+    <?php endif; ?>
+<?php endif; ?>
+',
+        ];
     }
 
     /**
diff --git a/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php b/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
index 4ccce5853b1..bc0dae9aecb 100644
--- a/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
+++ b/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
@@ -159,6 +159,26 @@ public function provideFindBlockEndCases(): iterable
         yield [$nested, 15, 83];
 
         yield [$nested, 41, 80];
+
+        $nestedWithHtml = <<<'PHP'
+            <?php if (1): ?>
+                <div></div>
+            <?php else: ?>
+                <?php if (2): ?>
+                    <div></div>
+                <?php else: ?>
+                    <div></div>
+                <?php endif; ?>
+            <?php endif; ?>
+            PHP;
+
+        yield [$nestedWithHtml, 1, 11];
+
+        yield [$nestedWithHtml, 11, 38];
+
+        yield [$nestedWithHtml, 17, 27];
+
+        yield [$nestedWithHtml, 27, 33];
     }
 
     /**