Refactor your code to functional style with PhpStorm 2018.1

How many times you wrote code like this to achieve a simple purpose?

Mapping values of an array:


Or reducing an array to some value:

The above code samples involve the imperative programming style, which is familiar to everybody. It has a certain flaw: we have to parse the code in our head and go through negligible but required details to understand it.

To solve this issue, PHP suggests relying on the built-in functions: array_map, array_filter, array_reduce, etc. These functions can improve readability and emphasize the intent of your code. Unfortunately, it’s not always clear when and how to apply them to the existing code, since in some cases details can be crucial.

To help you with the task, we are going to introduce automatic ways of transforming imperative code to functional equivalents via the corresponding inspections and quick fixes. Since there can be a lot of tricky cases and possible transformations, we’ve decided to start with the simplest case of the foreach to array_map transformation inspection and quick fix.

In PhpStorm 2018.1 EAP, you can find the new Loop can be converted to ‘array_map’ call inspection under Settings | Editor | Inspections | PHP | Code Style and apply the corresponding quick fix as usual, by pressing Alt + Enter.


Should you reconsider, the ‘array_map’ call can be converted to loop inspection and quick fix, which do exactly the opposite, are provided, too.

We’re going to add more transformations and cover more cases in the future.

What do you think about functional programming in PHP? What types of transformation do you find the most useful? Give us your opinions in the comments or tweet @phpstorm!

Your JetBrains PhpStorm Team
The Drive to Develop

About Kirill Smelov

PhpStorm developer
This entry was posted in Cool Feature and tagged , . Bookmark the permalink.
  • Matthew Hailwood

    This is a great feature, I’m often forgetting the name of one of the array functions even though I find them far easier to understand, so a quick intention to switch between the imperative and functional versions is great!

  • kyriakos

    I understand the readability point but aren’t these functions much slower?

    • GodLesZ Blubb

      See for results. 2 Posts above ( you’ll find the testsuite.
      As you can see, the difference on foreach and array_map are only noticable with enabled xdebug.

      But they are slower indeed.

    • David Harkness

      Unless you’re working with million-element arrays or running your code on a satellite, you’re not going to notice. Developer time is worth more than processor cycles, and you can always increase the latter.

  • Константин

    For what wrap with closure? Why not just array_map('func', $items) ?

  • Егор Иванов

    Why I can’t see “Convert loop to…” when pressing alt+enter?

    • Dmitry Tronin

      Please share a code sample you’re trying this on

  • Al Bunch

    There is a performance impact as cited here

    While developer time may be worth more than processor cycles, user time is worth more than developer time and processor cycles combined. Benchmark your code with both, choose the one that makes sense, and comment your code accordingly. A well written/placed comment will save far more developer time than just using an aptly named function.