Use null coalescing operator ?? instead of a ternary operator with an isset() condition

Created on 11 August 2019, over 5 years ago
Updated 22 May 2024, 11 months ago

Problem/Motivation

Since Drupal 8.8 doesn't support PHP 5 we can use null coalescing operator (??) instead of ternary operator with isset statement.

Benefits

The change will make code more laconic and easy to read. For example, this:

return isset($this->handlers['route_provider'][$entity_type_id]) ? $this->handlers['route_provider'][$entity_type_id] : [];

becomes:

return $this->handlers['route_provider'][$entity_type_id] ?? [];

Three supporters required

  1. https://www.drupal.org/u/jonathan1055 β†’ (8 Nov 2023)
  2. https://www.drupal.org/u/bbrala β†’ (8-11)
  3. https://www.drupal.org/u/larowlan β†’ (9 Nov 2023)

Proposed changes

There is currently no mention of null-coalescing operators in the standards.

1. Operators section of the Coding Standards β†’

None

The null coalescing operator ?? should be used instead of a ternary operator with an isset() condition, to make code more readable. For example use
$result = $values['entry'] ?? 'default'
instead of
$result = isset($values['entry']) ? $values['entry'] : 'default'

Remaining tasks

  1. https://www.drupal.org/node/3400475 β†’
  2. Coding Standards Committee takes action as required. Announced on 2023-11-07 and 2024-01-02
  3. N/A: Core is affected.
  4. Documentation updates
    1. Publish change record
    2. Remove 'Needs documentation edits' tag
  5. πŸ“Œ Enforce null coalescing operator ?? instead of a ternary operator through phpcs Fixed

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

πŸ“Œ Task
Status

Fixed

Component

Coding Standards

Created by

