Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JumpStatementsSpacing in switch blocks. #1610

Open
mikebronner opened this issue Aug 19, 2023 · 4 comments
Open

JumpStatementsSpacing in switch blocks. #1610

mikebronner opened this issue Aug 19, 2023 · 4 comments

Comments

@mikebronner
Copy link

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

I was hoping that linesCountBeforeWhenFirstInCaseOrDefault would take precedence over linesCountBefore.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants