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

Issue:
`braces` fixer adds unnecessary empty lines in files where code doesn't start at offset 0
## Bug report

given a file where code doesn't start at offset 0 (which is bad, I know), cs-fixer recently started  (through the `braces` fixer) adding random empty lines to the file in front of each indented statement.

If I manually fix the indent, the problem disappears.

### Code snippet that reproduces the problem

```php
<?php

    function foo()
    {
    }
```

running cs-fixer with just the `braces` fixer enabled will produce the following diff

```diff
   1) /path/to/test.php (braces)
      ---------- begin diff ----------
--- /path/to/test.php
+++ /path/to/test.php
@@ -1,5 +1,6 @@
 <?php

+
     function foo()
     {
     }

      ----------- end diff -----------
```

if I remove the 4 space indent at the beginning of all lines, the empty line will not be added.

Also, I can't quite reproduce it yet, but sometimes after making unrelated changes to the file, *another* empty line will be added.

Pull request description:
bug: BracesFixer - Fix unexpected extra blank line
Fixes #6647.

Patch:
diff --git a/doc/list.rst b/doc/list.rst
index 2a0eb7ba9..1c9dee97d 100644
--- a/doc/list.rst
+++ b/doc/list.rst
@@ -915,6 +915,8 @@ List of Available Rules
      | Default value: ``true``
 
 
+   Part of rule sets `@PhpCsFixer <./ruleSets/PhpCsFixer.rst>`_ `@Symfony <./ruleSets/Symfony.rst>`_
+
    `Source PhpCsFixer\\Fixer\\Import\\GlobalNamespaceImportFixer <./../src/Fixer/Import/GlobalNamespaceImportFixer.php>`_
 -  `group_import <./rules/import/group_import.rst>`_
 
diff --git a/doc/ruleSets/Symfony.rst b/doc/ruleSets/Symfony.rst
index 548a760a3..597ddbe32 100644
--- a/doc/ruleSets/Symfony.rst
+++ b/doc/ruleSets/Symfony.rst
@@ -37,6 +37,9 @@ Rules
 - `general_phpdoc_tag_rename <./../rules/phpdoc/general_phpdoc_tag_rename.rst>`_
   config:
   ``['replacements' => ['inheritDocs' => 'inheritDoc']]``
+- `global_namespace_import <./../rules/import/global_namespace_import.rst>`_
+  config:
+  ``['import_classes' => false, 'import_constants' => false, 'import_functions' => false]``
 - `include <./../rules/control_structure/include.rst>`_
 - `increment_style <./../rules/operator/increment_style.rst>`_
 - `integer_literal_case <./../rules/casing/integer_literal_case.rst>`_
diff --git a/doc/rules/import/global_namespace_import.rst b/doc/rules/import/global_namespace_import.rst
index 6c64c750e..c35da96d3 100644
--- a/doc/rules/import/global_namespace_import.rst
+++ b/doc/rules/import/global_namespace_import.rst
@@ -106,3 +106,18 @@ With configuration: ``['import_classes' => false, 'import_constants' => false, '
    +    $d = new \DateTimeImmutable();
    +    $p = \M_PI;
     }
