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:

Give PhpStorm 7 EAP a try and let us hear your thoughts in the issue tracker, the comments below or in our forums!

Develop with pleasure!
– JetBrains PhpStorm Team

This entry was posted in Cool Feature, Early Access Program and tagged , , , . Bookmark the permalink.
  • Frank Mayer

    Very practical addition. Thank you :)

    • Maarten Balliauw

      You’re welcome :-)

  • Brett Thomas

    When will this make it into IntelliJ?

  • Brett Thomas

    Oh snap, looks like it already is…awesomeness…been waiting for this for a while.

  • Albert

    Is it possible to extract functions to abstract super classes (as abstract function)?

    • Maarten Balliauw

      This can be done using the pull members up refactoring.

      • Dave Hulbert

        “Pull members up” only seems to work if there’s already a super class. Is it possible to pull members up to a super class (optionally abstract) or trait that doesn’t exist yet? I.e. create a new parent class, the same as is done for interfaces in the blog post?

  • Pascal Larocque

    I can’t seem to get this working in Intellij 12.1.6, the option is grayed out.
    Any ideas?

    • Mikhail Vink

      Probably it’s better to try IntelliJ IDEA 13 Beta as latest PHP plugin is compatible with IntelliJ IDEA 13 only. However, if it’s grayed out, probably you don’t invoke this refactoring in the right place.