Problem/Motivation
In the coding standard, it is not explained whether a semicolon can be used after the "case" instruction. Technically, it is possible to use both a colon and a semicolon (case 1: or case 1;).
Look at the code below from the "field" module. Technically everything is correct.
// Extract the actual value against which the condition is checked.
switch ($key) {
case 'field_name':
$checked_value = $field_storage->getName();
break;
case 'field_id':
case 'field_storage_uuid':
$checked_value = $field_storage->uuid();
break;
case 'uuid';
$checked_value = $field->uuid();
break;
case 'deleted';
$checked_value = $field->isDeleted();
break;
default:
$checked_value = $field->get($key);
break;
}
However, many programmers who see something like this will wonder if this form is correct.
Benefits
If we adopted this change, the Drupal Project will benefit by having consistency and clarity of code.
Three supporters required
- krzysztof domański
- pfrenssen
- drunken monkey
- andypost
- nicxvan
Proposed changes
Provide all proposed changes to the Drupal Coding standards. Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:
1. https://www.drupal.org/docs/develop/standards/php/php-coding-standards#c...
Current text
For switch statements:
existing example
Proposed text
For switch statements, always use a colon after the case condition. There must be no space between the condition and the colon. For example:
no change to the example
Remaining tasks
Create this issue in the Coding Standards queue, using the defined templateAdd supportersCreate a Change RecordReview by the Coding Standards CommitteeCoding Standards Committee takes action as requiredDiscussed by the Core Committer Committee, if it impacts Drupal CoreFinal review by Coding Standards Committee</li>- Documentation updates
Edit all pagesPublish change recordRemove 'Needs documentation edits' tag
If applicable, create follow-up issues for PHPCS rules/sniffs changes
For a full explanation of these steps see the Coding Standards project page
Comments
Comment #2
krzysztof domańskiComment #3
krzysztof domańskiComment #4
krzysztof domańskiComment #5
pfrenssenThanks for creating the issue. This is indeed something that is missing from the current standards. The title of the issue is currently in conflict with the issue summary: the title proposes to use a semicolon after the case statement, while the summary proposes to use a colon. I am assuming you mean to use the colon, so I will update the title accordingly.
I wasn't even aware that using a semicolon is legal for a case statement, but this syntax is indeed shown as the very last example on the official documentation page: PHP: switch.
This gets a +1 from me since this is already the de-facto standard in core and contrib. I have not counted the number of instances, but from experience I can say that I have never seen the semicolon being used in the wild so far.
Comment #6
drunken monkeyThanks for opening the issue!
Same here. Therefore, big +1! A semicolon would just confuse 90% of people reading the code.
I propose to phrase it like this, though – I think it sounds clearer:
Comment #7
nicxvan commentedI also support this!
Comment #8
andypost+1 and needs discuss a sniffer rule to maintain consistency
Comment #9
nicxvan commentedI think with three supporters we can RTBC this.
I updated the IS with the new language.
Comment #10
nicxvan commentedComment #11
nicxvan commentedComment #12
quietone commentedConverting to the new template so we can track progress.
Comment #13
quietone commentedI'd rather not add another sentence and I don't think we need to explain that PHP allows a semicolon.
Comment #14
quietone commentedComment #15
quietone commentedComment #16
quietone commentedChanging status to discuss proposed text in #15
Comment #17
theodorejb commentedI agree that it's not necessary to explain that PHP allows a semicolon. I would suggest using the proposed text in #13, or else don't change the text at all and just add an automated coding style rule to enforce the case syntax shown in the example.
Comment #18
nicxvan commentedI agree with your suggestion too. I think adding that line will just confuse people.
Comment #19
jonathan1055 commentedThe new text was actually in the wrong place in the suggestion. It should refer to the switch statement, not the if. I have updated the proposed text in the issue summary, also noting that there should be no space between the condition and the colon. This is already checked for by Coder.
Comment #20
quietone commentedChanging status so it will be reviewed by the committee.
Comment #21
borisson_The change record looks good, as well as the proposed change to the text.
Comment #22
quietone commentedThis was discussed at the Coding Standards Meeting Tuesday 2025-02-26 0900 UTC and there were no objections.
Comment #23
mondrakeLooks like PHP is going to deprecate semicolons soon: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_semicolon_after_...
So a Drupal standard may become rather irrelevant.
Comment #24
quietone commentedThis was included in the last core committer meeting and there were no objections to this change.
Comment #25
quietone commentedForgot to add that this was in the meeting 26 March and there were no objections.
Comment #26
quietone commentedCreated an issue in coder, #3518348: Add sniff to require a colon after a case instruction
Comment #27
quietone commentedComment #28
jonathan1055 commentedRegarding #25
Does this mean that step 7 'Final review by Coding Standards Committee' is done?
Regarding #26 it turns out that there was already a pre-exitsing Coder issue for this - #3487111: Enforce case statements ending in a : instead of a ;
The work was completed on 31 December and was included in Coder 8.3.27 released on 8 Jan 2025.
Comment #29
quietone commentedI updated the documentation as given in the issue summary. And made a core issue to enforce it. #3523118: Enable Drupal.ControlStructures.CaseSemicolon. I have updated credit.
So, the edits should be checked and the change record published.
Comment #30
jonathan1055 commentedIt looks correct to me, exactly as in the issue summary.
For the Change Record, I think it would be worth adding that this was introduced in Coder 8.3.27 which was released on 8 Jan 2025
Comment #31
quietone commented@jonathan1055, thanks for the review.
I updated the CR and published this.
I think this is complete now so changing status.
Thanks all!