+
+Rule sets
+---------
+
+The rule is part of the following rule sets:
+
+@PhpCsFixer
+  Using the `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_ rule set will enable the ``global_namespace_import`` rule with the config below:
+
+  ``['import_classes' => false, 'import_constants' => false, 'import_functions' => false]``
+
+@Symfony
+  Using the `@Symfony <./../../ruleSets/Symfony.rst>`_ rule set will enable the ``global_namespace_import`` rule with the config below:
+
+  ``['import_classes' => false, 'import_constants' => false, 'import_functions' => false]``
diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php
index e41fbf020..b46b191b0 100644
--- a/src/ConfigInterface.php
+++ b/src/ConfigInterface.php
@@ -15,7 +15,6 @@
 namespace PhpCsFixer;
 
 use PhpCsFixer\Fixer\FixerInterface;
-use SplFileInfo;
 
 /**
  * @author Fabien Potencier <fabien@symfony.com>
@@ -40,7 +39,7 @@ public function getCustomFixers(): array;
     /**
      * Returns files to scan.
      *
-     * @return iterable<SplFileInfo>
+     * @return iterable<\SplFileInfo>
      */
     public function getFinder(): iterable;
 
@@ -103,7 +102,7 @@ public function registerCustomFixers(iterable $fixers): self;
     public function setCacheFile(string $cacheFile): self;
 
     /**
-     * @param iterable<SplFileInfo> $finder
+     * @param iterable<\SplFileInfo> $finder
      */
     public function setFinder(iterable $finder): self;
 
diff --git a/src/Console/Command/ListFilesCommand.php b/src/Console/Command/ListFilesCommand.php
index 1ce7454c0..b92dae773 100644
--- a/src/Console/Command/ListFilesCommand.php
+++ b/src/Console/Command/ListFilesCommand.php
@@ -18,7 +18,6 @@
 use PhpCsFixer\ConfigInterface;
 use PhpCsFixer\Console\ConfigurationResolver;
 use PhpCsFixer\ToolInfoInterface;
-use SplFileInfo;
 use Symfony\Component\Console\Attribute\AsCommand;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputInterface;
@@ -81,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
 
         $finder = $resolver->getFinder();
 
-        /** @var SplFileInfo $file */
+        /** @var \SplFileInfo $file */
         foreach ($finder as $file) {
             if ($file->isFile()) {
                 $relativePath = str_replace($cwd, '.', $file->getRealPath());
diff --git a/src/Fixer/Whitespace/StatementIndentationFixer.php b/src/Fixer/Whitespace/StatementIndentationFixer.php
index 34ca8f984..94d7239ec 100644
--- a/src/Fixer/Whitespace/StatementIndentationFixer.php
+++ b/src/Fixer/Whitespace/StatementIndentationFixer.php
@@ -491,7 +491,7 @@ private function getLineIndentationWithBracesCompatibility(Tokens $tokens, int $
             && $tokens[$index + 1]->isWhitespace()
             && Preg::match('/\h+$/D', $tokens[$index + 1]->getContent())
         ) {
-            return Preg::replace('/.*?(\h+)$/D', '$1', $tokens[$index + 1]->getContent());
+            return Preg::replace('/.*?(\h+)$/sD', '$1', $tokens[$index + 1]->getContent());
         }
 
         return $regularIndent;
diff --git a/src/RuleSet/Sets/SymfonySet.php b/src/RuleSet/Sets/SymfonySet.php
index 4703c5796..02d4d6fbf 100644
--- a/src/RuleSet/Sets/SymfonySet.php
+++ b/src/RuleSet/Sets/SymfonySet.php
@@ -59,6 +59,11 @@ public function getRules(): array
                     'inheritDocs' => 'inheritDoc',
                 ],
             ],
+            'global_namespace_import' => [
+                'import_classes' => false,
+                'import_constants' => false,
+                'import_functions' => false,
+            ],
             'include' => true,
             'increment_style' => true,
             'integer_literal_case' => true,


Test patch:
diff --git a/tests/Fixer/Basic/BracesFixerTest.php b/tests/Fixer/Basic/BracesFixerTest.php
index 57915fe2d..78488e2ee 100644
--- a/tests/Fixer/Basic/BracesFixerTest.php
+++ b/tests/Fixer/Basic/BracesFixerTest.php
@@ -825,6 +825,13 @@ public function bar() {
     <?php
 }',
             ],
+            [
+                '<?php
+
+    function foo()
+    {
+    }',
+            ],
         ];
     }
 
diff --git a/tests/FixerFactoryTest.php b/tests/FixerFactoryTest.php
index dbd761326..8a21d1ade 100644
--- a/tests/FixerFactoryTest.php
+++ b/tests/FixerFactoryTest.php
@@ -21,7 +21,6 @@
 use PhpCsFixer\RuleSet\RuleSet;
 use PhpCsFixer\RuleSet\RuleSetInterface;
 use PhpCsFixer\WhitespacesFixerConfig;
-use stdClass;
 
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
@@ -375,7 +374,7 @@ public function provideConfigureFixerWithNonArrayCases(): array
     {
         return [
             ['bar'],
-            [new stdClass()],
+            [new \stdClass()],
             [5],
             [5.5],
         ];
diff --git a/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php b/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
index bc0dae9ae..e8270c1d6 100644
--- a/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
+++ b/tests/Tokenizer/Analyzer/AlternativeSyntaxAnalyzerTest.php
@@ -14,7 +14,6 @@
 
 namespace PhpCsFixer\Tests\Tokenizer\Analyzer;
 
-use InvalidArgumentException;
 use PhpCsFixer\Tests\TestCase;
 use PhpCsFixer\Tokenizer\Analyzer\AlternativeSyntaxAnalyzer;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -190,7 +189,7 @@ public function testItThrowsOnInvalidAlternativeSyntaxBlockStartIndex(string $co
 
         $analyzer = new AlternativeSyntaxAnalyzer();
 
-        $this->expectException(InvalidArgumentException::class);
+        $this->expectException(\InvalidArgumentException::class);
         $this->expectExceptionMessage($expectedMessage);
 
         $analyzer->findAlternativeSyntaxBlockEnd($tokens, $startIndex);