PhpStorm 2023.3 Help

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

  1. Select a class in the Project view or Structure view or select its name in the editor.

  2. From the main menu or from the context menu of the selection, select Refactor | Extract | Interface. The Extract Interface dialog appears.

  3. In 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.

  4. 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.

  5. Specify the namespace to which the new interface will belong. By default, the namespace of the source class in selected.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

Last modified: 25 March 2024