Extract interface
Suppose you have a class with several methods for retrieving and storing data. It is highly probable that similar functions will be used in other classes as well. It is very helpful to have an interfaces created on the base of this classes.
Extract an interface
Select a class in the Project view or Structure view or select its name in the editor.
From the main menu or from the context menu of the selection, select Extract Interface dialog appears.
. TheIn the Interface Name field, specify the name of the interface to be created. By default, PhpStorm suggests the name of the current class from which the interface will be extracted. Update the name otherwise PhpStorm will report an error and the refactoring will no start.
Specify whether you want PhpStorm to apply the extracted interface immediately:
To have PhpStorm create an interface and attempt to replace references to the source class with references to the interface in PhpDoc comments and parameters of methods, select the Replace class reference with interface where possible checkbox. The proposed changes are compulsory displayed in a Refactoring Preview pane of the Find tool window. Note that if an instance references a method or field, which are not defined in the interface, it won't be suggested for replacement.
To have PhpStorm just create an interface in a separate file and mark the source class as implementation of the new interface, clear the Replace class reference with interface where possible checkbox.
Specify the namespace to which the new interface will belong. By default, the namespace of the source class in selected.
In the Target destination directory list, specify the folder to store the dedicated file created for the new interface. By default, the field is read-only and shows the folder that corresponds to the namespace to which the interface will belong according to the PSR0 standard.
To have the class with the interface stored in another folder, click F2 and specify the folder of your choice.
In the Members to form interface area, specify the methods to be included in the interface. This table shows all the methods detected in the current class. To have a method moved to the interface specified in the Move Member To Class, select the checkbox next to the method.
In the PhpDoc area, specify how you want PhpDoc comments treated. The available options are:
As is: select this option to have the documentation left where it is in the source class.
Copy: select this option to have the inline documentation copied to the extracted interface without removing it from its current location.
Move: select this option to have the inline documentation moved to the extracted interface and deleted from its current location in the source class.
Click Refactor when ready. If PhpStorm shows you a Refactoring Preview in the Find tool window, review the suggested changes. To have the interface extracted and the proposed changes applied, click Do Refactor.