Require short ternary (Elvis operator) syntax

Created on 20 February 2024, 7 months ago
Updated 23 July 2024, about 2 months ago

Problem/Motivation

Since PHP 5.3 we can use short ternary (also known as "Elvis operator") syntax.

Benefits

Less repetition

Before:
$this->entityKeys['bundle'] = $bundle ? $bundle : $this->entityTypeId;

After:
$this->entityKeys['bundle'] = $bundle ?: $this->entityTypeId;

Three supporters required

  1. https://www.drupal.org/u/acbramley (2024-02-29)
  2. https://www.drupal.org/u/alexskrypnyk (2024-02-29)
  3. https://www.drupal.org/u/drunken-monkey (2024-03-02)

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

All binary operators (operators that come between two values), such as +, -, =, !=, ==, >, etc. should have a space before and after the operator, for readability. For example, an assignment should be formatted as $foo = $bar; rather than $foo=$bar;. Unary operators (operators that operate on only one value), such as ++, should not have a space between the operator and the variable or number they are operating on.

Checks for weak-typed inequality MUST use the != operator. The <> operator MUST NOT be used in PHP code.

All binary operators (operators that come between two values), such as +, -, =, !=, ==, >, etc. should have a space before and after the operator, for readability. For example, an assignment should be formatted as $foo = $bar; rather than $foo=$bar;. Unary operators (operators that operate on only one value), such as ++, should not have a space between the operator and the variable or number they are operating on.

The "short ternary" operator ?: must be used where the first operand of a ternary expression matches the condition. For example $result = $condition ? $condition : 'default'; should be written as $result = $condition ?: 'default'; since $condition matches both the condition and the first operand.

Checks for weak-typed inequality MUST use the != operator. The <> operator MUST NOT be used in PHP code.

2. Repeat the above for each page or sub-page that needs to be changed.

Remaining tasks

  1. Review by the Coding Standards Committee
  2. Coding Standards Committee takes action as required
  3. Discussed by the Core Committer Committee, if it impacts Drupal Core
  4. Final review by Coding Standards Committee
  5. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  6. If applicable, create follow-up issues for PHPCS rules/sniffs changes: 📌 Require short ternary (Elvis operator) syntax Postponed

For a full explanation of these steps see the Coding Standards project page

📌 Task
Status

RTBC

Component

Coding Standards

Created by

🇦🇺Australia mstrelan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024