πŸ‡ΊπŸ‡¦Ukraine init90 Ukraine

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    What exactly is the actionable proposed resolution here? is it to make an issue in core to enable <rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" ?

    Has anyone done a documentation search to see if anything needs to change?

  • πŸ‡³πŸ‡ΏNew Zealand quietone
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    What exactly is the actionable proposed resolution here? is it to make an issue in core to enable SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator?

    Yes that is it. Reading comments #5-9 it suggests that two years ago Core was fixed to use the ?? operator. But we need a core issue to add the sniff. That issue will also be used to re-check if anything has regressed.

    https://www.drupal.org/docs/develop/standards/php/php-coding-standards#o... β†’ is probably the place where this new standard should be stated.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Updated the IS for an announcement.

  • Status changed to Needs review over 1 year ago
  • πŸ‡³πŸ‡ΏNew Zealand quietone
  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Fixed IS XML visibilty.

    This also seems fine for announcement like this.

  • πŸ‡§πŸ‡ͺBelgium borisson_ Mechelen, πŸ‡§πŸ‡ͺ

    I agree, adding a cs rule for this is a good idea and the rules already exist as well. +1

  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    @borisson_ could you update the issue summary to follow the new Coding Standard template? To get the new template, probably the easiest way is to create a new issue, copy/paste the template then discard that issue. Put yourself as a supporter and then fill in as much as you can of the rest of it. Setting to 'needs work' for this.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    The Coding Standards Committee has developed a custom issue template β†’ to assist in managing issues efficiently. We have already found that it's use is helping. For issues created before the template was in use I am adding the new template to the Issue Summary and asking everyone here to help convert to it..

    Thank you for your help!

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Made some updates to fill in the new template. Other supporters are required, please add your own (I don't think it is right for anyone to add someone else's name)

    The proposed text is currently very short. Do we need an example?

    Does the standard / sniff cover other null-coalescing operators, such as assignment using ??=

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Added my name to support. Thanks for updating the issue summary @jonathan1055 πŸ’™

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    I have created a change record but I think it needs more detail. Do we just state the new coding standard text, as shown in the issue summary here?

  • Status changed to RTBC over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    The CR looks good to me, I think this is now back for another consideration before an announcement

  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    @larowlan I think you must have submitted with stale form data or something, as your updated in #28 has reverted all the changes we made since #23 to bring in the new template and proposed text.

  • Status changed to Needs review over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Hmm, I thought all I did was mark 'Add a change record' as done.

    I reverted to the previous version and tried again

    Apologies

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Thanks for reverting, and getting us back on track.

    I have just two questions (from #24) to resolve before this goes to the Coding Standards meeting for review

    1. Do we also want to include the null coalese equal operator ??= in this standard? There is a separate phpcodesniffer rule for it, so we can include both in the new standard, and the implementation of the sniffs to enforce can be independent.
    2. Does the proposed text need an example?
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Do we also want to include the null coalese equal operator ??= in this standard? There is a separate phpcodesniffer rule for it, so we can include both in the new standard, and the implementation of the sniffs to enforce can be independent.

    I think we should do that in a separate issue to keep the scope tighter and increase the chance of this progressing.

    Does the proposed text need an example?

    Added one

  • Status changed to RTBC over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Agreed, ??= can be a separate issue.

    Thanks for adding the example. I have updated the change request β†’ with the same exampe.

    I think this is RTBC for the next Standards Meeting. But if anyone sees a problem, feel free to revert to 'needs work' and explain what's missing.

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    The announcement has been published
    https://www.drupal.org/about/core/blog/coding-standards-proposals-for-fi... β†’
    so removing the "Needs announcement for final discussion" tag.
    Now at step 5

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I am late to reviewing this. The proposed text states "Null-coalescing operators such as ??" but "??" is the null coalescing operator. So, it should be

    The null coalescing operator ?? should be used to make code more readable. For example use $value = $values['entry'] ?? ''; instead of $value = isset($values['entry']) ? $values['entry'] : '';

    and I am thinking further to

    The null coalescing operator ?? should be used instead of the ternary operator. For example use $value = $values['entry'] ?? ';' instead of $value = isset($values['entry']) ? $values['entry'] : '';

    But the second idea may be a step to far at this stage.

  • πŸ‡³πŸ‡±Netherlands arkener

    Agree with #35, the current description might indicate that the null coalescing assignment operator is included in this coding standard. Scoping this in the description to ?? would clarify this.

    I would also clarify the title a bit, the current title has some ambiguity in regards to what we 're replacing.

    Use Null-Coalescing Operator ?? to replace isset with ternary operator

    with something like

    Use Null-Coalescing Operator ?? instead of a ternary operator with an isset statement

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    #35 and #36 are both excellent improvements. +1 for updating the title and IS with that.

    Also the example, it would be easier to read if we changed the name of $value, maybe to $result or anything other than $value. Then we don't have the extra cognative overload of dealing with $value and $values. The examples must be really clear and simple.

  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    I agree with #35 and #37. (#36 seems like it has been addressed already?) So maybe something like this:

    The null coalescing operator ?? should be used instead of a ternary operator with an isset() condition. For example, use $result = $values['entry'] ?? ';' instead of $result = isset($values['entry']) ? $values['entry'] : '';.

  • πŸ‡¬πŸ‡§United Kingdom jonathan1055

    Updated IS with the agreed changes in #36-39

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Formatting of the example

  • πŸ‡³πŸ‡ΏNew Zealand quietone
  • πŸ‡ΊπŸ‡ΈUnited States dww

    This is now formally pending review by core committers. Tagging as such.

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    As per meeting 26-3-2024 by larowlan, this has been approved.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    The was brought up as the core committer meeting on 5 Mar. There was only positive feedback.

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    I don't have permissions right now to do the doc updates. We are working on that.

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Documentation is updated. Think this is all done :)

    I'm not completely sure on the credit rules for this project. So keeping rtbc unril credits are fixed.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I reviewed the documentation updates and they agree with the Issue Summary.
    The core issue exists, πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work

    So, it is just the publishing of the change record that needs to be done.

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Fixed credits

  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Think you misreferenced @quietone. I didnt find one specifically for this change. So made πŸ“Œ Enforce null coalescing operator ?? instead of a ternary operator through phpcs Fixed .

  • Status changed to Fixed 12 months ago
  • πŸ‡³πŸ‡±Netherlands bbrala Netherlands

    Published the CR, since all is done i'm going ahead and set this to fixed :) Thanks everyone.

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

Production build 0.71.5 2024