Skip to content

Commit

Permalink
Remove use imports directly (#4008)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 29, 2023
1 parent 7891fda commit f429e28
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
3 changes: 3 additions & 0 deletions packages/NodeRemoval/NodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function __construct(
) {
}

/**
* @deprecated Return NodeTraverser::* to remove node directly instead
*/
public function removeNode(Node $node): void
{
// this make sure to keep just added nodes, e.g. added class constant, that doesn't have analysis of full code in this run
Expand Down
2 changes: 1 addition & 1 deletion packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function enterNode(Node $node): ?Node
}

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

return $result;
}
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,4 @@ parameters:
- '#Parameter \$nodesToAddCollector of (.*?) has typehint with deprecated class Rector\\PostRector\\Collector\\NodesToAddCollector#'

- '#Call to deprecated method addNodeToRemove\(\) of class Rector\\PostRector\\Collector\\NodesToRemoveCollector\:#'
- '#Call to deprecated method removeNode\(\) of class Rector\\NodeRemoval\\NodeRemover#'
26 changes: 13 additions & 13 deletions rules/CodingStyle/Application/UseImportsRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@

use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Use_;
use Rector\NodeRemoval\NodeRemover;

final class UseImportsRemover
{
public function __construct(
private readonly NodeRemover $nodeRemover
) {
}

/**
* @param Stmt[] $stmts
* @param string[] $removedUses
* @return Stmt[]
*/
public function removeImportsFromStmts(array $stmts, array $removedUses): void
public function removeImportsFromStmts(array $stmts, array $removedUses): array
{
foreach ($stmts as $stmt) {
foreach ($stmts as $key => $stmt) {
if (! $stmt instanceof Use_) {
continue;
}

$this->removeUseFromUse($removedUses, $stmt);
$stmt = $this->removeUseFromUse($removedUses, $stmt);

// remove empty uses
if ($stmt->uses === []) {
unset($stmts[$key]);
}
}

return $stmts;
}

/**
* @param string[] $removedUses
*/
private function removeUseFromUse(array $removedUses, Use_ $use): void
private function removeUseFromUse(array $removedUses, Use_ $use): Use_
{
foreach ($use->uses as $usesKey => $useUse) {
foreach ($removedUses as $removedUse) {
Expand All @@ -43,8 +45,6 @@ private function removeUseFromUse(array $removedUses, Use_ $use): void
}
}

if ($use->uses === []) {
$this->nodeRemover->removeNode($use);
}
return $use;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\InlineTypedFromConstructorDefaultValue\Fixture;

final class Fixture
{
private $url;

public function __construct()
{
$this->url = 'https://website.tld';
}
}

?>

0 comments on commit f429e28

Please sign in to comment.