Since we are now using PHP 7+ we can use the null coalescing operator (??). The related issue had this change, but rather split this off.

+++ b/core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php
@@ -188,7 +198,7 @@ public function get($entity_type_id, $bundle) {
- return isset($resource_types[$type_name]) ? $resource_types[$type_name] : NULL;
+ return $resource_types[$type_name] ?? NULL;
Technically the `??` construct is equal to the `isset` contruct, just more readable.

The related only consisted of changes in the jsonapi module, since it woukd be so many lines.

How to reproduce

php-cs-fixer mr914

Added a config this time so it will do everything at once.

git clone --branch '9.3.x' https://git.drupalcode.org/project/drupal.git
cd drupal
mkdir --parents tools/php-cs-fixer
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer

Now create the config file at tools/php-cs-fixer/.php-cs-fixer.3222251.dist.php.

<?php
/*
 * This document has been generated with
 * https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0.0|configurator
 * you can change this configuration by importing this file.
 */
$config = new PhpCsFixer\Config();
$finder = PhpCsFixer\Finder::create()
  ->exclude('vendor')
  ->notPath('tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Template.php')
  ->notPath('lib/Drupal/Component/Annotation/Doctrine/DocParser.php')
  ->name('*.php')
  ->name('*.theme')
  ->name('*.module')
  ->name('*.sh')
  ->name('*.inc')
  ->in(['core', 'composer']);

return $config
    ->setRules([
        'ternary_to_null_coalescing' => true,
    ])
    ->setFinder($finder);

Run the fixer

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix . -vvv --diff --config tools/php-cs-fixer/.php-cs-fixer.3222251.dist.php --path-mode=intersection
git add core
git add composer

There is some manual work to fix some style issues like: ([$var,] and indentation faults).

Issue fork drupal-3222251

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bbrala created an issue. See original summary.

bbrala’s picture

bbrala’s picture

This patch is just an illustration, not ready yet.

bbrala’s picture

bbrala’s picture

bbrala’s picture

bbrala’s picture

bbrala’s picture

I hate file permissions in git combined with Windows.

longwave’s picture

I didn't do a full review but I think these changes should be reverted:

  1. +++ b/core/lib/Drupal/Component/Annotation/Doctrine/DocParser.php
    @@ -565,9 +565,7 @@ private function collectAnnotationMetadata($name)
    -        $type = isset(self::$typeMap[$attribute->type])
    -            ? self::$typeMap[$attribute->type]
    -            : $attribute->type;
    +        $type = self::$typeMap[$attribute->type] ?? $attribute->type;
    

    This is a copy of code from the Doctrine project so I think we should leave this alone (there are other coding standards issues in these files).

  2. +++ b/core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Template.php
    @@ -9,6 +9,6 @@ class Template
    -        $this->name = isset($values['value']) ? $values['value'] : null;
    +        $this->name = $values['value'] ?? NULL;
    

    Same here.

bbrala’s picture

Valid point about the Doctrine project classes.

Also i've just created a branch on Gitlab. The patches weren't playing nice :/ No real review needed yet though, want to wait for the reaction on the related issue. This is a lot of changes ;)

alexpott’s picture

@bbrala are you scripting this patch? If so can you add instructions to the issue summary? If there's not a script then perhaps we want to try and put one together to make it really simple to re-create the patch.

I think we should schedule this patch for the 9.3.x beta and land it then.

bbrala’s picture

I'm just using an inspection from PHPStorm which can scan a codebase for specific things and help you refactor those. The commands:

  • select core directory
  • code -> run inspection by name
  • "isset can be replaced coalesce"
  • check each line and apply quickfix

That was my workflow, unfortunately no script :)

The merge request is a clean patch though, but perhaps a trial of PHPStorm is something you could use.

bbrala’s picture

Issue summary: View changes
FileSize
2.03 MB

Also attached a screenshot of the workflow I used to refactor this.

andypost’s picture

bbrala’s picture

Issue summary: View changes

Unfortunately php-cs-fixed doesn't include .inc and .theme files by default. There are missing there. Added to the script. This should reproduce MR903. Ofcourse if could be done with a custom php-cs-fixer config with adjusted filenames, but that didn't work right away.

git clone --branch '9.3.x' https://git.drupalcode.org/project/drupal.git
cd drupal
mkdir --parents tools/php-cs-fixer
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/includes/batch.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/includes/bootstrap.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/includes/common.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/includes/errors.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/includes/form.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/includes/install.core.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/includes/install.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/includes/theme.inc --rules=ternary_to_null_coalescing -v

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/ckeditor/ckeditor.admin.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/language/language.admin.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/locale/locale.batch.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/locale/locale.bulk.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/locale/locale.compare.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/locale/locale.translation.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/search/search.pages.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/system/system.admin.inc --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/views_ui/views_ui.theme.inc --rules=ternary_to_null_coalescing -v

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/themes/claro/claro.theme --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/themes/claro/claro.theme --rules=ternary_to_null_coalescing -v

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/contact/contact.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/editor/editor.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/file/file.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/forum/forum.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/layout_builder/layout_builder.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/layout_discovery/layout_discovery.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/locale/locale.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/media/media.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/menu_ui/menu_ui.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/rdf/rdf.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/system/system.module --rules=ternary_to_null_coalescing -v
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix core/modules/user/user.module --rules=ternary_to_null_coalescing -v

git add core
git reset core/lib/Drupal/Component/Annotation/Doctrine/DocParser.php
git reset core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Template.php

The reset for the two files is based on #9.

bbrala’s picture

There were 4 files missed by php-cs-fixer that did have possible changes. See commit 0289376d.

guilhermevp’s picture

If there is any way I can help, I'm here!

bbrala’s picture

Issue summary: View changes
longwave’s picture

Title: Replace all isset constructs with the null coalescing operator » [November 8, 2021] Replace all isset constructs with the null coalescing operator
Status: Active » Postponed

Thanks for the work on this so far, and for providing the script - this should be helpful if we need to rebuild this MR later on.

I agree with @alexpott in #12 that this change is disruptive, as per the allowed changes policy this consists of "code style updates which are likely to conflict with other patches in the queue" and therefore should wait until the beta window, which unfortunately isn't until November.

Marking this as postponed and flagging the date; ideally we should aim to get this RTBC in early November so it can be committed on or shortly after the 8th.

bbrala’s picture

No problem ofcourse. Just update here or send a message on slack around that time and I'll create a new MR. Pretty easy to do.

andypost’s picture

Would be great to explore phpstan ability to detect such cases

bbrala’s picture

Technically it could also live in the codestyle. There seems to be an implementation for PHPCS that has this SlevomatCodingStandard/Sniffs/ControlStructures/RequireNullCoalesceOperatorSniff.php. Perhaps something like that could help since we could connect to the existing codestyle checker and fixer in core.

Not too sure if phpstan should be the place for this. Isn't phpstan mostly about finding bugs early and codestyle about controle flow formatting and such?

bbrala’s picture

Issue summary: View changes

Added simple way with a config.

bbrala’s picture

Ping :)

What would you need here?

voleger’s picture

Status: Postponed » Needs review
voleger’s picture

Status: Needs review » Needs work

#25 is not up to date. The changes needs to be rabased.

bbrala’s picture

Version: 9.3.x-dev » 9.4.x-dev

bbrala’s picture

I reran the scripts against 9.4.x, waiting for CI to succeed before sending for review.

There was some manual work to fix some style issues like: ([$var,] and indentation faults). But that was pretty minimal

bbrala’s picture

Status: Needs work » Needs review

Everything is green, changes where automated for 95% setting to needs review.

bbrala’s picture

Issue summary: View changes
voleger’s picture

I see the replacement of the list construction only. Is it expected?

bbrala’s picture

Ow ow, did i push the wrong branch, let met check

bbrala’s picture

Status: Needs review » Needs work

Yeah, i made an error, i was doing two issues last night for 2 styles. Appearanly i mixed up something. I'll put up a fix soon

