Modify namespace on "Create New PHP Class"

Here is the case. I throw in code not declared exception. PhpStorm underlines it with yellow wave line. Alt+Enter - "Create class" and "Create New PHP Class" dialog window appears. I can point this creation to desired directory. But namespace can not be edited right in the dialog window and one still needs to edit namespace after new file created. Is that OK and I must modify namespace manually or I just missed some important IDE settings to provide its more smarter behavior?

0
5 comments
Avatar
Permanently deleted user

Hello,

Could you provide a small code sample to reproduce the issue please?

0

Hello, Vladimir! Here's the code. You may remove src/Exception/WrongNameProvidedException.php file and try to re-create new class with Alt+Enter to reproduce the issue. You'll find out that 1) you can not edit namespace in dialog window and 2) newly created file will have namespace of the file from which you started creating class.

0

Maybe I am missing something, but you throw the exception in App\Model.
The exception is called with a name, not FQN, thus PhpStorm knows that the class is located in the same namespace.
That's why it doesn't allow you to change the namespace.

If the exception was called with something like Namespace1\Namespace2\WrongNameProvidedException, it would post Namespace1\Namespace2 as the namespace.

Do you think this approach is wrong?

0

If I set FQN for the new class as App\Exception\WrongNameProvidedException and hit Alt+Enter I will get proposed non-editable namespace as App\Model\App\Exception and still need to choose manually directory new file should be created in. While using absolute FQN we almost can achieve result needed (non-editable namespace field is set correctly in this case!) but now we need to modify both file name (removing namespace part from the left part) and again choose directory for the new file. In other words I can not find scenario in which this dialog is useful in the sense of reducing manual work of creating files and navigating through directories. It looks like creating new class/exception in the directory structure and then importing it with Alt+Enter is simpler and more obvious.

When I saw this dialog window for the first time I though that choosing directory new file should be placed in is more than enough for generating proper namespace (and displaying it in non-editable field) under the hood. And every time I used it I had to manually modify generated namespace.

In fact i think I just misunderstand the idea behind this use case. Or have "wrong" IDE/project settings to let this dialog window to be a little smarter. And I want just to get the idea how can i "properly" use "Create class" action if I need to create it in another namespace.

0

This was implemented per users request: https://youtrack.jetbrains.com/issue/WI-11308https://youtrack.jetbrains.com/issue/WI-16112.

Here's a feature request to allow namespace changing: https://youtrack.jetbrains.com/issue/WI-34425 - feel free to vote for it to track it's updates and increase it's prioroty.

0

Please sign in to leave a comment.