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
Subscribe to Blog updates
PhpStorm 2024.1 Early Access Program Is Now Open
This EAP build brings support for PHPUnit 11, improvements for the Pest test framework, and more.
PhpStorm 2023.3 Is Now Available
Explore the new features in PhpStorm 2023.3: support for PHP 8.3, the Pest test framework, improvements for Symfony projects and AI Assistant.
PhpStorm 2023.3 RC
PhpStorm 2023.3 Release Candidate bundles the Pest plugin into PhpStorm, so you'll now be able to enjoy it out of the box.