Skip to content

Commit

Permalink
[CodingStyle] Fixes #5687 Skip RemoveUnusedAliasRector on used in Cla…
Browse files Browse the repository at this point in the history
…ssConstFetch
  • Loading branch information
samsonasik committed Feb 26, 2021
1 parent 38e8520 commit 617994d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Rector\CodingStyle\Rector\Use_;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use Rector\CodingStyle\Naming\NameRenamer;
Expand Down Expand Up @@ -140,7 +140,7 @@ public function refactor(Node $node): ?Node

/** @var string $aliasName */
$aliasName = $this->getName($use->alias);
if ($this->shouldSkip($lastName, $aliasName)) {
if ($this->shouldSkip($node, $use->name, $lastName, $aliasName)) {
continue;
}

Expand Down Expand Up @@ -188,7 +188,7 @@ private function lowercaseArray(array $values): array
}, $values);
}

private function shouldSkip(string $lastName, string $aliasName): bool
private function shouldSkip(Use_ $use, Name $name, string $lastName, string $aliasName): bool
{
// PHP is case insensitive
$loweredLastName = strtolower($lastName);
Expand All @@ -200,7 +200,27 @@ private function shouldSkip(string $lastName, string $aliasName): bool
}

// part of some @Doc annotation
return in_array($loweredAliasName, $this->resolvedDocPossibleAliases, true);
if (in_array($loweredAliasName, $this->resolvedDocPossibleAliases, true)) {
return true;
}

$isFoundNext = (bool) $this->betterNodeFinder->findFirstNext($use, function (Node $node) use ($name): bool {
if (! $node instanceof ClassConstFetch) {
return false;
}

if (! $node->class instanceof Name) {
return false;
}

return $node->class->toString() === $name->toString();
});

if ($isFoundNext) {
return true;
}

return false;
}

private function refactorAliasName(string $aliasName, string $lastName, UseUse $useUse): void
Expand Down

0 comments on commit 617994d

Please sign in to comment.