Skip to content

Commit

Permalink
[CodingStyle] Clean up check last name on UseImportsRemover (#5173)
Browse files Browse the repository at this point in the history
* [CodingStyle] Clean up check last name on UseImportsRemover

* [CodingStyle] Clean up check last name on UseImportsRemover

* fix phpstan
  • Loading branch information
samsonasik committed Oct 15, 2023
1 parent 29370c7 commit c5d3a0e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/PostRector/Rector/ClassRenamingPostRector.php
Expand Up @@ -82,7 +82,11 @@ public function enterNode(Node $node): ?Node
}

$removedUses = $this->renamedClassesDataCollector->getOldClasses();
$this->rootNode->stmts = $this->useImportsRemover->removeImportsFromStmts($this->rootNode->stmts, $removedUses, $file->getFilePath());
$this->rootNode->stmts = $this->useImportsRemover->removeImportsFromStmts(
$this->rootNode->stmts,
$removedUses,
$file->getFilePath()
);

return $result;
}
Expand Down
Expand Up @@ -21,7 +21,6 @@
'Queue' => 'Illuminate\Support\Facades\Queue',

/**
*
* For testing skip remove use statement part of rename during auto import
* the rename annotation is allowed only on specific symfony assert, doctrine, and serializer
*
Expand Down
12 changes: 6 additions & 6 deletions rules/CodingStyle/Application/UseImportsRemover.php
Expand Up @@ -4,6 +4,7 @@

namespace Rector\CodingStyle\Application;

use Nette\Utils\Strings;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Use_;
use Rector\PostRector\Collector\UseNodesToAddCollector;
Expand All @@ -12,9 +13,9 @@

final class UseImportsRemover
{
public function __construct(private readonly UseNodesToAddCollector $useNodesToAddCollector)
{

public function __construct(
private readonly UseNodesToAddCollector $useNodesToAddCollector
) {
}

/**
Expand Down Expand Up @@ -59,18 +60,17 @@ private function removeUseFromUse(array $removedUses, Use_ $use, array $useImpor
continue;
}

$lastUseName = Strings::after($useName, '\\', -1);
foreach ($useImportTypes as $useImportType) {
$className = $useImportType instanceof AliasedObjectType
? $useImportType->getFullyQualifiedName()
: $useImportType->getClassName();

if ($className === $useName) {
if ($className === $useName || Strings::after($className, '\\', -1) === $lastUseName) {
unset($use->uses[$usesKey]);
continue 2;
}
}

unset($use->uses[$usesKey]);
}

return $use;
Expand Down
Expand Up @@ -4,9 +4,9 @@

namespace Rector\DeadCode\Rector\If_;

use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
Expand Down

0 comments on commit c5d3a0e

Please sign in to comment.