Change !isset to ??= assignment operator: generator and their generated files case.

Created on 27 February 2024, 9 months ago
Updated 26 March 2024, 8 months ago

Problem/Motivation

Some files exist that are concerned by the use of the null coalescing assignment operator ??= and are generated. As met and said in #3423310-#11 πŸ“Œ Use null coalescing assignment operator: multi-lines case. Needs work . That is some php files with header,

/**
 * This file was generated via php core/scripts/generate-proxy-class.php <*> <*>.
 */

Those files are generated by the the generator,
core/lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php

Two other involved generators exist,

core/tests/Drupal/Tests/Core/ProxyBuilder/ProxyBuilderTest.php
core/tests/Drupal/Tests/Component/ProxyBuilder/ProxyBuilderTest.php

Up to indentations the function to be altered is unique,

/**
 * Lazy loads the real service from the container.
 *
 * @return object
 *   Returns the constructed real service.
 */
protected function lazyLoadItself()
{
    if (!isset($this->service)) {
        $this->service = $this->container->get($this->drupalProxyOriginalServiceId);
    }

    return $this->service;
}

Make some changes in the involved generators files.

The generated files must be altered too, since the associated generated files should correspond with its generator, and since the generated files must be in the Drupal package.

The motivation is to make the changes !isset() to ??= in the code wherever possible, as required in #3418494-#2 πŸ“Œ 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,

Steps to reproduce

Proposed resolution

The code part should be changed to,

protected function lazyLoadItself()
{
    $this->service ??= $this->container->get($this->drupalProxyOriginalServiceId);

    return $this->service;
}

- Generator: Make the change in the generators files
- Generated:
-- Either, regenerate the corresponding generated files.
-- Or, alter the original generated files in accordance with the generator.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Postponed

Version

11.0 πŸ”₯

Component
OtherΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡«πŸ‡·France Chris64 France

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

Merge Requests

Comments & Activities

  • Issue created by @Chris64
  • πŸ‡«πŸ‡·France Chris64 France
  • Status changed to Closed: won't fix 9 months ago
  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Generated files are immune to CS changes, and are better off for it. Generated files should only receive changes if there is a fundamental incompatibility due, such as API/PHP changes.

    You can see at the top of these files they have an cs ignore directive.

  • πŸ‡«πŸ‡·France Chris64 France

    Yes @dpi, I saw that: // phpcs:ignoreFile. But here it is about code instruction changes. I ask the question: Should we make some change in this kind of files? Your answer is: We should not.

    Yes it is a special case. A reason why this issue exists. Critical.

    I made the distinction between generated files and generator files. If some changes are made in the generator, some changes will happen in the generated files. And the generated need to be regenerated. Hence the generator is the key.

    (The generated files are in the Drupal core package and are not regenerated during a drush cr.)

    One an other hand the directive πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work is,

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

    and if I ignore some cases I face a kind of contradiction with this goal.

  • πŸ‡«πŸ‡·France Chris64 France

    The generator file should also concerned by the phpcs. However this file is not protected by the instruction // phpcs:ignoreFile. Probably the nowdoc block used is ignored by phpcs.

  • πŸ‡«πŸ‡·France Chris64 France

    @dpi, after reading your comments I think the actual problem exposition is not clear enough: generator is not present as it should be. The title and the IS should be changed.

    To be more faithful the title might be changed to "... generator and their generated files".

    In fact, the ask is about a change in the generator. A Drupal authority has to decide if the asked change is acceptable or not.

    If the generator is altered, the regenarated files will be too. The generated files must be in the package. Either the generator script must be applied, or the original generated files must be altered in accordance with the generator.

    The motivation for the ask is to make the changes !isset() to ??= in the code wherever possible.

  • πŸ‡«πŸ‡·France Chris64 France
  • Pipeline finished with Success
    8 months ago
    Total: 557s
    #113080
  • Status changed to Needs review 8 months ago
  • πŸ‡«πŸ‡·France Chris64 France

    A proposal. MR mergeable. Need review. Work done even maybe refused.

    Sed used. And not Rector, since I haven't its rule. Normally no files missed. The targeted function is lazyLoadItself. 31 files modified.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Is there a Meta tracking all of these?

    Is there a ticket to add a check for this (searching coder now myself)

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

    There are now several issues on this topic. To help reviews and understand the scoping of the issue it will help if a meta issue is made to discuss the overall change. The issues to implement the change should be children of that meta and then they can be removed as related. Use the component 'other' for these because they are not directly related to a particular subsystem. This is the same component used for Coding Standard fixes.

    See special issue titles β†’ for info about '[meta]'

  • πŸ‡«πŸ‡·France Chris64 France

    Actually a parent issue exists, with all the children. Is it the right place for such a [Meta] and discussion? Or some thing else is needed.

    Title changed. Is it suitable now?

  • πŸ‡«πŸ‡·France Chris64 France

    To take into account the request #11 πŸ“Œ Change !isset to ??= assignment operator: generator and their generated files case. Needs review the parent issue πŸ“Œ Use null coalescing assignment operator Active has been updated.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Just FYI not ignoring these just trying to find out the correct way these kind of changes are handled. If a check needs to be in place before making these changes for example

  • Status changed to Needs work 8 months ago
  • πŸ‡«πŸ‡·France Chris64 France
  • πŸ‡«πŸ‡·France Chris64 France

    Okay @smustgrave, an important information. How to do this with phpcs due to the obstruction in that case?

  • πŸ‡«πŸ‡·France Chris64 France

    For a method without phpcs. (supposing phpcs part done, processing the remaining part.) Supposing the concerned instruction is !isset($this->service). To get a file list,
    sudo grep -nr \!isset | grep service\)
    To verify the instructions,
    sudo grep -nrA 2 \!isset\(\$this-\>service\)
    In the files list, to distinguish generator and generated, as simply generated files are different but the instruction is always at the same line. For generator the line number is singular.

  • Status changed to Needs review 8 months ago
  • πŸ‡«πŸ‡·France Chris64 France
  • Status changed to Postponed 8 months ago
Production build 0.71.5 2024