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

Issue:
Parse error when @PhpCsFixer rule is enabled
## Bug report

### Description

When running php-cs-fixer on some code that only has namespace and a class I got  an error about an unexpected `;` but the code has no `;` in it. This only happens with the `@PhpCsFixer` rule enabled, if I remove this rule it runs successfully. Example code at the bottom.

The error:
```
[PhpCsFixer\Linter\LintingException]
Parse error: syntax error, unexpected token ";", expecting "{" on line 3.

Applied fixers: no_unneeded_braces, class_definition, braces_position, statement_indentation, single_line_empty_body, blank_line_after_namespace, single_blank_line_at_eof
```

### Runtime version

PHP CS Fixer version: 3.43.1
PHP runtime version: 8.2.13

### Used command

`php vendor/bin/php-cs-fixer fix -vvv`

### Configuration file

```php
<?php

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__);

$config = new PhpCsFixer\Config();

return $config->setRules([
    '@PhpCsFixer' => true,
])
    ->setFinder($finder);

```

### Code snippet that reproduces the problem

  ```php
<?php

namespace {
    class Foo {}
}
  ```



Pull request:
fix: NoUnneededBracesFixer - improve handling of global namespace
closes #7636

Patch:
diff --git a/src/Fixer/ControlStructure/NoUnneededBracesFixer.php b/src/Fixer/ControlStructure/NoUnneededBracesFixer.php
index 281eca162..3d1e72bc4 100644
--- a/src/Fixer/ControlStructure/NoUnneededBracesFixer.php
+++ b/src/Fixer/ControlStructure/NoUnneededBracesFixer.php
@@ -134,9 +134,17 @@ private function clearIfIsOverCompleteNamespaceBlock(Tokens $tokens): void
 
         $index = $tokens->getNextTokenOfKind(0, [[T_NAMESPACE]]);
 
-        do {
+        $namespaceIsNamed = false;
+
+        $index = $tokens->getNextMeaningfulToken($index);
+        while ($tokens[$index]->isGivenKind([T_STRING, T_NS_SEPARATOR])) {
             $index = $tokens->getNextMeaningfulToken($index);
-        } while ($tokens[$index]->isGivenKind([T_STRING, T_NS_SEPARATOR]));
+            $namespaceIsNamed = true;
+        }
+
+        if (!$namespaceIsNamed) {
+            return;
+        }
 
         if (!$tokens[$index]->equals('{')) {
             return; // `;`


Test patch:
diff --git a/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php b/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php
index e78e1ad77..93d5f8b2b 100644
--- a/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php
+++ b/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php
@@ -182,6 +182,27 @@ function Bar(){}
             } ?>",
             ['namespaces' => true],
         ];
+
+        yield [
+            '<?php
+            namespace A;
+                class X {}
+            ',
+            '<?php
+            namespace A {
+                class X {}
+            }',
+            ['namespaces' => true],
+        ];
+
+        yield [
+            '<?php
+            namespace {
+                class X {}
+            }',
+            null,
+            ['namespaces' => true],
+        ];
     }
 
     /**