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

Issue:
class_reference_name_casing is confused by class with the same name
## Bug report

Hi @SpacePossum, there is another bug with the `class_reference_name_casing` rule.

In the following automatic PR there is a false positive error from PHP CS Fixer
https://github.com/sonata-project/exporter/pull/572

### Code snippet that reproduces the problem

  ```php
  <?php

use Sonata\Exporter\Writer\XmlWriter;

$services->set('sonata.exporter.writer.xml', XmlWriter::class);
  ```

The fixer is proposing to change to `XMLWriter::class` because of the native class https://www.php.net/manual/fr/book.xmlwriter.php



Discussion:
thanks for reporting, please see https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/6283
Thanks a lot @SpacePossum. Will a 3.6.1 version be planned ? 



Patch:
diff --git a/src/Fixer/Casing/ClassReferenceNameCasingFixer.php b/src/Fixer/Casing/ClassReferenceNameCasingFixer.php
index 11b70ac41a3..565bc46717d 100644
--- a/src/Fixer/Casing/ClassReferenceNameCasingFixer.php
+++ b/src/Fixer/Casing/ClassReferenceNameCasingFixer.php
@@ -20,6 +20,7 @@
 use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
 use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
 use PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer;
+use PhpCsFixer\Tokenizer\Analyzer\NamespaceUsesAnalyzer;
 use PhpCsFixer\Tokenizer\CT;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -53,14 +54,21 @@ public function isCandidate(Tokens $tokens): bool
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
         $namespacesAnalyzer = new NamespacesAnalyzer();
+        $namespaceUsesAnalyzer = new NamespaceUsesAnalyzer();
         $classNames = $this->getClassNames();
 
         foreach ($namespacesAnalyzer->getDeclarations($tokens) as $namespace) {
+            $uses = [];
+
+            foreach ($namespaceUsesAnalyzer->getDeclarationsInNamespace($tokens, $namespace) as $use) {
+                $uses[strtolower($use->getShortName())] = true;
+            }
+
             foreach ($this->getClassReference($tokens, $namespace) as $reference) {
                 $currentContent = $tokens[$reference]->getContent();
                 $lowerCurrentContent = strtolower($currentContent);
 
-                if (isset($classNames[$lowerCurrentContent]) && $currentContent !== $classNames[$lowerCurrentContent]) {
+                if (isset($classNames[$lowerCurrentContent]) && $currentContent !== $classNames[$lowerCurrentContent] && !isset($uses[$lowerCurrentContent])) {
                     $tokens[$reference] = new Token([T_STRING, $classNames[$lowerCurrentContent]]);
                 }
             }


Test patch:
diff --git a/tests/Fixer/Casing/ClassReferenceNameCasingFixerTest.php b/tests/Fixer/Casing/ClassReferenceNameCasingFixerTest.php
index 9a213c691e0..a29312ae3f6 100644
--- a/tests/Fixer/Casing/ClassReferenceNameCasingFixerTest.php
+++ b/tests/Fixer/Casing/ClassReferenceNameCasingFixerTest.php
@@ -202,6 +202,14 @@ public function provideFixCases(): \Generator
                 \A\B\\Bar::bind(fn () => null, null, new class {});
             ',
         ];
+
+        yield [
+            "<?php
+declare(strict_types=1);
+use Sonata\\Exporter\\Writer\\EXCEPTION;
+\$services->set('sonata.exporter.writer.xml', EXCEPTION::class);
+",
+        ];
     }
 
     /**