PhpStorm Catch sentence with multiple exception classes PSR-12 code style

41 Views Asked by At

I am using PhpStorm 2023.3.4.

I set the code standards for my project to PSR12. This means 4 space indentation, no tab char.

I have a PSR-12 compliant chunk of code like this:

<?php
class MyClass {
    public function myMethod(): void 
    {
        try {
            ...
        } catch (
            Exception01 |
            Exception02 |
            ...
            ExceptionNN $e
        ) {
            // Do something about it
        }
    }
}

When I hit the auto-formatting, the IDE reformats that section as follows:

<?php
class MyClass {
    public function myMethod(): void 
    {
        try {
            ...
        } catch (
        Exception01 |
        Exception02 |
        ...
        ExceptionNN $e
        ) {
            // Do something about it
        }
    }
}

Not only that, but if manually re-indent the exception classes, the IDE would complain about "incorrect whitespaces".

Funny thing, PHP-CS-Fixer will format the code as I want, but that only happens when I launch the fixer in the command line to reformat the entire project. I'd like to have it correctly configured in the IDE too but I am not able to find out which is the correct code style configuration.

Edit: my code style XML exported configuration:

<code_scheme name="Project" version="173">
  <PHPCodeStyleSettings>
    <option name="BLANK_LINES_BETWEEN_IMPORTS" value="1" />
    <option name="THROWS_WEIGHT" value="0" />
    <option name="PARAM_WEIGHT" value="28" />
    <option name="RETURN_WEIGHT" value="28" />
    <option name="LOWER_CASE_BOOLEAN_CONST" value="true" />
    <option name="LOWER_CASE_NULL_CONST" value="true" />
    <option name="ELSE_IF_STYLE" value="COMBINE" />
    <option name="KEEP_RPAREN_AND_LBRACE_ON_ONE_LINE" value="true" />
    <option name="BLANK_LINES_AFTER_OPENING_TAG" value="1" />
    <option name="KEEP_BLANK_LINES_AFTER_LBRACE" value="0" />
    <option name="NEW_LINE_AFTER_PHP_OPENING_TAG" value="true" />
    <option name="SPACES_AROUND_PIPE_IN_UNION_TYPE" value="true" />
  </PHPCodeStyleSettings>
  <codeStyleSettings language="PHP">
    <option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="0" />
    <option name="BLANK_LINES_AFTER_PACKAGE" value="1" />
    <option name="ALIGN_MULTILINE_PARAMETERS" value="false" />
    <option name="ALIGN_MULTILINE_BINARY_OPERATION" value="true" />
    <option name="ALIGN_MULTILINE_EXTENDS_LIST" value="true" />
    <option name="CALL_PARAMETERS_WRAP" value="5" />
    <option name="CALL_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" />
    <option name="CALL_PARAMETERS_RPAREN_ON_NEXT_LINE" value="true" />
    <option name="METHOD_PARAMETERS_WRAP" value="5" />
    <option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" />
    <option name="METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE" value="true" />
    <option name="EXTENDS_LIST_WRAP" value="5" />
    <option name="METHOD_CALL_CHAIN_WRAP" value="5" />
    <option name="PARENTHESES_EXPRESSION_LPAREN_WRAP" value="true" />
    <option name="PARENTHESES_EXPRESSION_RPAREN_WRAP" value="true" />
    <option name="BINARY_OPERATION_WRAP" value="5" />
    <option name="FOR_STATEMENT_LPAREN_ON_NEXT_LINE" value="true" />
    <option name="FOR_STATEMENT_RPAREN_ON_NEXT_LINE" value="true" />
    <option name="ARRAY_INITIALIZER_WRAP" value="5" />
    <option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" />
    <option name="ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE" value="true" />
    <option name="IF_BRACE_FORCE" value="3" />
    <option name="DOWHILE_BRACE_FORCE" value="3" />
    <option name="WHILE_BRACE_FORCE" value="3" />
    <option name="FOR_BRACE_FORCE" value="3" />
  </codeStyleSettings>
</code_scheme>
1

There are 1 best solutions below

2
On

As I understand after my experiments: there is no option for that ATM.

https://youtrack.jetbrains.com/issue/WI-61677 -- watch this ticket (star/vote/comment) to get notified with any progress (and hopefully increase the priority / have it implemented sooner).


Funny thing, PHP-CS-Fixer will format the code as I want, but that only happens when I launch the fixer in the command line to reformat the entire project.

In the next 2024.1 version (currently in EAP stage) you should be able to completely disable the built-in formatter and use the external formatter only.

This could be used as a workaround. Try the latest EAP version and see if it works for you: https://www.jetbrains.com/phpstorm/nextversion/