coding-standard icon indicating copy to clipboard operation
coding-standard copied to clipboard

JumpStatementsSpacing in `switch` blocks.

Open mikebronner opened this issue 1 year ago • 4 comments

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):

  • https://github.com/slevomat/coding-standard/issues/867
  • https://github.com/slevomat/coding-standard/issues/993
  • https://github.com/slevomat/coding-standard/issues/809

mikebronner avatar Aug 19 '23 15:08 mikebronner

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

mikebronner avatar Aug 19 '23 15:08 mikebronner

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.

kukulich avatar Aug 19 '23 15:08 kukulich

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.

mikebronner avatar Aug 19 '23 15:08 mikebronner

I was hoping that linesCountBeforeWhenFirstInCaseOrDefault would take precedence over linesCountBefore.

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

kukulich avatar Aug 19 '23 15:08 kukulich