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

struggling with my first rule #8784

Closed
DanielRuf opened this issue Aug 13, 2024 · 5 comments
Closed

struggling with my first rule #8784

DanielRuf opened this issue Aug 13, 2024 · 5 comments

Comments

@DanielRuf
Copy link

Question

I'm trying to change some code like that:

use \Namespace\ClassD;

ClassD::getA();
ClassD::getA(1,2,3);

to this:

use \Namespace\ClassC;

ClassC::getB()->getValue();
ClassC::getB()->getValue(1, null, 2,3);

I tried to change name and class of StaticCall::class but I can not get the args. And I don't know how to change or add the use statement.

any pointers for the smallest possible solution?

@samsonasik
Copy link
Member

First, there is RenameClassRector + auto import with remove unused import for that + RenameMethodRector

https://getrector.com/demo/a38638ff-7f36-4b24-9d49-dc7a9daaff80

then after that, you can create custom rule to inject argument in the middle on MethodCall args which came from Namespace\ClassC::getB() StaticCall, and place the rule after that to inject the argument as needed.

@DanielRuf
Copy link
Author

DanielRuf commented Aug 13, 2024

Thanks, that brings me a bit further.

then after that, you can create custom rule to inject argument in the middle on MethodCall args which came from Namespace\ClassC::getB() StaticCall, and place the rule after that to inject the argument as needed.

Hm, I tried that but probably $node->args and $node->getArgs() is not correct. $node is StaticCall according to my IDE.

What about the extra getB() in getB()->getValue()? Can I simply use a string like getB()->getValue? Doesn't seem so:

[ERROR] "getB()->getValue" is not a valid method name

@samsonasik
Copy link
Member

You can learn about Node structure at AST page https://getrector.com/ast

For example https://getrector.com/ast/a4686a7810518c6c740f126e056bdb77c39b4847 , I renamed namespace named "Namespace" to "Some" for demo due to namespace is php syntax to avoid error.

@DanielRuf
Copy link
Author

You can learn about Node structure at AST page https://getrector.com/ast

Sure, but since this is a support issue the quick closure is not that helpful as it leaves me (the user) hanging with just too little information. I tried different ways but so far I still need some time to grasp the whole logic.

It's great that you are that quick, but it would be better to take some time to give a complete answer.

@samsonasik
Copy link
Member

Learning AST requires time, and be slow is the way to go :)

On another chance , use discusion instead of issue https://github.com/rectorphp/rector/discussions/categories/q-a :)

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

No branches or pull requests

2 participants