Early Access Program

PhpStorm 2021.2 EAP #2: Extract Method Refactoring Reworked

The Extract Method refactoring will be significantly improved in the upcoming major PhpStorm release. Read on to find out what’s new and try it today in our second EAP build.

Last week we launched the Early Access Program for PhpStorm 2021.2 and we are going to be releasing updates every week for the next two months.

Download PhpStorm 2021.2 EAP

Extract Method refactoring, renewed and improved

Extract Method has been available in PhpStorm since 2011 and is one of the most used refactorings.

Problem: You have a method that’s too big and you want to split it into several smaller ones. Or you have some duplicate code that you want to eliminate.

Solution: Move the code to a new, separate method and replace the old code with a call to the method.

To use this refactoring, select any piece of code and press ⌘⌥M (Cmd+Alt+M / Ctrl+Alt+M).

Now let us tell you about all the improvements that we’re making to Extract Method in PhpStorm 2021.2.

Refactoring duplicate code

First of all, it is now easy to find candidates for refactoring thanks to the Duplicated code fragment inspection.

If your project contains almost identical chunks of code, PhpStorm will highlight the first line of each such chunk. Note that formatting, some variable names, and order of some statements may vary but the code will still be considered a duplicate.

This inspection has already been available, but now there is an added quick-fix to refactor the code.

Press Alt+Enter on the highlighted line and invoke the Extract method from duplicate code quick-fix.

All duplicate fragments will be replaced with a new method call.

Detecting duplicates during refactoring

PhpStorm can also find smaller duplicates in the current scope (method, class, or file) during refactoring. In v2021.2, this detection process has become a lot smarter.

Select a code fragment and then call the refactoring with ⌘⌥M (Cmd+Alt+M / Ctrl+Alt+M). At the bottom of the Extract Method dialog, a Review and replace duplicates checkbox will appear if any are found.

Note that this detection will now find duplicates when the code is not identical.

New smart selector for code fragments

If you try to invoke the refactoring without selecting a piece of code, PhpStorm 2021.2 will suggest a list of possible code fragments. The suggestions can include simple expressions, statements, and full blocks.

PhpStorm verifies the control flow for the selected elements and excludes invalid or meaningless selections. For example, see the following code where assignment expression is skipped, and you can choose to extract the right part of it.

Expand selection improved

You probably already know that you can select a piece of code with the Expand / Shrink selection action ⌥+Up / ⌥+Down (Ctrl+W / Ctrl+Shift+W). This is a fast way to select code for refactoring.

In PhpStorm 2021.2, this will receive a small but useful update – the ability to select blocks without curly braces.

Better naming for extracted methods

PhpStorm 2021.2 is attempting to solve one of the hardest problems in Computer Science: naming things. :)

When you use the Extract Method refactoring, PhpStorm will suggest a name for the new method.

If there is a variable used in the extracted code, the method name will be based on that variable’s name. PhpStorm will also take into account the expression type, the name from a method call, and other factors when suggesting a name.

If the code block is annotated with a comment, PhpStorm will use this comment as a name source for the extracted method.

Would you like to learn more about refactorings in PhpStorm? Subscribe to the JetBrains YouTube channel as we are going to publish a free course on refactorings by Matthew Setter soon!


The full list of changes in the EAP #2 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 and don’t require a license. But they expire 30 days after the build date, and you’ll have to download a new build.

Download PhpStorm 2021.2 EAP

Please let us know about any problems you find by reporting them to our issue tracker or by commenting on this post.

Your JetBrains PhpStorm team
The Drive to Develop

image description