[meta] Change !isset to the null coalescing assignment operator ??=.

Created on 22 February 2024, 10 months ago
Updated 27 March 2024, 9 months ago

Problem/Motivation

For example,

A: in core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php, in function getNormalization,
$base['links'] = $base['links'] ?? $this->serializer->normalize($object->getLinks(), $format, $context)->omitIfEmpty();
could be changed in,
$base['links'] ??= $this->serializer->normalize($object->getLinks(), $format, $context)->omitIfEmpty();

B: in core/modules/layout_discovery/layout_discovery.module, in function template_preprocess_layout,

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

could be changed in,
$variables['content'][$name]['#attributes'] ??= [];

Steps to reproduce

Proposed resolution

From #2 πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work in πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work ,

.... assume there are many similar changes we could make across all of core, and ideally a coding standards sniff we could enable to prevent this code from creeping back in.

Ideally we would change all instances of this across core in one go,

A code sniffing rule exists about this topic,
slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/RequireNullCoalesceEqualOperatorSniff
but is missing in,
drupal/coder/coder_sniffer/Drupal/ruleset.xml
that is,
<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator" />

From #3418494-#31 πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work the corresponding phpcs command for this rule is,
phpcs --standard=SlevomatCodingStandard --sniffs=SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator core

Remaining tasks

A: πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work
B: πŸ“Œ Use null coalescing assignment operator: multi-lines case. Needs work
B1: πŸ“Œ Change !isset to ??= assignment operator: generator and their generated files case. Needs review
B2: πŸ“Œ Use null coalescing assignment operator ??=: phpstan obstruction case. Closed: won't fix

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component
OtherΒ  β†’

Last updated 7 minutes ago

Created by

πŸ‡«πŸ‡·France Chris64 France

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

Comments & Activities

Production build 0.71.5 2024