|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PostRector\Rector; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PhpParser\Node\Stmt\Namespace_; |
| 10 | +use PhpParser\Node\Stmt\Use_; |
| 11 | +use PhpParser\NodeTraverser; |
| 12 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 13 | +use Rector\Core\Configuration\RectorConfigProvider; |
| 14 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
| 15 | +use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 18 | + |
| 19 | +final class UnusedImportRemovingPostRector extends AbstractPostRector |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser, |
| 23 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 24 | + private readonly RectorConfigProvider $rectorConfigProvider, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + public function enterNode(Node $node): ?Node |
| 29 | + { |
| 30 | + if (! $this->rectorConfigProvider->shouldRemoveUnusedImports()) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + if (! $node instanceof Namespace_) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + $hasChanged = false; |
| 39 | + |
| 40 | + $names = $this->resolveUsedPhpAndDocNames($node); |
| 41 | + |
| 42 | + foreach ($node->stmts as $key => $namespaceStmt) { |
| 43 | + if (! $namespaceStmt instanceof Use_) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + $useUse = $namespaceStmt->uses[0]; |
| 48 | + // skip aliased imports, harder to check |
| 49 | + if ($useUse->alias !== null) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + $comparedName = $useUse->name->toString(); |
| 54 | + if ($this->isUseImportUsed($comparedName, $names)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + unset($node->stmts[$key]); |
| 59 | + $hasChanged = true; |
| 60 | + } |
| 61 | + |
| 62 | + if ($hasChanged === false) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + $node->stmts = array_values($node->stmts); |
| 67 | + return $node; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * The higher, the later |
| 72 | + */ |
| 73 | + public function getPriority(): int |
| 74 | + { |
| 75 | + // run this last |
| 76 | + return 100; |
| 77 | + } |
| 78 | + |
| 79 | + public function getRuleDefinition(): RuleDefinition |
| 80 | + { |
| 81 | + return new RuleDefinition('Removes unused import names', [ |
| 82 | + new CodeSample( |
| 83 | + <<<'CODE_SAMPLE' |
| 84 | +namespace App; |
| 85 | +
|
| 86 | +use App\SomeUnusedClass; |
| 87 | +
|
| 88 | +class SomeClass |
| 89 | +{ |
| 90 | +} |
| 91 | +CODE_SAMPLE |
| 92 | + , |
| 93 | + <<<'CODE_SAMPLE' |
| 94 | +namespace App; |
| 95 | +
|
| 96 | +class SomeClass |
| 97 | +{ |
| 98 | +} |
| 99 | +CODE_SAMPLE |
| 100 | + ), |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return string[] |
| 106 | + */ |
| 107 | + private function findNonUseImportNames(Namespace_ $namespace): array |
| 108 | + { |
| 109 | + $names = []; |
| 110 | + |
| 111 | + $this->simpleCallableNodeTraverser->traverseNodesWithCallable($namespace, static function (Node $node) use ( |
| 112 | + &$names |
| 113 | + ) { |
| 114 | + if ($node instanceof Use_) { |
| 115 | + return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 116 | + } |
| 117 | + |
| 118 | + if (! $node instanceof Name) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + $names[] = $node->toString(); |
| 123 | + return $node; |
| 124 | + }); |
| 125 | + |
| 126 | + return $names; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return string[] |
| 131 | + */ |
| 132 | + private function findNamesInDocBlocks(Namespace_ $namespace): array |
| 133 | + { |
| 134 | + $names = []; |
| 135 | + |
| 136 | + $this->simpleCallableNodeTraverser->traverseNodesWithCallable($namespace, function (Node $node) use ( |
| 137 | + &$names |
| 138 | + ) { |
| 139 | + if (! $node->hasAttribute(AttributeKey::COMMENTS)) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); |
| 144 | + $names = array_merge($names, $phpDocInfo->getAnnotationClassNames()); |
| 145 | + }); |
| 146 | + |
| 147 | + return $names; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @return string[] |
| 152 | + */ |
| 153 | + private function resolveUsedPhpAndDocNames(Namespace_ $namespace): array |
| 154 | + { |
| 155 | + $phpNames = $this->findNonUseImportNames($namespace); |
| 156 | + $docBlockNames = $this->findNamesInDocBlocks($namespace); |
| 157 | + |
| 158 | + return array_merge($phpNames, $docBlockNames); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @param string[] $names |
| 163 | + */ |
| 164 | + private function isUseImportUsed(string $comparedName, array $names): bool |
| 165 | + { |
| 166 | + if (in_array($comparedName, $names, true)) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + // match partial import |
| 171 | + foreach ($names as $name) { |
| 172 | + if (str_ends_with($comparedName, $name)) { |
| 173 | + return true; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return false; |
| 178 | + } |
| 179 | +} |
0 commit comments