Search code examples
phpphpstorm

Refactor ternary to if-block programatically


Is there a way through the context menus to refactor a ternary assignment into one that is done with an if-else block?

So, for example you'd have something like this:

$a = ($b > -32)? "up" : "down";

You'd then apply this transformation, and PHPStorm would magically change it into:

if ($b > -32) {
    $a = "up";
} else {
    $a = "down";
}

Seems like a really common and easily automated operation, so it must be automated somewhere in this labyrinth of menus.


Solution

  • PHPStorm 8 can do it with Alt-Enter shortcut. Put your cursor at the ? operator and key in Alt+Enter.