With the Extract Protocol refactoring you can create a new protocol based on the members of the current class. The created protocol will be implemented automatically.
- Select a class in the Project view or Structure view or select its name in the editor.
- On the main menu or on the context menu of the selection, choose Extract Interface dialog box appears. . The
- In the Interface Name text box, 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 check box. 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 check box.
- 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 drop-down 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 check box 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.