PhpStorm 2021.3 EAP #5: Improved Refactorings

PhpStorm 2021.3 EAP build #5 is now available! To catch up on all the new features coming to PhpStorm 2021.3, check out our previous EAP blog posts.
In this blog post, you can read about how Inline method, Inline variable, and Extract variable will be improved in the upcoming release.
The Toolbox App is the easiest way to get the latest EAP builds and keep both your stable and EAP versions up to date. You can also manually download the EAP builds from our website.

Download PhpStorm 2021.3 EAP

Improved Refactorings

We’re constantly working on improving the refactorings available in PhpStorm. In the upcoming release, you’ll see Inline function, Inline variable, and Extract variable working significantly better, with edge cases covered, bugs resolved, and new quick-fixes added.

All of the improvements are already available to try in this EAP build.

Let’s take a quick look at what we have improved for these refactorings.

Inline function or method

When a method body is more obvious than the method itself, it makes sense to replace calls to the method with the method’s content and delete the method itself.

To use this refactoring, position the caret at the redundant method and press Ctrl+Alt+N (⌥⌘N on macOS).

Here are the highlights of the improvements:

Allow refactoring non-static methods WI-53049.
Disable for functions with multiple return statements WI-61834.
Show a chooser in case multiple resolve WI-61839.
Support method chains WI-60622.
Don’t allow inlining methods which implements the interface abstract class method WI-60744.
Forbid inlining magic methods like __get/__sleep/__construct WI-60746.
Fix exception when attempting to inline __invoke methods WI-60745.
Add additional parenthesis around the “new” expression WI-60753.
Replace duplicate variable names inside the match arm WI-60756.
Fixes for parent/self/static::f() calls WI-60752.
Support arrow function/anonymous function as parameters WI-60755.

There is also a new quick-fix available. If there is a private method that is only used once in the code, you can press Alt+Enter on it and choose Inline method:

Inline variable

Sometimes you may have redundant variables in the code that just add clutter and can be removed.

A temporary variable that’s assigned as the result of a simple expression and nothing more is a good example. In this case, it makes sense to replace the references to the variable with the expression itself.

To use this refactoring, position the caret at the redundant variable and press Ctrl+Alt+N (⌘⌥N on macOS).

If you don’t remember the refactoring name or shortcut, you can also use Ctrl+T (⌃T) and choose one from the list.

If a variable has only been used once, then you can inline it with a quick-fix by pressing Alt+Enter:

Here is what else has been improved for Inline Variable:

Don’t allow inlining unary operations (++, –) WI-62666.
Support inlining NOWDOC/HEREDOC variables WI-60083.
Proper escape variable if NOWDOC is inlined into HEREDOC WI-60084.
Replace duplicate variable names inside the match arm WI-60756.
Fixes for inlining concatenated strings WI-49266.
Fixed bug with ternary expression WI-55004.
Fixed bug with quotes WI-56065.To see more examples of different refactorings in PhpStorm, check out the Refactoring with PhpStorm video series by Matthew Setter.

Introduce (extract) variable

If you have an expression that’s hard to understand, it makes sense to replace the result of the expression or its parts with separate variables that are self-explanatory. That’s what the Extract variable does.

Press Ctrl+Alt+V (⌘⌥V) on any expression and create a new variable from it.

If you select some code, PhpStorm will replace it with a new variable. If you have not selected anything, then you will see a drop-down list with suggestions to extract. To reduce your cognitive load, the list will not include assignment expressions or other things that don’t make sense. Neat!

If you decide to go back and undo the refactoring with Ctrl+Z, every change will be back, and even your caret will be put back in the same position it was in before the refactoring. So really does not hurt to try refactoring as you can easily undo it.

Here are some other interesting cases of Introduce Variable refactoring that we covered in the upcoming release.

Smart extract from condition:

Extracting array item from a loop:

Extract Method

We improved Extract method refactoring significantly inPhpStorm 2021.2. Read the full story of Extract Method refactoring, renewed and improved.

We hope you are getting a lot out of all these improvements! Please share your feedback in the comments or file tickets on our issues tracker if you find any bugs.

To see more examples of different refactorings in PhpStorm, check out the Refactoring with PhpStorm video series by Matthew Setter.

The full list of changes in this build is available in the release notes.

Important! PhpStorm EAP builds are not fully tested and may be unstable.
You can install an EAP build side by side with a stable PhpStorm version to try out the latest features.
EAP builds are free to use but expire 30 days after the build date.

Please report any problems you find to our issue tracker or comment on this post!

Your JetBrains PhpStorm team
The Drive to Develop

The PhpStorm Blog : The Lightning-Smart IDE for PHP Programming | JetBrains Blog
Read More

Latest News

PHP-Releases

PHP 8.0.23 released!

PHP 8.1.10 released!

PHP 7.4.30 released!