Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodingStyle] Skip RemoveUnusedAliasRector on used in ClassConstFetch #5691

Merged
merged 3 commits into from
Feb 26, 2021
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
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',
];
}