Change !isset to the null coalescing assignment operator ??=

Created on 26 March 2024, 9 months ago
Updated 14 August 2024, 4 months ago

Problem/Motivation

To cover πŸ“Œ Use null coalescing assignment operator Active

Benefits

If we adopted this change, the Drupal Project would benefit by a shorter code, more laconic and easy to read.

Three supporters required

  1. https://www.drupal.org/u/solideogloria β†’ (2024-08-14)
  2. https://www.drupal.org/u/ β†’ {userid} (date that user added support)
  3. https://www.drupal.org/u/ β†’ {userid} (date that user added support)

Proposed changes

There is currently no mention of null coalescing assignment operator ??= in the standards. The proposal is to add a sub section "Null coalescing assignment operator ??=" to the operator section β†’ , with the two parts,

1. Operators section of the Coding Standards β†’

None

The null coalescing assignment operator ??= should be used instead of a null coalescing operator ??, to make code more readable. For example use
$elements['#attached'] ??= [];
instead of
$elements['#attached'] = $elements['#attached'] ?? [];

2. Operators section of the Coding Standards β†’

None

The null coalescing assignment operator ??= should be used instead of a if (!isset()) condition, to make code shorter and more readable. For example use
$variables['content'][$name]['#attributes'] ??= [];
instead of

if (!isset($variables['content'][$name]['#attributes'])) {
  $variables['content'][$name]['#attributes'] = [];
}

Remaining tasks

  1. Add supporters
  2. Create a Change Record
  3. Review by the Coding Standards Committee
  4. Coding Standards Committee takes action as required
  5. Discussed by the Core Committer Committee, if it impacts Drupal Core
  6. Final review by Coding Standards Committee
  7. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  8. If applicable, create follow-up issues for PHPCS rules/sniffs changes

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

πŸ“Œ Task
Status

Active

Component

Coding Standards

Created by

πŸ‡ΊπŸ‡ΈUnited States smustgrave

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

Comments & Activities

Production build 0.71.5 2024