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:
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:
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
Subscribe to Blog updates
Thanks, we've got you!
Qodana Clone Finder: Early Access Program
In December 2020, we announced the EAP for Qodana, which is rapidly evolving into a comprehensive platform that allows companies to perform multi-level evaluations of the quality of code they own, contract, or purchase. Qodana helps you detect bugs without relying on an IDE, either on a local machi…
PhpStorm 2020.2 EAP #3
The Early Access Program for PhpStorm 2020.2 is in full swing and today we’ve got the third build of the 2020.2 EAP for you. In this blog post, you can read about improvements for Git installed in WSL2 and Search Everywhere. Download PhpStorm 2020.2 EAP (more…)…
PhpStorm 2019.3.3 Preview
We’ve just rolled out a preview for the third minor update for PhpStorm 2019.3. Please give the PhpStorm 2019.3.3 Preview build 193.6494.5 a try and share your feedback with us. (more…)…
How to Upgrade to PHP 7.4 with PhpStorm
Check out our fresh video with a few nice tips and tricks on how to update your codebase to PHP 7.4. It combines materials from the PhpStorm 2019.2 and PhpStorm 2019.3 releases, with a few cool bonuses to boot. Nikita drew our attention to the fact that setters are slower than assigning prop…