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

Current text

None

Proposed new paragraph

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. Create this issue in the Coding Standards queue, using the defined template
  2. Add supporters
  3. Create a Change Record https://www.drupal.org/node/3400475
  4. Review by the Coding Standards Committee
  5. Coding Standards Committee takes action as required. Announced on 2023-11-07 and 2024-01-02
  6. N/A: Core is affected. Tagged with 'Needs documentation edits' if Core is not affected
  7. Discussed by the Core Committer Committee, if it impacts Drupal Core
  8. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  9. Create follow-up issue for Core to enable the existing coder sniff SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator #3446107: Enforce null coalescing operator ?? instead of a ternary operator through phpcs

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

Comments

init90 created an issue. See original summary.

cilefen’s picture

Project: Drupal core » Coding Standards
Version: 8.8.x-dev »
Component: other » Coding Standards
Priority: Minor » Normal

+1

Because this style will be unenforceable without a coding standard for it, I am moving this issue to the coding standards queue. slevomat/coding-standard can check for this. It could be added as a dependency to coder.

drunken monkey’s picture

For PHP 7 code, I much prefer this style, too. I’m not quite sure whether we really need a coding standard for it, though. I think most developers already prefer this, so if you are aware that it can be used, they will.
And whether going through all of Core replacing this makes sense is also debatable.

barrett’s picture

I disagree about the need to update core to use ?? rather than ternaries. Developers look to core to show the "right" way things should be done. If the expectation is that they are using null coalescing but core still uses ternaries, that will create confusion.

bbrala’s picture

Core has been updated to use null coalescing operator so this issue is quite relevant again :)

#3222251: [November 8, 2021] Replace all isset constructs with the null coalescing operator

bbrala’s picture

bbrala’s picture

drunken monkey’s picture

Now that Core follows it completely, I’m also in favor of adding a coding standard for it.

bbrala’s picture

Related issue is merged, the dev version of coder now supports a new set of sniffs that supports ?? also.

jonathan1055’s picture

As it stands currently, the Drupal coding standards do not appear to have officially adopted the standard that the null-coalescing operator should be used. However, in contrib land, where the default is to use all sniffs defined and provided by Coder (unlike core, which only uses the specific named sniffs in phpcs.xml.dist) we are seeing coding standards messages for this. If the standard has not been discussed, agreed and adopted yet, then it seems wrong to show the messages by default.

The two slevomat sniffs were added in https://git.drupalcode.org/project/coder/-/commit/929d7d4 - 23 Jan 2022, and first appear in Coder 8.3.14

I notice that there has been no test coverage added to Coder for the null-coalescing operator (is that because the actual sniff code is provided by a third-party?) Anyway, that's another issue, the point here is that the two new sniffs are active and causing coding standards faults when the standard has not been officially agreed and documented on https://www.drupal.org/docs/develop/standards/coding-standards

What's the best way forward to resolve this?

mfb’s picture

@jonathan1055 do you mean that this standard has not been adopted? I've never been involved w/ adopting a drupal coding standard but it looks like the process is for two people to "sponsor" the new standard - if that is still needed I'm happy to do so.

jonathan1055’s picture

Hi mfb, yes I noticed I'd made a rather critical typo in missing out 'not' in my first line above [I have now edited to fix that].

Apologies if you know this already, but I am sure there is a proper process somewhere to discuss and adopt new standards. After Coder implements the sniff, then contrib developers can use and test it (by using the dev version of Coder). Then when a new Coder releases a new version, it can be used by all in contrib, but Core testing is locked to specific Coder versions, which only get updated at times when it is agreed. However, Core 9.4 and 9.5 both user Coder 8.3.15 so this sniff is available for use, and is added to Coder's Drupal standard

<rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" 

When Core adheres to the standard the sniff is added to core phpcs.xml.dist so that it remains clean. This sniff has not yet been added to core phpcs.xml.dist so it is not being checked in core tests, but will be checked in contrib tests if they use the full 'Drupal' standard as defined by Coder.

There is a coding standards channel on slack https://app.slack.com/client/T06GX3JTS/C02LJCF78E8 and I am sure there are places on drupal.org where addoption of new standards is discussed.

mfb’s picture

@jonathan1055 ok, I've not been on drupal slack lately. I was looking at https://www.drupal.org/project/coding_standards which says "At least two additional active community members (contrib module maintainers, active patch contributors, etc) need to agree to sponsor the change in the form of a comment on the issue."

jonathan1055’s picture

Issue tags: +Needs announcement for final discussion

OK, so I think this is the issue where that process happens :-)
Steps 1 and 2 are done (I second your offer to sponsor), so now tagging "needs announcement for final discussion" as per step 3

quietone’s picture

Issue summary: View changes
Status: Active » Needs work

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?

quietone’s picture

jonathan1055’s picture

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.

docs/develop/standards/php/php-coding-standards#operators is probably the place where this new standard should be stated.

quietone’s picture

Issue summary: View changes

Updated the IS for an announcement.

quietone’s picture

Status: Needs work » Needs review
bbrala’s picture

Issue summary: View changes

Fixed IS XML visibilty.

This also seems fine for announcement like this.

borisson_’s picture

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

jonathan1055’s picture

Status: Needs review » Needs work

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

quietone’s picture

Issue summary: View changes
Issue tags: +Needs issue summary update

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!

jonathan1055’s picture

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 ??=

bbrala’s picture

Issue summary: View changes
larowlan’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

Added my name to support. Thanks for updating the issue summary @jonathan1055 💙

jonathan1055’s picture

Issue summary: View changes

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?

larowlan’s picture

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

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

jonathan1055’s picture

Status: Reviewed & tested by the community » Needs work

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

larowlan’s picture

Issue summary: View changes
Status: Needs work » Needs review

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

I reverted to the previous version and tried again

Apologies

jonathan1055’s picture

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?
larowlan’s picture

Issue summary: View changes

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

jonathan1055’s picture

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

Agreed, ??= can be a separate issue.

Thanks for adding the example. I have updated the change request too.

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.

jonathan1055’s picture

Issue summary: View changes
Issue tags: -Needs announcement for final discussion

The announcement has been published
coding-standards-proposals-for-final-discussion-on-2-january-2024

Removing the "Needs announcement for final discussion" tag. Now at step 5.

quietone’s picture

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.

arkener’s picture

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

jonathan1055’s picture

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

drunken monkey’s picture

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'] : '';.

jonathan1055’s picture

Title: Use null coalescing operator (??) instead of ternary operator with isset statement » Use null coalescing operator ?? instead of a ternary operator with an isset() condition
Issue summary: View changes

Updated IS with the agreed changes in #35-38

quietone’s picture

Issue summary: View changes

Formatting of the example

quietone’s picture

Issue summary: View changes
dww’s picture

Issue summary: View changes
Issue tags: +Needs committer feedback

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

bbrala’s picture

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

quietone’s picture

Issue summary: View changes

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

bbrala’s picture

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

bbrala’s picture

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.

quietone’s picture

Issue summary: View changes

I reviewed the documentation updates and they agree with the Issue Summary.
The core issue exists, #3418494: Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=)

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

bbrala’s picture

Fixed credits

bbrala’s picture

Issue summary: View changes

Think you misreferenced @quietone. I didnt find one specifically for this change. So made #3446107: Enforce null coalescing operator ?? instead of a ternary operator through phpcs.

bbrala’s picture

Status: Reviewed & tested by the community » Fixed

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

Status: Fixed » Closed (fixed)

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