-
Notifications
You must be signed in to change notification settings - Fork 0
Add remove imports method #59
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
base: master
Are you sure you want to change the base?
Changes from all commits
adf1204
efab0d1
996ae46
4e7b935
3fbb081
a65acbb
8de82a8
92ef39f
a21e20b
19e86cb
054bddc
e6475dd
e76faa2
fc673cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Visitors; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Stmt\GroupUse; | ||
| use PhpParser\Node\Stmt\Use_; | ||
| use PhpParser\Node\UseItem; | ||
| use PhpParser\NodeFinder; | ||
| use RonasIT\Larabuilder\Nodes\PreformattedCode; | ||
|
|
||
| class RemoveImport extends AbstractNodeVisitor | ||
| { | ||
| protected array $allowedParentNodesTypes = self::ANY_TYPE; | ||
|
|
||
| protected NodeFinder $nodeFinder; | ||
|
|
||
| public function __construct( | ||
| protected string $import, | ||
| protected bool $force = false, | ||
| ) { | ||
| $this->nodeFinder = new NodeFinder(); | ||
| } | ||
|
|
||
| public function afterTraverse(array $nodes): ?array | ||
| { | ||
| $targetNodes = &$this->getNamespaceStatements($nodes); | ||
|
|
||
| foreach ($targetNodes as $node) { | ||
| if ($node instanceof Use_ || $node instanceof GroupUse) { | ||
| $this->removeTargetImport($node, $targetNodes); | ||
| } | ||
| } | ||
|
|
||
| $targetNodes = array_filter($targetNodes, fn ($node) => !$this->isEmptyImportNode($node)); | ||
|
artengin marked this conversation as resolved.
|
||
|
|
||
| return $nodes; | ||
| } | ||
|
|
||
| protected function removeTargetImport(Use_|GroupUse $node, array $targetNodes): void | ||
| { | ||
| $prefix = $node instanceof GroupUse ? $node->prefix : null; | ||
|
|
||
| $node->uses = array_filter($node->uses, fn (UseItem $useItem) => !$this->shouldRemove($useItem, $targetNodes, $prefix)); | ||
| } | ||
|
|
||
| protected function shouldRemove(UseItem $useItem, array $targetNodes, ?Name $prefix = null): bool | ||
| { | ||
| if ($this->resolveFqcn($useItem, $prefix) !== $this->import) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($this->force) { | ||
| return true; | ||
| } | ||
|
|
||
| $resolvedName = $useItem->alias?->name ?? $useItem->name->getLast(); | ||
|
|
||
| return !$this->isImportUsed($resolvedName, $targetNodes); | ||
| } | ||
|
|
||
| protected function resolveFqcn(UseItem $useItem, ?Name $prefix): string | ||
| { | ||
| return $prefix !== null | ||
| ? $prefix->toString() . '\\' . $useItem->name->toString() | ||
| : $useItem->name->toString(); | ||
| } | ||
|
|
||
| protected function isEmptyImportNode(Node $node): bool | ||
| { | ||
| return ($node instanceof Use_ || $node instanceof GroupUse) && empty($node->uses); | ||
| } | ||
|
|
||
| protected function isImportUsed(string $importName, array $targetNodes): bool | ||
| { | ||
| $nodesWithoutImports = array_filter($targetNodes, fn ($node) => !($node instanceof Use_) && !($node instanceof GroupUse)); | ||
|
|
||
| if ($this->hasUsageOf($importName, $nodesWithoutImports)) { | ||
|
Comment on lines
+77
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a requested import is referenced only from PHPDoc, such as Useful? React with 👍 / 👎. |
||
| return true; | ||
| } | ||
|
|
||
| $preformattedNodes = $this->nodeFinder->find($nodesWithoutImports, fn (Node $node) => $node instanceof PreformattedCode); | ||
|
|
||
| foreach ($preformattedNodes as $preformattedNode) { | ||
| /** @var PreformattedCode $preformattedNode */ | ||
| if ($this->hasUsageOf($importName, $preformattedNode->code)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| protected function hasUsageOf(string $name, array $nodes): bool | ||
| { | ||
| return !empty($this->nodeFinder->findFirst( | ||
| $nodes, | ||
| fn (Node $node) => $node instanceof Name && get_class($node) === Name::class && $node->getFirst() === $name, | ||
|
artengin marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The usage detector matches any plain Useful? React with 👍 / 👎. |
||
| )); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afterTraverse()assignsarray_filter(...)directly to$targetNodes, butarray_filterkeeps original numeric keys. When this visitor is chained withaddImports(),BaseNodeVisitorAbstract::getInsertIndex()uses those sparse keys as if they were positional indexes, soarray_splicecan insert a newuseafter the class (or below the import separator) if an earlier import was removed. This can produce invalid PHP in workflows likeremoveImports(...)->addImports(...); reindexing the filtered array (e.g.,array_values) avoids the offset mismatch.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array_valuesis not needed here,testRemoveImportsThenAddImportsconfirms this