Skip to content

Commit

Permalink
[CodingStyle] Skip RemoveUnusedAliasRector when same class in use sta…
Browse files Browse the repository at this point in the history
…tement exists, but not used (#732)

* [CodingStyle] Skip RemoveUnusedAliasRector used by return method call

* update fixture

* update fixture

* Fixed 🎉

* clean up

* [ci-review] Rector Rectify

* no scope handling

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 22, 2021
1 parent 138aa5e commit 1cc465b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Fixture;

use Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Source\StandaloneClass;
use Rector\Tests\CodingStyle\Rector\Use_\RemoveUnusedAliasRector\Source\DifferentNamespace\StandaloneClass as StandaloneClassDifferentNamespace;

class SkipSameClassInUseStatementNotUsed
{
public function run()
{
return new StandaloneClassDifferentNamespace();
}
}
27 changes: 18 additions & 9 deletions rules/CodingStyle/Rector/Use_/RemoveUnusedAliasRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
use Rector\CodingStyle\Naming\NameRenamer;
use Rector\CodingStyle\Node\DocAliasResolver;
use Rector\CodingStyle\Node\UseManipulator;
Expand Down Expand Up @@ -46,7 +47,8 @@ public function __construct(
private DocAliasResolver $docAliasResolver,
private UseManipulator $useManipulator,
private UseNameAliasToNameResolver $useNameAliasToNameResolver,
private NameRenamer $nameRenamer
private NameRenamer $nameRenamer,
private ClassNameImportSkipper $classNameImportSkipper
) {
}

Expand Down Expand Up @@ -129,7 +131,7 @@ public function refactor(Node $node): ?Node
continue;
}

$this->refactorAliasName($aliasName, $lastName, $use);
$this->refactorAliasName($node, $aliasName, $lastName, $use);
}

return $node;
Expand Down Expand Up @@ -206,20 +208,27 @@ private function shouldSkip(Use_ $use, Name $name, string $lastName, string $ali
});
}

private function refactorAliasName(string $aliasName, string $lastName, UseUse $useUse): void
private function refactorAliasName(Use_ $use, string $aliasName, string $lastName, UseUse $useUse): void
{
// only alias name is used → use last name directly
$lowerAliasName = strtolower($aliasName);
if (! isset($this->resolvedNodeNames[$lowerAliasName])) {
$parentUse = $use->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentUse instanceof Node) {
return;
}

// keep to differentiate 2 aliases classes
$lowerLastName = strtolower($lastName);
if (count($this->useNamesAliasToName[$lowerLastName] ?? []) > 1) {
/** @var Use_[] $uses */
$uses = $this->betterNodeFinder->find($parentUse, function (Node $node) use ($use): bool {
if ($node === $use) {
return false;
}

return $node instanceof Use_;
});

if ($this->classNameImportSkipper->isShortNameInUseStatement(new Name($lastName), $uses)) {
return;
}

$lowerAliasName = strtolower($aliasName);
$this->nameRenamer->renameNameNode($this->resolvedNodeNames[$lowerAliasName], $lastName);
$useUse->alias = null;
}
Expand Down

0 comments on commit 1cc465b

Please sign in to comment.