2

In all Java IDEs I have used, refactoring/renaming was a breeze because IntelliJ would scan the projects files and rename everything for me.

I am currently using a project where there are hundreds of references to a class spread around hundreds of files. I would like to refactor it so my Compound class is renamed to CompoundTag.

So instead of new Compound(), the IDE (PhpStorm) should change it to new CompoundTag().

So, first I right clicked the file, went to Refactor -> Rename:

enter image description here

Next, I renamed it and made sure to check these options:

enter image description here

Finally, after clicking refactor.. nothing happens. The file name gets changed but nothing else in the project gets refactored. PhpStorm does not even change the class name.

enter image description here

How can I fix this to get PhpStorm to properly refactor my files?

1
  • 2
    you can try to refactor by right-clicking on the class name itself. I think by changing the name of the file it is not enough, because in some php projects the classes are not the same as their file name. I'm not sure, but I think if you do the refactoring by changing the class name (and the file name is the same) the file name will change too. Commented May 13, 2017 at 19:41

1 Answer 1

4

You have invoked Refactor | Rename on a file.

What you did is you have renamed a file .. and possibly all references to a file. But in PHP world class name does not necessarily must be the same as file name .. and you may find quite a lot of older projects (especially those those with long history) where you may even see more than one class in a single file. WordPress is one of the popular examples. Surely, these days it's not a common practice (thanks to PSRs) but still...

It works for Java (in IntelliJ) because:

  • it's more common there (has been around for a really long time);
  • such renaming (rename file = rename class) is implemented in full.

For PHP this path (rename file = rename class) is simply not yet implemented.


What you need to do for PHP ... is to invoke Refactor | Rename on actual CLASS and not a file.

Refactoring actions are context dependant in IDEA-based IDEs .. so the same action will do different things depending if you invoked it on a file (e.g. in Project View panel) or on a some element under caret (in the Editor).

Such class refactoring can do more than just this class rename:

  • it can rename file
  • it can rename child classes and their files

Some links:

P.S. Similar story with just moving files and changing namespaces -- invoke Refactor | Move on a class/namespace and not on a file.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked great! Felt like an idiot after reading this, heh. Guess I was just to focused on java development, forgetting how laid back PHP really is compared to a strict language like Java, etc...
stumbling often at always great responses from LazyOne, cant be a conincidence, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.