简体   繁体   中英

PhpStorm not refactoring classes correctly?

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 :

在此处输入图片说明

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

在此处输入图片说明

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.

在此处输入图片说明

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

You have invoked Refactor | Rename 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 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 (eg 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM