11

I have some questions regards to phpstorm code reformat.

I have long line and single line.

$this->getSelect()->join('some_code_here')->join('some_code_here')->join('some_code_here')->join('some_code_here')->join('some_code_here');
$this->getSelect()->join('some_code_here')->join('some_code_here');

I want to configure setting:

Code style / PHP / Wrapping and Braces / Chained method calls

This setting has 4 variants:

Do not wrap (1)
Wrap if long (2)
Crop down if long (3)
Wrap always (4)

When I choose 2 or 3 I have following:

    $this->getSelect()->join('some_code_here')->join('some_code_here')->join('some_code_here')->join(
        'some_code_here'
    )->join('some_code_here');
    $this->getSelect()->join('some_code_here')->join('some_code_here');

When I choose 4th, I have:

    $this->getSelect()
        ->join('some_code_here')
        ->join('some_code_here')
        ->join('some_code_here')
        ->join('some_code_here')
        ->join('some_code_here');
    $this->getSelect()
        ->join('some_code_here')
        ->join('some_code_here');

My question is:

Is there any possibility wrap every call from new line, only if method is very long (more than 120 symbols).

Expected result:

    $this->getSelect()
        ->join('some_code_here')
        ->join('some_code_here')
        ->join('some_code_here')
        ->join('some_code_here')
        ->join('some_code_here');
    $this->getSelect()->join('some_code_here')->join('some_code_here');

1 Answer 1

4

To get the desired auto-formatting use the following settings:

  1. Editor > Code Style - Right margin (columns) - 120 [screenshot]
  2. Editor > Code Style > PHP > Wrapping and Braces (tab) - Chained method calls - Chop down if long [screenshot]

Note: To get the desired auto-formatting like this:

$this->getSelect()
    ->join('some_code_here')
    ->join('some_code_here')
    ->join('some_code_here')
    ->join('some_code_here')
    ->join('some_code_here');
$this->getSelect()->join('some_code_here')->join('some_code_here');

you should start with a chained method calls longer than your right margin (i.e. 120 in your example):

$this->getSelect()->join('some_code_here')->join('some_code_here')->join('some_code_here')->join('some_code_here')->join('some_code_here');
$this->getSelect()->join('some_code_here')->join('some_code_here');

If you auto-format with a chained method calls with length less than 120 columns the rule will not trigger i.e. this

$this->getSelect()
    ->join('some_code_here')->join('some_code_here')->join('some_code_here')
    ->join('some_code_here')->join('some_code_here');
$this->getSelect()->join('some_code_here')->join('some_code_here');

will not trigger the auto-formatting rule since the chained method calls does not exceed 120 columns

3
  • Thank you for your response, but if we have long chain - each function must start from new row.
    – zhartaunik
    Oct 10, 2017 at 13:29
  • Hi @zhartaunik. The configuration from my answer will auto-format the code exactly as described in "Expected result" section of the question. With your comment you are confusing me about the expected end result from the autoformatting. Maybe you should give this configuration a try.
    – svet
    Oct 13, 2017 at 12:59
  • If we use your approach after reformat code we will receive following: s015.radikal.ru/i332/1710/78/5ff9482868a8.png
    – zhartaunik
    Oct 13, 2017 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.