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

[PostRector] Reduce loop on ClassRenamingPostRector #5174

Merged
merged 2 commits into from
Oct 15, 2023
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
13 changes: 11 additions & 2 deletions packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\Renaming\NodeManipulator\ClassRenamer;

final class ClassRenamingPostRector extends AbstractPostRector
Expand All @@ -29,7 +30,8 @@ public function __construct(
private readonly ClassRenamer $classRenamer,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector,
private readonly UseImportsRemover $useImportsRemover,
private readonly CurrentFileProvider $currentFileProvider
private readonly CurrentFileProvider $currentFileProvider,
private readonly UseNodesToAddCollector $useNodesToAddCollector
) {
}

Expand Down Expand Up @@ -81,11 +83,18 @@ public function enterNode(Node $node): ?Node
return null;
}

$useImportTypes = $this->useNodesToAddCollector->getObjectImportsByFilePath($file->getFilePath());

// nothing to remove, as no replacement
if ($useImportTypes === []) {
return null;
}

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

return $result;
Expand Down
16 changes: 2 additions & 14 deletions rules/CodingStyle/Application/UseImportsRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@
use Nette\Utils\Strings;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Use_;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

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

/**
* @param Stmt[] $stmts
* @param string[] $removedUses
* @param AliasedObjectType[]|FullyQualifiedObjectType[] $useImportTypes
* @return Stmt[]
*/
public function removeImportsFromStmts(array $stmts, array $removedUses, string $filePath): array
public function removeImportsFromStmts(array $stmts, array $removedUses, array $useImportTypes): array
{
$useImportTypes = $this->useNodesToAddCollector->getObjectImportsByFilePath($filePath);

foreach ($stmts as $key => $stmt) {
if (! $stmt instanceof Use_) {
continue;
Expand All @@ -49,11 +42,6 @@ public function removeImportsFromStmts(array $stmts, array $removedUses, string
*/
private function removeUseFromUse(array $removedUses, Use_ $use, array $useImportTypes): Use_
{
// nothing to remove, as no replacement
if ($useImportTypes === []) {
return $use;
}

foreach ($use->uses as $usesKey => $useUse) {
$useName = $useUse->name->toString();
if (! in_array($useName, $removedUses, true)) {
Expand Down