Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPStorm refactor shortcut to give an alias in use statement

Tags:

phpstorm

When I try to use a class that causes name conflict ie. I have two classes with the same name but different namespaces and I try to use both of them PHPStorm shows a prompt to rename the second class = give it an alias with as.

<?php
use MyNamespace\SomeClass;
use MyOtherNamespace\SomeClass as SomeOtherClass;

I would like to be able to call this prompt any time when pointer is on a class preferably from a keyboard shortcut. Is this possible?

like image 902
sebastian_t Avatar asked Dec 22 '15 09:12

sebastian_t


2 Answers

With PhpStorm 2018.3 you can use "Replace with Alias" feature: https://blog.jetbrains.com/phpstorm/2018/12/new-refactorings-in-phpstorm-2018-3/

enter image description here

like image 190
MingalevME Avatar answered Nov 07 '22 00:11

MingalevME


You can use PHPStorm refactoring feature (cursor on SomeOtherClass, Shift+F6), it allows to rename alias and its usages.

click here to see the screenshot

If you have already used SomeClass in your script without an alias, first you should set an alias with the same name:

use MyNamespace\SomeClass as SomeClass;

and then refactor this alias, as decribed above.

Another one solution:

If you import a conflicting name, like Foo\MyClass and you already have use Bar\MyClass; in your document, you should be faced with the following prompt

+--------------------------------------------+
| Import class                               |
+--------------------------------------------+
| Alias name:                                |
| [________________________________________] |
|                                            |
| [X] Always create alias while class import |
|                                            |
|                          [ OK ] [ Cancel ] |
+--------------------------------------------+

The trick is to check the "Always create alias" checkbox to always be faced with the prompt while importing.

Forgot to mention that you should also uncheck the settings

Settings > Editor > General > Auto Import > [ ] Enable auto-import in file scope

Settings > Editor > General > Auto Import > [ ] Enable auto-import in namespace scope

https://laracasts.com/discuss/channels/general-discussion/phpstorm-importing-namespaces-with-aliases/replies/99388

like image 5
medveddev Avatar answered Nov 06 '22 23:11

medveddev