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

  1. Create this issue in the Coding Standards queue, using the defined template
  2. Add supporters
  3. Create a Change Record
  4. Review by the Coding Standards Committee
  5. Coding Standards Committee takes action as required
  6. Discussed by the Core Committer Committee, if it impacts Drupal Core
  7. Final review by Coding Standards Committee</li>
  8. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  9. 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

Krzysztof Domański created an issue. See original summary.

krzysztof domański’s picture

Issue summary: View changes
krzysztof domański’s picture

Issue summary: View changes
krzysztof domański’s picture

Issue summary: View changes
pfrenssen’s picture

Title: Semicolon after case instruction in switch statements » Require using a colon after case instruction in switch statements

Thanks 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.

drunken monkey’s picture

Issue summary: View changes

Thanks for opening the issue!

I wasn't even aware that using a semicolon is legal for a case statement

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:

Always use a colon after the case instruction (e.g., case 1:). (PHP also allows a semicolon.)

nicxvan’s picture

I also support this!

andypost’s picture

+1 and needs discuss a sniffer rule to maintain consistency

nicxvan’s picture

Issue summary: View changes
Status: Active » Reviewed & tested by the community

I think with three supporters we can RTBC this.

I updated the IS with the new language.

nicxvan’s picture

Issue summary: View changes
nicxvan’s picture

Issue summary: View changes
quietone’s picture

Issue summary: View changes

Converting to the new template so we can track progress.

quietone’s picture

Issue summary: View changes

I'd rather not add another sentence and I don't think we need to explain that PHP allows a semicolon.

Always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added. The opening curly should be on the same line as the opening statement, preceded by one space. The closing curly should be on a line by itself and indented to the same level as the opening statement.

For switch statements always use a colon after the case instruction.

quietone’s picture

Issue summary: View changes
quietone’s picture

Issue summary: View changes
quietone’s picture

Status: Reviewed & tested by the community » Needs review

Changing status to discuss proposed text in #15

theodorejb’s picture

I 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.

nicxvan’s picture

I agree with your suggestion too. I think adding that line will just confuse people.

jonathan1055’s picture

Issue summary: View changes

The 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.

quietone’s picture

Status: Needs review » Reviewed & tested by the community

Changing status so it will be reviewed by the committee.

borisson_’s picture

The change record looks good, as well as the proposed change to the text.

quietone’s picture

Issue summary: View changes

This was discussed at the Coding Standards Meeting Tuesday 2025-02-26 0900 UTC and there were no objections.

mondrake’s picture

Looks 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.

quietone’s picture

Issue summary: View changes

This was included in the last core committer meeting and there were no objections to this change.

quietone’s picture

Forgot to add that this was in the meeting 26 March and there were no objections.

quietone’s picture

quietone’s picture

Issue summary: View changes
jonathan1055’s picture

Regarding #25

this was in the meeting 26 March and there were no objections

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.

quietone’s picture

Issue summary: View changes

I 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.

jonathan1055’s picture

the edits should be checked

It 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

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed

@jonathan1055, thanks for the review.

I updated the CR and published this.

I think this is complete now so changing status.

Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.