Search code examples
phpstorm

PhpStorm: manually force hard wrapping on specific chained methods or an array definition


I'm looking for a shortcut in PhpStorm to turn this:

$object->method()->method()->method();

into this:

$object->method()
       ->method()
       ->method();

and this:

$array = [1,2,3,4,5];

into this:

$array = [
    1,
    2,
    3,
    4,
    5,
];

Basically, I'm looking for a shortcut for the formatter to treat the current line as if it was over the "Hard wrap at" limit. It seems like such a shortcut should exist, but I can't find it. I don't want to turn wrapping "always" on, I'm looking to arbitrarily decide which should wrap.


Solution

  • I'm looking for a shortcut in PhpStorm to turn this:

    $object->method()->method()->method();
    

    into this:

    $object->method()
           ->method()
           ->method();
    

    There is no dedicated action for this. At very least I'm not aware of any.

    You may only configure Code Style for PHP to place 2nd+ chainable call on a separate line.

    • Settings/Preferences
    • Editor | Code Style | PHP
    • Wrapping and Braces | Chained method calls

    enter image description here


    and this:

    $array = [1,2,3,4,5];
    

    into this:

    $array = [
        1,
        2,
        3,
        4,
        5,
    ];
    

    There is an intention to switch from one to another (and other way around). Will work for arrays as well as function/method call parameters.

    It's accessible from Intentions/Quick Fix menu (Alt + Enter or click on the light bulb).

    enter image description here

    enter image description here

    P.S. Since 2022.1 or so version you can assign a shortcut to the actual Intention. Can be done right there from that menu:

    enter image description here