Extract Interface Refactoring for PHP
The PhpStorm 7 EAP introduces a new refactoring: Extract Interface. The Extract Interface refactoring allows users to quickly create a new interface based on a selected interface or class.
Imagine we have a PersonRepository which features several methods for retrieving and storing data. Chances are that similar functions to getById(), getAll(), save() and delete() will be used in other classes as well. An ideal candidate for an interface!
Let’s give the Extract Interface refactoring a go!
When placing the caret on the PersonRepository class name, we can use the context menu and find the Refactor | Extract | Interface action. A dialog will pop up in which we can select different options for the refactoring.
First of all, we can give the extracted interface a name. In this example, IRepository would make sense. We can select if we want to replace class references with interface references where possible. This is incredibly useful if we want to generalize function parameters and type hints throughout our application.
We can also select the namespace for our new interface. PhpStorm will add the namespace as well as imports where needed in the application. Next, we can select the members that will form the interface. Finally, we can also choose if we want to keep PhpDoc blocks, copy or move them.
Once the refactoring completes, we have a fresh interface present in our project:
The PersonRepository will now also implement the IRepository interface:
Develop with pleasure!
– JetBrains PhpStorm Team