Inline method refactoring

In PhpStorm, you can use the Extract method refactoring to transform a group of statements into a method. Surprisingly enough, doing the opposite has not been possible up until recently. In PhpStorm 2018.1, we are introducing the Inline method refactoring.

inline-method-splash

Sometimes using a method’s body in your code turns out to be more obvious than using the method itself. The good solution for the situation is to replace the calls to the method with the actual method’s content.

The Inline method refactoring will assist you with that: simply invoke Refactor | Inline on a function or a static method call, and automatically inline the code from the called function. You can optionally delete the inlined function or method if there are no more usages:

Run suite

If the refactoring is possible, PhpStorm will perform all the necessary transformations and reformat the resulting code.

Examples

It’s not always as simple as it sounds though: in some cases, inlining a method can be quite tricky, and we’ve done our best to make sure such cases are covered properly.

For example, PhpStorm will handle functions with multiple calls as well as make use of the parameters’ default values:

Inline multiple returns

Functions with multiple return points are supported, as well:

Inline multiple returns

For class methods, PhpStorm will auto-import classes and properly handle self to resolve a method within the classes hierarchy:

Inline multiple returns

For now, PhpStorm only allows you to inline static class methods. As a workaround, you can use two refactorings on a desired method, Make method static and Inline, in sequence. In the future, we plan to support the inlining of non-static methods, too. And since this feature is under active development, feel free to report other inlining cases that don’t work as you expect, or are simply not covered by the refactoring.

Your JetBrains PhpStorm Team
The Drive to Develop

This entry was posted in Cool Feature, Newsletter and tagged , . Bookmark the permalink.