Ben
Ben

Reputation: 62356

How can I tell PHPStorm to refactor namespaces and class names?

I've noticed when I move a file in PHPStorm, it doesn't adjust the namespace at all. My project uses PSR-0, so directory structure mirrors the namespacing. Is it possible to have PHPStorm adjust the namespace when a file is moved?

Also, when renaming a file the class name is not changed and I then have to go in and rename the class. Is it possible to rename a file and the class in a single step?

I'm using 7.1.3

Upvotes: 31

Views: 22059

Answers (4)

Tofandel
Tofandel

Reputation: 3565

To have the namespace automatically changed when moving a file, I've found that you simply need to configure your project in Settings > Directories and set the source root

Settings dialog

With the correct PSR-4 prefix if necessary

Edit propertis Prefix dialog

After that any move operation will result in the namespace being automatically changed

Upvotes: 1

Thibault Witzig
Thibault Witzig

Reputation: 2200

I encountered the same issue. The reason was that I was trying to move the file (from project view), instead of moving the class (right click on the name in the source code).

Upvotes: 32

CommandZ
CommandZ

Reputation: 3611

If you use the F6 refactoring, it will allow you to move the class and change the namespace at the same time: PHPStorm docs

However, you do need to have the directory namespace mappings setup:

Upvotes: 12

Ben
Ben

Reputation: 62356

Because my application is a Laravel app the root directory was not the source of the namespacing. PHPStorm has a new option as of 8.0 that allows you to specify a directory as the namespace root: http://youtrack.jetbrains.com/issue/WI-22585

Upvotes: 6

Related Questions