Skip to content

Commit

Permalink
bug #6283 ClassReferenceNameCasingFixer - detect imports (SpacePossum)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

ClassReferenceNameCasingFixer - detect imports

closes #6282

Commits
-------

90786d9 ClassReferenceNameCasingFixer - detect imports
  • Loading branch information
SpacePossum committed Feb 12, 2022
2 parents 905baea + 90786d9 commit 21f5a2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Fixer/Casing/ClassReferenceNameCasingFixer.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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]]);
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixer/Casing/ClassReferenceNameCasingFixerTest.php
Expand Up @@ -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);
",
];
}

/**
Expand Down

0 comments on commit 21f5a2a

Please sign in to comment.