Move Instance Method Refactoring

When you need to move a method across the class hierarchy, PhpStorm provides you with several facilities for this. For example, you can move some functionality between a class and its parent using the Pull Up/Push Down refactorings. Or you can use the Make Static refactoring in combination with Move Static Method to move a method to a different class.

To cover the cases when a method cannot be made static or moved across the class hierarchy, PhpStorm 2018.2 introduces a new Move Instance Method refactoring.

As its name states, the new refactoring allows moving an instance method to a parameter class or to a field class. Why use it after all? The primary reason is reducing the dependency between classes. If your method uses the data mostly contained in a different class, it probably makes sense to keep the method itself there, too, for simplicity and better consistency. See here for more theory behind this.

To apply the refactoring, simply invoke the Move action on a method declaration. PhpStorm will check if the new refactoring can be applied and will then prompt you to choose the target class and the visibility scope for the method to be moved:

Basic move refactoring

If the method being moved contains the $this  references to the current class, PhpStorm will let you add a parameter to replace them. The self  or parent  references will be handled correctly, too:

References move refactoring

With this new refactoring in place, moving methods between the related classes should become easier, and your classes should become more coherent. As always, please share your feedback on what works or doesn’t work for you, and we’ll look into it.

Your JetBrains PhpStorm Team
The Drive to Develop

This entry was posted in Cool Feature, Newsletter and tagged , . Bookmark the permalink.
  • https://egor.sh Egor Kokorin

    Good feature, thanks