Move and Copy refactorings
Basics
The Move refactorings allow you to move classes, members (methods, variables, and constants), files, and directories within a project. So doing, PhpStorm tracks these movements and automatically corrects all references to the moved symbols in the source code.
Moving a file or a directory to another directory
Select the file or directory in the Project tool window.
- Do one of the following:
On the main menu, choose
.On the context menu of the selection, choose
Press F6.
The dialog that opens depends on the type of the selected symbol.
In the To directory field, specify the folder to move the selected file or folder to. Choose an existing folder from the drop-down list or type the full path to the parent folder to be created.
To have the references to the selected file or folder updated according to the refactoring result, select the Search for references checkbox.
Moving a PHP namespace
PhpStorm supposes that the namespaces in your project are arranged in compliance with the PSR0 standard and enforces you to retain the folder structure and the namespace hierarchy in accordance with this standard when moving namespaces.
When you specify a namespace to move a namespace to, PhpStorm automatically updates the Target Destination Directory field, which shows the path to the folder that corresponds to the namespace in question.
To move a namespace with all or some of its related classes to another namespace
In the editor, place the caret at the namespace to move and choose Project tool window and then choose , the Move Directory refactoring will be invoked.
on the main menu or on the context menu of the selection. Note that if you select the folder that corresponds to the required namespace in the- In the Move Namespace Dialog that opens, specify the new namespace name and the folder that will correspond to it.
The New Namespace Name field shows the fully qualified name of the selected namespace. Specify the new namespace name. Use only backslashes (
\
) as namespace separators.- When the dialog box opens, the Target Destination Directory field shows the path to the folder which corresponds to the current namespace.
The path is displayed in the following format: ...\<project root folder>\<path to the current namespace folder relative to the project root>. The path is updated automatically as you specify the new namespace name. However, if you are going to move a namespace to another parent namespace, PhpStorm will not suggest the proper folder unless you appoint a root folder for your namespace structure by marking the relevant folder as Sources on the Directories page of the Settings dialog.
See Directories for details.
Do one of the following:
Accept the preselected path displayed in the field.
Choose another path from the list. All of them are evaluated from the namespace root or from the current directory, so it is safe to choose any of them.
Click and select a folder in the dialog that opens.
Press F2 and edit the preselected path. Keep in mind that this may cause problems with automatic loading in the future.
- Click Refactor and in the Move Files with Related Namespaces dialog box that opens specify the classes and files to be moved to the new namespace and the new folder.
The upper pane of the dialog lists the destination namespaces and folders for classes and files related to the namespace in question. Each item in the list corresponds to a class/file. When you move the cursor to an item, the bottom pane shows the contents of the file related with it.
To have a class and the corresponding file moved to the destination namespace and the destination folder, select the checkbox next to the namespace/folder.
To add all the items to the list or remove all of them from the list, click Select All or Unselect All respectively.
Click OK to start the refactoring.
Moving a static PHP method, variable, or constant to another class
Open a PHP class in the editor and select a static method, variable, or constant or just position the cursor at it.
On the context menu of the selection or on the main menu, choose
.- In the Move Static Member Dialog that opens, specify the following:
In the Members to be moved to another class field, select the checkboxes next to the methods, constants, and variables that you want to move to another class. The list shows all the static methods, variables, and constants detected in the current class.
In the Move Member To Class text box, type the fully qualified name of the class where you want to move the members selected in the Members to be moved to another class list. PhpStorm provides completion for the class and namespace reference: as you type a class or namespace name, a suggestion list with the relevant item is displayed. You can also get a full list of available classes by pressing Ctrl+Space.
Moving an instance PHP method to another class
The Move instance method refactoring moves an instance (non-static) method to another class.
To move an instance PHP method to another class
- In the editor, position the caret at an instance method and either do one of the following:
On the main menu or context menu, choose
.Press F6.
- In the Move Instance Method dialog that opens, specify the following:
In the Select an instance expression list, select the target class to move an instance method to. The list of potential move targets includes the method parameters' classes and fields' classes in the current class.
In the Visibility area, select the preferred visibility modifier for the target method.
In the Parameter name to replace the $this reference to class <class_name> field, type the desired name of the parameter. When refactoring is performed, the parameter will be added for the method being moved, which will replace all
$this
references to the current class.
Initiate refactoring by clicking Refactor or Preview. For more details, see Refactoring Source Code.
Example
Let's move the foo
instance method from class B
to class A
. As a result, PhpStorm will move the method and replace the $this
reference to class B
with created $b
parameter:
Before | After |
---|---|
class A{
}
class B {
function foo(A $a)
{
$this->bar();
return $a;
}
public function bar(){
}
} |
class A {
function foo(B $b)
{
$b->bar();
return $this;
}
}
class B {
public function bar()
{
}
} |
Moving a class to another namespace
PhpStorm supposes that the namespaces in your project are arranged in compliance with the PSR0 standard and enforces you to retain the folder structure and the namespace hierarchy in accordance with this standard when moving classes among namespaces.
When you specify a namespace to move a class to, PhpStorm automatically updates the Target Destination Directory field, which shows the path to the folder where the file that implements the class in question will be stored.
To move a class to another namespace
In the editor, place the caret at the class to move.
- Do one of the following:
On the main menu, choose
.On the context menu of the selection, choose
Press F6.
- In the Move Class dialog box, that opens, specify the namespace to move the class to and the folder where the file that implements the class will be stored.
- In the Move Class <class name> to Namespace field, appoint the namespace to move the selected class to. This can be:
A new namespace under the current namespace.
Another subordinate namespace under the current parent.
An existing or a new namespace under another parent.
- In the Target Destination Directory field, appoint the folder where you want PhpStorm to move the file that implements the class.
PhpStorm supposes that the namespaces in your project are arranged in compliance with the PSR0 standard and enforces you to retain the folder structure and the namespace hierarchy in accordance with this standard when moving classes among namespaces.
When you specify a namespace to move a class to, PhpStorm automatically updates the Target Destination Directory field, which shows the path to the folder file that implements the class in question.
When the dialog box opens, the field shows the path to the folder which corresponds to the current namespace.
The path is displayed in the following format: ...\<project root folder>\<path to the current namespace folder relative to the project root>. The path is updated automatically as you specify the new namespace name. However, if you are going to move a namespace to another parent namespace, PhpStorm will not suggest the proper folder unless you appoint a root folder for your namespace structure by marking the relevant folder as Sources on the Directories page of the Settings dialog.
See Directories for details.
Do one of the following:
Accept the preselected path displayed in the field.
Choose another path from the list. All of them are evaluated from the namespace root or from the current directory, so it is safe to choose any of them.
Click and select a folder in the dialog that opens.
Press F2 and edit the preselected path. Keep in mind that this may cause problems with automatic loading in the future.
The Move class to a new file checkbox is selected. Leave this default selection.
- In the Move Class <class name> to Namespace field, appoint the namespace to move the selected class to. This can be:
Initiate refactoring by clicking Refactor or Preview, for more details, see Refactoring Source Code.
Examples of moving PHP classes
Moving a Class to a New Namespace under the Current Namespace
Suppose, your project move_class
has a namespace namespace1
with one class Class1
implemented in the Class1.php file. Let's create a subordinate namespace namespace1\ns1
and move Class1
to it.
Open Class1.php in the editor, position the cursor at
Class1
, and press F6. The Move Class dialog opens.The Move class Class1 to namespace field shows the current namespace
namespace1
. Change it tonamespace1\ns1
.As you type, the Target destination directory read-only field is updated automatically from
...\move_class\namespace1
to...\move_class\namespace1\ns1
.Press Refactor. As a result, PhpStorm creates a new subfolder
ns1
and moves the fileClass1
to it. Accordingly, the classClass1
is moved to thenamespace1\ns1
namespace:
Moving a Class between Two Namespaces under the Same Parent
Suppose, your project move_class
has a namespace namespace1
with two subordinate namespaces namespace1\ns1
and namespace1\ns3
. These subordinate namespaces have Class1
Class3
respectively. In accordance with the PSR0 standard, these classes are implemented in the files Class1.php and Class3.php that are stored in the folders ...\move_class\namespace1\ns1
and ...\move_class\namespace1\ns3
respectively. Let's move Class3
from namespace1\ns3
to namespace1\ns1
.
Open Class3.php in the editor, position the cursor at
Class3
, and press F6. The Move Class dialog opens.The Move class Class3 to namespace field shows the current namespace
namespace1\ns3
. Change it tonamespace1\ns1
.As you type, the Target destination directory read-only field is updated automatically from
...\move_class\namespace1\ns3
to...\move_class\namespace1\ns1
.Press Refactor. As a result, PhpStorm moves the class
Class3
fromnamespace1\ns3
tonamespace1\ns1
. Accordingly, the fileClass3
is moved from folder...\move_class\namespace1\ns3
to folder...\move_class\namespace1\ns1
:
Moving a Class to an Existing Namespace under Another Parent Namespace
Suppose, your project move_class
has two parent namespaces namespace1
and namespace2
with subordinate namespaces namespace1\ns1
and namespace2\ns2
. Class2
is defined in namespace2\ns2
, and in accordance with the PSR0 standard the file Class2.php that implements it is stored in ...\move_class\namespace2\ns2
. Let's move Class2
from namespace2\ns2
to namespace1\ns1
.
Open Class2.php in the editor, position the cursor at
Class2
, and press F6. The Move Class dialog opens.The Move class Class2 to namespace field shows the current namespace
namespace2\ns2
. Change it tonamespace1\ns1
.As you type, the Target destination directory read-only field is updated automatically from
...\move_class\namespace2\ns2
to...\move_class\namespace1\ns1
.Press Refactor. As a result, PhpStorm moves the class
Class2
fromnamespace2\ns2
tonamespace1\ns1
. Accordingly, the fileClass2
is moved from folder...\move_class\namespace2\ns2
to folder...\move_class\namespace1\ns1
:
Moving a Class to a New Namespace Outside the Immediate Parent Namespace
Suppose, your project move_class
has two parent namespaces namespace1
and namespace2
with subordinate namespaces namespace1\ns1
and namespace2\ns2
. Class2
is defined in namespace2\ns2
, and in accordance with the PSR0 standard the file Class2.php that implements it is stored in ...\move_class\namespace2\ns2
. Let's move Class2
from namespace2\ns2
to a new namespace namespace3
that does not exist yet. To meet the PSR0 requirements, a new folder ...\move_class\namespace3
for the namespace3
namespace should be also created.
When starting the refactoring we should keep in mind that the new namespace is to be created outside the immediate parent namespace of our class's namespace, therefore PhpStorm will not suggest a proper folder for it. So we can either specify the target folder manually or appoint a root folder for namespaces which will be the starting point for PhpStorm to calculate the folder for the new namespace.
Open Class2.php in the editor, position the cursor at
Class2
, and press F6. The Move Class dialog opens.- The Move class Class2 to namespace field shows the current namespace
namespace2\ns2
. Change it tonamespace3
. The result depends on whether you have appointed the root folder for your namespaces or not.- If no root folder is specified, the Target destination directory field still shows the path to
...\move_class\namespace2\ns2
because PhpStorm cannot calculate the path to the folder for the new namespacenamespace3
to be created:To solve the problem, press F2 and type the path manually:
...\move_class\namespace2\ns2
- Alternatively, specify the root folder for your namespaces by marking the
...\move_class\
folder as Sources as described in Marking directories. Note that this should be done before you start the refactoring, so if you have already opened the Move Class dialog, close it first.After you have appointed the
...\move_class\
folder as the root for your namespaces, PhpStorm will calculate the folder for the new namespace and update the Target Destination Directory field automatically: Press Refactor. As a result, PhpStorm moves the class
Class2
fromnamespace2\ns2
tonamespace3
. Accordingly, the fileClass2
is moved from folder...\move_class\namespace2\ns2
to folder...\move_class\namespace3
:
- If no root folder is specified, the Target destination directory field still shows the path to
Copy/Clone refactoring
The Copy refactoring allows you to copy a class, file, or directory with its entire structure from one directory to another, or clone it within the same directory.
To copy a class, file, or directory
Select the desired item in one of the views or open it in the editor.
- Do one of the following:
On the main menu or on the context menu of the selection, choose
.Press F5.
Select the desired class in the Project tool window and drag it to the target destination with the Ctrl key pressed.
In the Copy dialog box that opens specify the new name and destination, then click OK.
To clone a class or file
Select the desired item in one of the views or open it in the editor.
Press N/A.
In the Clone dialog box that opens specify the new name, then click OK.