Skip to content

Error with merged carbon imports with importNames and removeUnusedImports.#7811

Closed
BinaryKitten wants to merge 4 commits intorectorphp:mainfrom
BinaryKitten:merged-import-namespace-carbon-issue
Closed

Error with merged carbon imports with importNames and removeUnusedImports.#7811
BinaryKitten wants to merge 4 commits intorectorphp:mainfrom
BinaryKitten:merged-import-namespace-carbon-issue

Conversation

@BinaryKitten
Copy link
Copy Markdown
Contributor

When using Carbon as a class and the CarbonInterface - we get a weird merged import process.

this imports use Carbon\Carbon\CarbonInterface which is not valid.

Test case here in PR
Demo
https://getrector.com/demo/14fa4e52-32b1-4931-811e-17d522a8c833

@samsonasik
Copy link
Copy Markdown
Member

The removeUnusedImports doesn't detect @property for now, I will check if that can be detected ;)

@BinaryKitten
Copy link
Copy Markdown
Contributor Author

BinaryKitten commented Jan 6, 2026

it's weird that its creating the use Carbon\Carbon\CarbonInterface; and use Illuminate\Support\Carbon\CarbonInterface lines, both of which are invalid though?

It only occurs when there's a class named the start of the other - otherwise it doesn't import the @property - which is almost better (having @property support would be great - but would setting for not breaking the import with invalid merged class name/namespace.

@BinaryKitten
Copy link
Copy Markdown
Contributor Author

@samsonasik if you could point me in the general direction - i'd love to submit a possible fix - learn more and be useful :)

@samsonasik
Copy link
Copy Markdown
Member

@BinaryKitten the remove unused imports is on UnusedImportRemovingPostRector, it basically if @property type can be fetch, it will be correctly detected, see this part:

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($nodeToCheck);
$names = [...$names, ...$phpDocInfo->getAnnotationClassNames()];
$constFetchNodeNames = $phpDocInfo->getConstFetchNodeClassNames();
$names = [...$names, ...$constFetchNodeNames];
$genericTagClassNames = $phpDocInfo->getGenericTagClassNames();
$names = [...$names, ...$genericTagClassNames];
$arrayItemTagClassNames = $phpDocInfo->getArrayItemNodeClassNames();
$names = [...$names, ...$arrayItemTagClassNames];

To add more resolved class, you can add more PhpDocNodeDecoratorInterface implementation, for example:

final readonly class ConstExprClassNameDecorator implements PhpDocNodeDecoratorInterface

then register it:

private const PHP_DOC_NODE_DECORATOR_CLASSES = [
ConstExprClassNameDecorator::class,
DoctrineAnnotationDecorator::class,
ArrayItemClassNameDecorator::class,
PhpDocTagGenericUsesDecorator::class,
];

then, you may can fetch via PhpDocInfo :)

@@ -0,0 +1,43 @@
<?php
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only for autoimport + remove, nothing todo with rename, so you can move these fixture tests to tests/Issues/NamespacedUseAutoImport which only check on this functionality for auto import + remove unused import:

https://github.com/rectorphp/rector-src/tree/main/tests/Issues/NamespacedUseAutoImport/Fixture

@samsonasik
Copy link
Copy Markdown
Member

handled at #7957

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants