Use null coalescing operator ?? instead of a ternary operator with an isset() condition
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3074131. -->
Reported by: [init90](https://www.drupal.org/user/1322186)
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>Since Drupal 8.8 doesn't support PHP 5 we can use null coalescing operator (??) instead of ternary operator with isset statement.</p>
<h3 id="summary-benefits">Benefits</h3>
<p>The change will make code more laconic and easy to read. For example, this: </p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">return isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">handlers</span><span style="color: #007700">[</span><span style="color: #DD0000">'route_provider'</span><span style="color: #007700">][</span><span style="color: #0000BB">$entity_type_id</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">handlers</span><span style="color: #007700">[</span><span style="color: #DD0000">'route_provider'</span><span style="color: #007700">][</span><span style="color: #0000BB">$entity_type_id</span><span style="color: #007700">] : [];<br></span><span style="color: #0000BB">?></span></span></pre></div>
<p>becomes:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">handlers</span><span style="color: #007700">[</span><span style="color: #DD0000">'route_provider'</span><span style="color: #007700">][</span><span style="color: #0000BB">$entity_type_id</span><span style="color: #007700">] ?? [];<br></span><span style="color: #0000BB">?></span></span></pre></div>
<h3 id="summary-supporters">Three supporters required</h3>
<ol>
<li><a href="https://www.drupal.org/u/jonathan1055">https://www.drupal.org/u/jonathan1055</a> (8 Nov 2023)</li>
<li><a href="https://www.drupal.org/u/bbrala">https://www.drupal.org/u/bbrala</a> (8-11)</li>
<li><a href="https://www.drupal.org/u/larowlan">https://www.drupal.org/u/larowlan</a> (9 Nov 2023)</li>
</ol>
<h3 id="summary-proposed-changes">Proposed changes</h3>
<p>There is currently no mention of null-coalescing operators in the standards.</p>
<h4>1. <a href="https://www.drupal.org/docs/develop/standards/php/php-coding-standards#operators" title="Operators section of the Coding Standards">Operators section of the Coding Standards</a></h4>
<h5>Current text</h5>
<p>None</p>
<h5 id="proposed-text">Proposed new paragraph</h5>
<blockquote><p>The null coalescing operator <code>??</code> should be used instead of a ternary operator with an isset() condition, to make code more readable. For example use<br>
<code>$result = $values['entry'] ?? 'default'</code><br>
instead of<br>
<code>$result = isset($values['entry']) ? $values['entry'] : 'default'</code></p></blockquote>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<ol>
<li><del>Create this issue in the Coding Standards queue, using the defined template</del></li>
<li><del>Add supporters</del></li>
<li><del>Create a Change Record</del> <a href="https://www.drupal.org/node/3400475">https://www.drupal.org/node/3400475</a></li>
<li><del>Review by the Coding Standards Committee</del></li>
<li>Coding Standards Committee takes action as required. Announced on 2023-11-07 and 2024-01-02</li>
<li>N/A: Core <em>is</em> affected. <del>Tagged with 'Needs documentation edits' if Core is not affected</del></li>
<li><del>Discussed by the Core Committer Committee, if it impacts Drupal Core</del></li>
<li>Documentation updates
<ol>
<li><del>Edit all pages</del></li>
<li>Publish change record</li>
<li>Remove 'Needs documentation edits' tag</li>
</ol>
</li>
<li><del>Create follow-up issue for Core to enable the existing coder sniff <code>SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator</code></del> <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/drupal/issues/3446107" title="Status: Closed (fixed)">#3446107: Enforce null coalescing operator ?? instead of a ternary operator through phpcs</a></span></li>
</ol>
<p>For a fuller explanation of these steps see the <a href="https://www.drupal.org/project/coding_standards" title="Coding Standards project page">Coding Standards project page</a></p>
> Related issue: [Issue #3010032](https://www.drupal.org/node/3010032)
> Related issue: [Issue #3222251](https://www.drupal.org/node/3222251)
issue
GitLab AI Context
Project: project/coding_standards
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/coding_standards/-/raw/main/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/coding_standards
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD