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

Issue:
BracesPositionFixer: Crash ("Cannot access offset of type null on SplFixedArray") when fixing some template code
### Problem description

File: 

```php
<?php
if (true) {
}
$foo="a";
?>
foo: <?= $foo ?>
```

when `BracesPositionFixer` (using default @PER-CS2.0 config) analyzes this file, it will throw `TypeError` with message "Cannot access offset of type null on SplFixedArray".

The Bug is here:

https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/05bec8890f42f80b9525687300dda9bc78aa77e9/src/Fixer/Basic/BracesPositionFixer.php#L254

`$tokens->getNextTokenOfKind()` returns `null`.

I think it's probably safe to `continue` in that case. I'll investigate whether such a fix would still pass the tests.

### Minimal reproducer CLI command

phpjit vendor-bin/php-cs-fixer/vendor/php-cs-fixer/shim/php-cs-fixer fix --sequential --rules "@PER-CS2.0" -vvvv file.php

### Minimal reproducer code snippet

```php
<?php
if (true) {
}
$foo="a";
?>
foo: <?= $foo ?>
```

### Fixer version

3.80.0

### PHP version

8.4.8

### How do you run Fixer?

Composer Shim package (binary only)

### Contribution Checks

- [x] I have verified if this problem was already reported
- [x] I am familiar with ["Feature or bug?"](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/feature-or-bug.rst)

Pull request:
fix: `BracesPositionFixer` - do not throw error when variable is terminated by PHP close tag
The test case is just the slimmed down modified PHP code. The real code (slimmed too) causing errors is:
```php
<?php class A {} ?>
<!-- reproducer.php --!>
<?= $hello ?>

```

The erroring code snippet is meant for documentation. Though PHP-CS-Fixer proclaims it does not fully support mixed PHP code, I believe it should not error.
<img width="610" alt="image" src="https://github.com/user-attachments/assets/9de59cda-80aa-4860-be5c-b8025f476bf8" />

Most likely caused by #8782 

Fixes #8803 

Patch:
diff --git a/src/Fixer/Basic/BracesPositionFixer.php b/src/Fixer/Basic/BracesPositionFixer.php
index 0939b5456..cf5024c54 100644
--- a/src/Fixer/Basic/BracesPositionFixer.php
+++ b/src/Fixer/Basic/BracesPositionFixer.php
@@ -251,7 +251,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
 
                 $positionOption = 'control_structures_opening_brace';
             } elseif ($token->isGivenKind(T_VARIABLE)) {
-                $openBraceIndex = $tokens->getNextTokenOfKind($index, ['{', ';', [CT::T_PROPERTY_HOOK_BRACE_OPEN]]);
+                $openBraceIndex = $tokens->getNextTokenOfKind($index, ['{', ';', [CT::T_PROPERTY_HOOK_BRACE_OPEN], [T_CLOSE_TAG]]);
 
                 if (!$tokens[$openBraceIndex]->isGivenKind(CT::T_PROPERTY_HOOK_BRACE_OPEN)) {
                     continue;


Test patch:
diff --git a/tests/Fixer/Basic/BracesPositionFixerTest.php b/tests/Fixer/Basic/BracesPositionFixerTest.php
index 68f1711fe..28535edaf 100644
--- a/tests/Fixer/Basic/BracesPositionFixerTest.php
+++ b/tests/Fixer/Basic/BracesPositionFixerTest.php
@@ -762,6 +762,30 @@ public function example(){ // example
                 }
                 PHP,
         ];
+
+        yield 'variable terminated by close tag' => [
+            <<<'PHP'
+                <?php
+
+                $pager = new class {
+                public function bar()
+                {
+                return 'baz';
+                }
+                };
+
+                echo $pager->bar()
+                ?>
+                PHP,
+            <<<'PHP'
+                <?php
+
+                $pager = new class { public function bar() { return 'baz'; } };
+
+                echo $pager->bar()
+                ?>
+                PHP,
+        ];
     }
 
     /**