Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ private function processFqnNameImport(
$newNode = new IdentifierTypeNode($fullyQualifiedObjectType->getShortName());

// should skip because its already used
if ($this->useNodesToAddCollector->isShortImported($file, $fullyQualifiedObjectType)) {
if (! $this->useNodesToAddCollector->isImportShortable($file, $fullyQualifiedObjectType)) {
return null;
}
if ($this->useNodesToAddCollector->isShortImported($file, $fullyQualifiedObjectType)
&& ! $this->useNodesToAddCollector->isImportShortable($file, $fullyQualifiedObjectType)) {
return null;
}

if ($this->shouldImport($newNode, $identifierTypeNode, $fullyQualifiedObjectType)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private function processNodeName(Name $name, File $file): ?Node
return $nameInUse;
}

return $this->nameImporter->importName($name, $file, $currentUses);
return $this->nameImporter->importName($name, $file);
}

return null;
Expand Down
37 changes: 3 additions & 34 deletions rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace Rector\CodingStyle\Node;

use PhpParser\Node\Name;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Use_;
use Rector\CodingStyle\ClassNameImport\AliasUsesResolver;
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
Expand All @@ -19,23 +16,14 @@

final class NameImporter
{
/**
* @var string[]
*/
private array $aliasedUses = [];

public function __construct(
private readonly AliasUsesResolver $aliasUsesResolver,
private readonly ClassNameImportSkipper $classNameImportSkipper,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly UseNodesToAddCollector $useNodesToAddCollector
) {
}

/**
* @param Use_[]|GroupUse[] $uses
*/
public function importName(Name $name, File $file, array $uses): ?Name
public function importName(Name $name, File $file): ?Name
{
if ($this->shouldSkipName($name)) {
return null;
Expand All @@ -46,13 +34,7 @@ public function importName(Name $name, File $file, array $uses): ?Name
return null;
}

$className = $staticType->getClassName();
// class has \, no need to search in aliases, mark aliasedUses as empty
$this->aliasedUses = str_contains($className, '\\')
? []
: $this->aliasUsesResolver->resolveFromStmts($uses);

return $this->importNameAndCollectNewUseStatement($file, $name, $staticType, $className);
return $this->importNameAndCollectNewUseStatement($file, $name, $staticType);
}

private function shouldSkipName(Name $name): bool
Expand Down Expand Up @@ -91,8 +73,7 @@ private function shouldSkipName(Name $name): bool
private function importNameAndCollectNewUseStatement(
File $file,
Name $name,
FullyQualifiedObjectType $fullyQualifiedObjectType,
string $className
FullyQualifiedObjectType $fullyQualifiedObjectType
): ?Name {
// the same end is already imported → skip
if ($this->classNameImportSkipper->shouldSkipNameForFullyQualifiedObjectType(
Expand All @@ -112,18 +93,6 @@ private function importNameAndCollectNewUseStatement(
}

$this->addUseImport($file, $name, $fullyQualifiedObjectType);

if ($this->aliasedUses === []) {
return $fullyQualifiedObjectType->getShortNameNode();
}

// possibly aliased
foreach ($this->aliasedUses as $aliasedUse) {
if ($className === $aliasedUse) {
return null;
}
}

return $fullyQualifiedObjectType->getShortNameNode();
}

Expand Down