-
-
Notifications
You must be signed in to change notification settings - Fork 670
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
Incorrect behavior of PseudoNamespaceToNamespaceRector #7016
Comments
Hi, thanks for reporting 👍 For more complex issues like this, we need a reproducible repository. I saw you've invested way too much energy, so I'll make one myself based on your input. Then we'll take it from there. |
I've created the repository here: https://github.com/TomasVotruba/rector-underscore-to-psr4 I'm testing the +namespace Do\Some;
final class Do_Some_Stuff
{
- public function test(): DateTimeInterface
+ public function test(): \DateTimeInterface
{
- return new DateTimeImmutable('now');
+ return new \DateTimeImmutable('now');
}
} 👍 Yet, this rule does not matter for your use case at all. We won't use it. For second rule, $services->set(PseudoNamespaceToNamespaceRector::class)
->configure([
new PseudoNamespaceToNamespace('PHPUnit_');
]); ↓ -class MyTest extends PHPUnit_Framework_TestCase {}
+class MyTest extends \PHPUnit\Framework\TestCase {} It applies to use of code, not the classes. You cannot refactor your own code with it, as it's not deterministic where the class should be located after unwrapping the underscore namespaces. We will not need this rule, as your use case is different. It seems your use case kinda combines both rules together. If I understand it correctly, you need to:
No Rector rule can do it at the moment. You'd have to create a new rule, that would handle the 1st step, unwrapping of namespaces to directory. Then generatate the list of renamed classes, and run |
This is how such rule would look like: TomasVotruba/rector-underscore-to-psr4#1 |
Thank you for the detailed responses and example repository. I don't have the energy to get into the details tonight but I'll check these out some time tomorrow and get back to you then. Again, thank you for following up from here and Reddit. |
Hi Tomas, I've read through your reply and the two example repositories. One thing I should clarify is that, prior to the discovery of invalid classes after the transformation was applied, there wasn't an expectation that the file would move. The existing code was using PSR-0 autoloading so The If In the fixture for It sounds like, other than maybe clarifying the documentation on the Pseudo rector, that this issue can be closed as "working as intended / unsupported scenario". ¯\_(ツ)_/¯ I'll take it from here when I revisit this in a week or two. Thank you again for your effort and following up! |
Thanks for reaching out. I wish we got a question issue before you started, so I could save you from the suffering. Sometimes I struggle with other PHP tools (all the dev dependencies) for hours or days, then I pose the question in despair on GitHub/Twitter. Just to learn there is a new 3rd solution to my problem. It might help (also for other renaming rules) to grasp the philosophy of Rector. It does not replace PHPStorm. If we need to rename single class itself and all its references, we use refactor -> rename class. But if we upgrade from PHPUnit 4 to 8 and somebody else renamed 50 classes, here is where Rector helps. This also applies for rename method, rename property etc. I hope this will help with use of other rules. If you find better wording for the mentioned rules docs, please send an improvement, to clarify it for future developers. If you have more refactoring you need help in your project, reach me via email. I'll share what I know to automate the process and hopefully, make your life easier 👍 |
Bug Report
When
PseudoNamespaceToNamespaceRector
renames a class and adds anamespace
declaration it doesn't maintain references to other unprefixed classes (e.g. belowDateTimeInterface
becomes invalid and becomes a reference toDo\Stuff\DateTimeInterface
instead).Minimal PHP Code Causing Issue
See https://getrector.org/demo/e326b671-4568-4929-8b12-efb661d68596
NormalizeNamespaceByPSR4ComposerAutoloadRector
it handles other class references correctly but the namespace + class becomesDo\Some\Do_Some_Stuff
.PseudoNamespaceToNamespaceRector
is used."\\Do\\Some\\": "src/"
then it correctly prefixes other classes and shortens the actual class name, but it prefixes the namespace declaration itself with an extra\
which is a syntax error and makes all class references use a FQCN (including those covered by an existinguse
).Responsible rules
PseudoNamespaceToNamespaceRector
NormalizeNamespaceByPSR4ComposerAutoloadRector
Expected Behavior
Ideally
PseudoNamespaceToNamespaceRector
is all that would be necessary to do what it already does and prefix all other class references not already covered by ause
statement with\
.The text was updated successfully, but these errors were encountered: