switch case and default must not have semicolons#129
Conversation
|
I don't think this is needed, since the example already uses colons, and the semicolon syntax is now deprecated in PHP itself. |
|
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. |
|
I've changed the wording to below:
|
|
The description should probably mention that this will close #128. |
I've updated the description accordingly. |
|
(Adjusted reference in the summary to match GitHub automation expectations.) |
|
@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 ? |
|
(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. 😅 I'd be OK with forbidding those if the rest of the WG is. |
|
After sleeping on this for a while, here's my recommendation: I agree with @jrfnl that "do like the example below" is suboptimal wording, regardless of the section. However, the text does that in a few places right now (eg, The text above the example currently reads:
I propose we change that to: That more precisely describes the current ruleset, implicitly forbids the semicolon option, and explicitly forbids wrapping Would that be satisfactory for everyone? |
I suggest slightly different wording for the 'must have a comment' rule: - * If a non-empty case intends to continue into the following case, then a comment `// no break` MUST be included to highlight the deliberate lack of a `break` statement.
+ * If a non-empty case intends to continue into the following case, then a comment MUST be included to highlight the deliberate lack of a `break` statement. For example, `// no break`.It seems that in the original text,
The example 'case 4' does not satisfy this rule as it has a |
|
"break or return" seems fine to me. For the first, yes, the text above would also standardize the comment text. Whether that's good or an overreach, I suppose the WG will reply after the New Year. 😄 |
|
@Crell Thanks for that proposal. In my opinion that would definitely be better than what's there now and I'd also advocate for opening separate issues to address the other instances of "look at the examples and figure out what the rules should be". As for the proposed text, please consider the following feedback:
Think: switch (true) {
case $a === 10:
break;
}Note: I'm not making a case for this type of code, just giving an example of why the phrasing needs to be adjusted. Also note: the phrasing is still ambiguous as it could strictly be interpreted as not allowing any whitespace or comments on that line.... Think: switch (true) {
case (
$a === 10
&& $b === 20
):
break;
}
I support @fredden's observation and don't think standardizing the exact comment wording is a good idea. Quite aside from the fact that the proposed comment does not comply with normalized capitalization and punctuation rules, so will easily conflict with comment related sniffs. If it would be standardized then I'd suggest that a comment like
How about calling it a "terminating statement" instead ? That way you avoid having to list all the possibilities. For the record, the complete list of terminating statements as far as I currently can see it is: |
|
Revised suggestion based on the above comments: Working group, your thoughts? |
|
looks good, just a note on "case condition" I think value was more appropriate, what's after the in the PHP documentation about switch, we can read
and
so "expression" could work too |
|
@kenguest Are you going to update this PR or shall I make a new one? |
|
@Crell I'll update it soon. |
|
trivial-but-not-too-trivial body added to skeleton code example. |
Update section 5.2 advising that semi-colons must not be used in switch statement.
Resolves #128