bbrala’s picture

Status: Needs work » Needs review

Updated the PR, fixing the push. This monday i pushed the wrong changes to this issue while working on them both simulataniously.

alexpott’s picture

Status: Needs review » Needs work

We need to run this on the composer directory as well as core. Plus we need to include run-tests.sh.

bbrala’s picture

Issue summary: View changes

Updated config

<?php
/*
 * This document has been generated with
 * https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0.0|configurator
 * you can change this configuration by importing this file.
 */
$config = new PhpCsFixer\Config();
$finder = PhpCsFixer\Finder::create()
  ->exclude('vendor')
  ->notPath('tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Template.php')
  ->notPath('lib/Drupal/Component/Annotation/Doctrine/DocParser.php')
  ->name('*.php')
  ->name('*.theme')
  ->name('*.module')
  ->name('*.sh')
  ->name('*.inc')
  ->in(['core', 'composer']);

return $config
    ->setRules([
        'ternary_to_null_coalescing' => true,
    ])
    ->setFinder($finder);

Updated command:

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix . -vvv --diff --config tools/php-cs-fixer/.php-cs-fixer.3222251.dist.php --path-mode=intersection

This removes the needfor a reset. I'll push the updated code.

bbrala’s picture

Status: Needs work » Needs review

Run tests was already updated in the MR btw :)

longwave’s picture

Status: Needs review » Needs work

Reviewed the full patch. A few nits about stylistic comments (mostly rewrapping lines and useless brackets - feel free to argue back on any of these!) but otherwise this looks good, and makes every single case more readable than before.

bbrala’s picture

Went through them quickly, and a few need some extra attention and if correct a child issue imo.

Most nits I agree to, so many stray brackets :)

bbrala’s picture

Status: Needs work » Needs review

That was a bit of spam. There are 3 thread that need your attention @longwave.

I've also made a child issue for the views plugin test.

bbrala’s picture

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks - all issues resolved, this is RTBC for me.

  • larowlan committed 330473e on 9.4.x
    Issue #3222251 by bbrala, longwave: [November 8, 2021] Replace all isset...

  • larowlan committed f436f7c on 9.3.x
    Issue #3222251 by bbrala, longwave: [November 8, 2021] Replace all isset...
larowlan’s picture

Version: 9.4.x-dev » 9.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 330473e and pushed to 9.4.x. Thanks!

Backported to 9.3.x to keep the two branches in alignment and maximise the chance of backports applying cleanly.

Amazing work here.

Full credit to `git diff --color-words` which made reviewing this locally much easier.

Can we get a follow up to investigate a PHPCS rule to prevent any of these creeping back in?

Wrote and published a change record.

quietone’s picture

Can we get a follow up to investigate a PHPCS rule to prevent any of these creeping back in?

As I understand it, In order to do that we first need agreement on that as a coding standard, If that is correct then an issue is needed in the Coding Standards project.

Looking now for an issue.

Yes, there is, it is #3074131: Use null coalescing operator ?? instead of a ternary operator with an isset() condition.

There is no need for a followup.

larowlan’s picture

Awesome, thanks

larowlan’s picture

Crediting guilhermevp from the druplicate

xjm’s picture

Issue tags: +Needs followup

We should explore adding a phpcs rule for this so that we don't have to do cleanups of it every beta.

bbrala’s picture

@xjm The parent issue refers to a new dependency, in coder. That would also allow this:

SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator

(On mobile, links are hard sorry)

bbrala’s picture

Just to be complete;

As mentioned in #3222769: [November 8, 2021] Replace all list (array destructuring) assignment to the array syntax by @xjm this could be enforced if we adopt the library mentioned in #3010032: Add dependency on slevomat/coding-standard. This includes a rule that could validatie this change.

bbrala’s picture

Added related issue in Coding standards project.

quietone’s picture

Issue tags: -Needs followup

Removing tag, there are followups in Coding Standards project and the Coder project.

Status: Fixed » Closed (fixed)

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