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

Allow AliasedObjectType adding to UseNodesToAddCollector #8768

Closed
pincher2012 opened this issue Jul 31, 2024 · 1 comment
Closed

Allow AliasedObjectType adding to UseNodesToAddCollector #8768

pincher2012 opened this issue Jul 31, 2024 · 1 comment
Labels

Comments

@pincher2012
Copy link

pincher2012 commented Jul 31, 2024

Feature Request

I want create Rector that import missed imports for doctrine annotations for example:

declare(strict_types=1);

+use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Entity
{
}

I found out that I can add new imports via UseNodesToAddCollector. UseNodesToAddCollector can only use FullyQualifiedObjectType as input, but can return AliasedObjectType. Moreover some annotations do not take into account that UseNodesToAddCollector can return AliasedObjectType

I think that UseNodesToAddCollector must be able to accept AliasedObjectType as input.

May be we should introduce interface for FullyQualifiedObjectType and AliasedObjectType.

I think that this code will be more semantic

if ($this->shouldAddUse($phpDocInfo)) {
-    $this->useNodesToAddCollector->addUseImport(new FullyQualifiedObjectType('Doctrine\ORM\Mapping as ORM'));
+    $this->useNodesToAddCollector->addUseImport(new AliasedObjectType('ORM', 'Doctrine\ORM\Mapping'));

    return $node;
}

May be I even could provide PR after some research.

@TomasVotruba
Copy link
Member

Thanks for the feature request and thank you for the patience.

We've used to provide aliases in the path, but it made FQN importing twice as complex with many bug-cases poping out here and there. That's why we took a path to use FQN first.

But if you want to use alises on e.g. Doctrine annotations/attributes, you can easily create a custom Rector rule that handles Use_ imports and groups them this way. This could be achieve in no more than 50-lines and would apply on all your files at once 👍

Thanks for understanding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants