Skip to content

Commit

Permalink
[AutoImport] Skip auto import on no namespace used class on auto impo…
Browse files Browse the repository at this point in the history
…rt enabled (#5247)

* [AutoImport] Skip auto import on no namespace used class on auto import enabled

* Fixed 🎉

* Fixed 🎉
  • Loading branch information
samsonasik committed Nov 14, 2023
1 parent e72fb25 commit f0b8af8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

/**
Expand All @@ -24,8 +25,7 @@
final class FullyQualifiedNameClassNameImportSkipVoter implements ClassNameImportSkipVoterInterface
{
public function __construct(
private readonly ShortNameResolver $shortNameResolver,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector
private readonly ShortNameResolver $shortNameResolver
) {
}

Expand All @@ -34,9 +34,9 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
// "new X" or "X::static()"
/** @var array<string, string> $shortNamesToFullyQualifiedNames */
$shortNamesToFullyQualifiedNames = $this->shortNameResolver->resolveFromFile($file);
$removedUses = $this->renamedClassesDataCollector->getOldClasses();
$fullyQualifiedObjectTypeShortName = $fullyQualifiedObjectType->getShortName();
$className = $fullyQualifiedObjectType->getClassName();
$justRenamed = $node instanceof FullyQualified && ! $node->hasAttribute(AttributeKey::ORIGINAL_NAME);

foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
if ($fullyQualifiedObjectTypeShortName !== $shortName) {
Expand All @@ -54,11 +54,7 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
return false;
}

if (in_array($fullyQualifiedName, $removedUses, true)) {
return false;
}

return str_contains($fullyQualifiedName, '\\');
return ! $justRenamed;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

function skipNoNamespaceUsedClass(
// fqcn
\PHPUnit\Framework\MockObject\Stub\Exception $e
)
{
// from root
$obj = new Exception();
}

0 comments on commit f0b8af8

Please sign in to comment.