Refactor your code to functional style with PhpStorm 2018.1

Posted on by Kirill Smelov

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

Mapping values of an array:

$result = [];

foreach ($items as $key => $item) {
   $result[$key] = func($item);

$result = [];

foreach ($items as $key => $item) {
   if (condition($item)) {
       $result[$key] = $item;

Or reducing an array to some value:
$acc = 0;

foreach ($numbers as $number) {
   $acc += $number;

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

Comments below can no longer be edited.

10 Responses to Refactor your code to functional style with PhpStorm 2018.1

  1. Matthew Hailwood says:

    January 26, 2018

    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!

  2. kyriakos says:

    January 27, 2018

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

    • GodLesZ Blubb says:

      January 27, 2018

      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 says:

      February 22, 2018

      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.

  3. Константин says:

    January 29, 2018

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

  4. Егор Иванов says:

    March 30, 2018

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

    • Dmitry Tronin says:

      March 30, 2018

      Please share a code sample you’re trying this on

  5. Al Bunch says:

    May 31, 2018

    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.


Subscribe for updates