slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.39k stars 171 forks source link

JumpStatementsSpacing in `switch` blocks. #1610

Open mikebronner opened 1 year ago

mikebronner commented 1 year ago

I have the following rule definition:

    <rule ref="SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing">
        <properties>
            <property name="allowSingleLineYieldStacking" type="boolean" value="false" />
            <property name="linesCountBefore" type="integer" value="1" />
            <property name="linesCountBeforeFirst" type="integer" value="0" />
            <property name="linesCountBeforeWhenFirstInCaseOrDefault" type="integer" value="0" />
            <property name="linesCountAfter" type="integer" value="1" />
            <property name="linesCountAfterLast" type="integer" value="0" />
            <property name="linesCountAfterWhenLastInCaseOrDefault" type="integer" value="1" />
            <property name="linesCountAfterWhenLastInLastCaseOrDefault" type="integer" value="0" />
        </properties>
    </rule>

My ideal switch statement is formatted as follows (the logic is nonsense, of course):

        switch ($test) {
            case "test":
                $test = "test";
                break;

            case "test":
                $test = "test";
                break;

            case "test2":
                break;
        }

However, when I lint this, I get the following errors on the first two break statements:

Expected 1 line before "break", found 0.
(SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing.IncorrectLinesCountBeforeControlStructure)phpcs

This appears to be due to the setting:

            <property name="linesCountBefore" type="integer" value="1" />

However, that setting is needed, so that I can enforce an empty line before return, continue, and so forth, outside of switch statements. Am I configuring this incorrectly?

Possibly related, but haven't proven useful solutions (that I could find):

mikebronner commented 1 year ago

Yes, I now have the following errors for the first two breaks:

Expected 1 line before "break", found 0.
(SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing.IncorrectLinesCountBeforeControlStructure)phpcs
Expected 0 lines after "break", found 1.
(SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing.IncorrectLinesCountAfterLastControlStructure)phpcs
kukulich commented 1 year ago

I'm sorry I've read it bad that's why I've removed my comment.

The error is expected and right. There's no settings to force behaviour you want. There should be one empty line before break.

mikebronner commented 1 year ago

Thank you for confirming. I will exclude switch from JumpStatementSpacing for now, and try to write my own. I was hoping that linesCountBeforeWhenFirstInCaseOrDefault would take precedence over linesCountBefore.

kukulich commented 1 year ago

I was hoping that linesCountBeforeWhenFirstInCaseOrDefault would take precedence over linesCountBefore.

It has precedence but break is not first statement in your case.