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
NormalizeNamespaceByPSR4ComposerAutoloadRector: Replace references correctly #2644
Conversation
e2e/psr-4/rector.php
Outdated
]); | ||
|
||
$rectorConfig->rule(NormalizeNamespaceByPSR4ComposerAutoloadRector::class); | ||
$rectorConfig->rule(ClassRenamingPostRector::class); |
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.
Not sure if this is needed but it does not work either way – it does not look like the ClassRenamingPostRector
does anything to fully-qualified name references.
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.
Apparently, it is not necessary. And it is skipping the processing here:
rector-src/rules/Renaming/NodeManipulator/ClassRenamer.php
Lines 164 to 166 in 02cec7e
$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE); | |
if ($this->shouldSkip($newName, $name, $parentNode)) { | |
return null; |
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.
Apparently, the $parentNode
is null
. Possibly FullyQualifyStmtsAnalyzer
is removing the attribute?
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.
FullyQualifyStmtsAnalyzer->process()
produce create new FullyQualified
instance, the attribute may need to be mirrored:
return new FullyQualified($name, $node->getAttributes());
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.
Even then, it still does not seem to work properly – only fixes references in one of the files (weird, they are structurally identical). Will need to do more digging.
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.
Possibly, you can re-use RenamedClassesDataCollector
. Add old -> new names there and it should be picked up in ClassRenamingPostRector
and resolve this for you.
The post-Rector are run after all other rules, to post-process the final printer file code.
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.
I am using RenamedClassesDataCollector
now and it still updates the references only in one of the files correctly but not the other. Possibly by that time the namespace of the referenced file is changed so the rename no longer finds it.
ad3d9ce
to
0b961d2
Compare
rules/PSR4/Rector/FileWithoutNamespace/NormalizeNamespaceByPSR4ComposerAutoloadRector.php
Outdated
Show resolved
Hide resolved
Do you need help with this one? It seems almost finished 🙂 |
I have yet to find time to investigate why it still does not work. Not sure when I will be able to work on this. |
67ff423
to
a399935
Compare
It will be needed in `NormalizeNamespaceByPSR4ComposerAutoloadRector` next. Additionally, make it use `ClassAnalyzer::isAnonymousClass` to cover more cases.
…rrectly Previously, `NormalizeNamespaceByPSR4ComposerAutoloadRector` did not update references to other normalized classes properly. This change fixes that by recording the renames using `RenamedClassesDataCollector`.
a399935
to
021123a
Compare
@jtojnar We'd love to have this feature in Rector :) What is missing here to make CI pass? |
I would expect the code to work as is so there is something wrong with my assumptions. Unfortunately, I still have not found a time to dive into Rector innards to learn how rules are interleaved, and how |
Closing as the buggy rule is now removed to avoid causing harm. |
Previously,
NormalizeNamespaceByPSR4ComposerAutoloadRector
did not update references to other normalized classes properly. This change fixes that by recording the renames usingRenamedClassesDataCollector
.Supersedes #2482