per-coding-style icon indicating copy to clipboard operation
per-coding-style copied to clipboard

switch case and default must not have semicolons

Open kenguest opened this issue 2 months ago • 9 comments

Update section 5.2 advising that semi-colons must not be used in switch statement.

Resolves https://github.com/php-fig/per-coding-style/issues/128

kenguest avatar Nov 19 '25 21:11 kenguest

I don't think this is needed, since the example already uses colons, and the semicolon syntax is now deprecated in PHP itself.

theodorejb avatar Nov 20 '25 15:11 theodorejb

For some people, implicit/implied requirements as shown in sample code is not as clear-cut compared to when outlined in the prose of the requirements document, so this is still very much needed.

kenguest avatar Nov 23 '25 09:11 kenguest

I've changed the wording to below:

The caseanddefault keywords MUST use colons as shown in the sample code below.

kenguest avatar Nov 26 '25 20:11 kenguest

The description should probably mention that this will close #128.

theodorejb avatar Nov 26 '25 23:11 theodorejb

The description should probably mention that this will close #128.

I've updated the description accordingly.

kenguest avatar Nov 29 '25 00:11 kenguest

(Adjusted reference in the summary to match GitHub automation expectations.)

Crell avatar Dec 01 '25 20:12 Crell

@jrfnl Would this language be good enough for you? It's a bit wiggly in how it specifies things, but maybe that's OK in this case?

Crell avatar Dec 01 '25 20:12 Crell

@jrfnl Would this language be good enough for you? It's a bit wiggly in how it specifies things, but maybe that's OK in this case?

I still think it's kind of redundant, but if this is being made explicit, than the text should be leading. The reference to the sample code like this, would be the only one of its kind in the whole document, so to me, it feels like it doesn't belong.

If anything, the rule as described now, still leaves room for code like this - take note of the redundant curly braces -, which IIRC is discouraged:

switch ($foo) {
    case 10 : {
        // Do something.
        break;
    }
}

I wonder if PERCS should be explicit with an opinion about redundant curlies in this context if the use of the colon is being made explicit now anyway ?

jrfnl avatar Dec 01 '25 20:12 jrfnl

(With editor hat on) I'm open to not doing anything here if that ends up being the consensus. Or if the consensus is we should specify something, that's OK too. I don't have a large personal stake here.

I... didn't know extra braces were legal, either. :sweat_smile: I'd be OK with forbidding those if the rest of the WG is.

Crell avatar Dec 01 '25 21:12 Crell