Change !isset to ??= assignment operator: multi-line case.

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

Problem/Motivation

The purpose is to use null coalescing assignment operator ??=. This issue corresponds to the particular case B where the original code is multi-lines. Typically,

if (!isset($A)) {
  $A = $B;
}

changed to,
$A ??= $B;

For example, in core/modules/layout_discovery/layout_discovery.module, in function template_preprocess_layout,

    if (!isset($variables['content'][$name]['#attributes'])) {
      $variables['content'][$name]['#attributes'] = [];
    }

could be changed in,
$variables['content'][$name]['#attributes'] ??= [];

Steps to reproduce

Proposed resolution

From #2 πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work in πŸ“Œ 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,

Therefore trying to do this with some sed commands.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Postponed

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 12 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 Needs review 9 months ago
  • πŸ‡«πŸ‡·France Chris64 France

    A Commit corresponding nearly to the patch 9B β†’ . As said in #9 πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work this need review and comments.

  • Status changed to Needs work 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Don't see anything to review?

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

    Also if this is a spin off of the other ticket can it be noticed how come?

    Also if comments can be copied so reviewers/committers don't have to jump between tickets to follow a conversation.

  • Merge request !6757Resolve #3423310 "Use null coalescing" β†’ (Open) created by Chris64
  • Pipeline finished with Failed
    9 months ago
    #102545
  • πŸ‡«πŸ‡·France Chris64 France
  • πŸ‡«πŸ‡·France Chris64 France

    Mistakes exist. Still some work.

  • Pipeline finished with Failed
    9 months ago
    Total: 197s
    #102782
  • πŸ‡«πŸ‡·France Chris64 France

    The MR faced obstructions from phpstan: 4 = 2x2 errors: 2 files,

     ------ --------------------------------------------------------------------------------------------- 
      Line   core/modules/views/src/Plugin/views/display/PathPluginBase.php                               
     ------ --------------------------------------------------------------------------------------------- 
             Ignored error pattern #^Variable \$access_plugin in isset\(\) always                         
             exists and is not nullable\.$# in path                                                       
             /builds/issue/drupal-3423310/core/modules/views/src/Plugin/views/display/PathPluginBase.php  
             was not matched in reported errors.                                                          
      199    Variable $access_plugin on left side of ??= always exists and is not                         
             nullable.                                                                                    
     ------ --------------------------------------------------------------------------------------------- 
     ------ --------------------------------------------------------------------------------------------------- 
      Line   core/modules/views_ui/tests/src/FunctionalJavascript/PreviewTest.php                               
     ------ --------------------------------------------------------------------------------------------------- 
             Ignored error pattern #^Variable \$message in isset\(\) always exists                              
             and is not nullable\.$# in path                                                                    
             /builds/issue/drupal-3423310/core/modules/views_ui/tests/src/FunctionalJavascript/PreviewTest.php  
             was not matched in reported errors.                                                                
      309    Variable $message on left side of ??= always exists and is not                                     
             nullable.                                                                                          
     ------ --------------------------------------------------------------------------------------------------- 
     [ERROR] Found 4 errors

    The problem seems not the present code transformation but an other problem. To allow a MR we propose,
    - to remove these 2 files from this MR,
    - to create an other issue about these phpstan problems,
    - to treat in an other issue the ??= code transformation of these 2 remaining files.

  • Pipeline finished with Failed
    9 months ago
    Total: 173s
    #103600
  • Pipeline finished with Success
    9 months ago
    Total: 605s
    #103604
  • πŸ‡«πŸ‡·France Chris64 France

    Mergeable.

  • Status changed to Needs review 9 months ago
  • πŸ‡«πŸ‡·France Chris64 France

    Okay @smustgrave: MR is powerful, compare to patch. Lot of validation work is made by the machine. Now MR mergeable got, the human work needed is smaller.

    Then, compared to the initial goal,

    ideally a coding standards sniff we could enable to prevent this code from creeping back in.

    all the cases are not considered here. Two sets are known missing: #8 πŸ“Œ Use null coalescing assignment operator: multi-lines case. Needs work and #9 πŸ“Œ Use null coalescing assignment operator: multi-lines case. Needs work .
    #8 is not explicit enough about "generated files". That is some php files with header,

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

    I plan 2 other issues for them.

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

    Do you know if there is a code sniffing rule we could enable that would prevent these from coming back?

  • Status changed to Needs work 8 months ago
  • The Needs Review Queue Bot β†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

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

    As observed by @dpi for an other case πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work , some files are missed. Since some directories was ignored.

  • Pipeline finished with Success
    8 months ago
    Total: 479s
    #114842
  • Pipeline finished with Success
    8 months ago
    Total: 502s
    #115036
  • πŸ‡«πŸ‡·France Chris64 France

    A new commit, since a rebase was needed. 4 conflicts, in,

    core/lib/Drupal/Core/Database/Query/Insert.php
    core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php
    core/modules/workspaces/src/WorkspaceManager.php
    core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php
  • Status changed to Needs review 8 months ago
  • πŸ‡«πŸ‡·France Chris64 France

    MR mergeable. NR.

    Two sets are voluntary ignored here,
    - generator/generated files πŸ“Œ Change !isset to ??= assignment operator: generator and their generated files case. Needs review .
    - 2 files with phpstan obstruction πŸ“Œ Use null coalescing assignment operator ??=: phpstan obstruction case. Closed: won't fix .

    For a code sniff rule, this multi-line case is much more complicate than the uni-line case. Since several different configurations exist, and many should be exclude from this transformation: if else, if elseif, etc.

    The sed command used has a rule to reach the good targets. Not perfect. Maybe the best is to use rector rule, as suggest by @dpi in #3418494-#23 πŸ“Œ Switch long-form single-line assign and null coalesce (= ??) to null coalesce assign operator (??=) Needs work . However the rule doesn't exist (?).

    Questions,
    -1. Did the transformations done are right?
    -2. Did some files are missed?
    -3. Did some cases are missed?

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

    Do you know if there is a code sniffing rule we could enable that would prevent these from coming back?

    Okay @smustgrave, you mean a rule for phpcs... There is,
    slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/RequireNullCoalesceEqualOperatorSniff
    but is missing in drupal/coder/coder_sniffer/Drupal/ruleset.xml. That is,
    <rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator" />
    Maybe to be after,
    <rule ref="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator" />

  • Status changed to Needs work 8 months ago
  • The Needs Review Queue Bot β†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    8 months ago
    Total: 254s
    #127045
  • Pipeline finished with Success
    8 months ago
    Total: 624s
    #127051
  • Status changed to Needs review 8 months ago
  • πŸ‡«πŸ‡·France Chris64 France

    and ideally a coding standards sniff we could enable to prevent this code from creeping back in.

    A coding standards sniff rule now ready for this multi-line case. And now case fixable with phpcbf. MR mergeable. Need review.

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

    phpcs command used,
    phpcs --standard=SlevomatCodingStandard --sniffs=SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperatorIsset --extensions=php,module,inc core
    The file RequireNullCoalesceOperatorIssetSniff.php has to be in the right place: in slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures, in /home/.config/composer/vendor.

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

    Better file...

  • Status changed to Postponed 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave
Production build 0.71.5 2024