Skip to content

Commit

Permalink
[CodingStyle] Skip RemoveUnusedAliasRector on used in ClassConstFetch (
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik authored Feb 26, 2021
1 parent 38e8520 commit 8604151
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
22 changes: 18 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,21 @@ 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;
}

return (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();
});
}

private function refactorAliasName(string $aliasName, string $lastName, UseUse $useUse): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

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

use App\Bar as BarAlias;

class SkipUsedInClassConstant
{
private $classMap = [
BarAlias::class => 'bar',
];
}

0 comments on commit 8604151

Please sign in to comment.