Avoid static \Drupal calls inside class methods and use proper DI

Created on 19 September 2023, about 1 year ago
Updated 5 August 2024, 4 months ago

Problem/Motivation

Right now in many parts of the codebase, services are called via static \Drupal. While this is fine in *.module, *.install and in static functions and callbacks, in classes it shouldn't be used. Need to refactor it with proper dependeny injection.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs work

Version

4.0

Component

Code

Created by

🇸🇰Slovakia kaszarobert

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

Merge Requests

Comments & Activities

  • Issue created by @kaszarobert
  • First commit to issue fork.
  • Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    Not currently mergeable.
  • @sandipta opened merge request.
  • 🇩🇪Germany simonbaese Berlin

    I can help with reviewing this issue.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • 🇩🇪Germany simonbaese Berlin

    Hi Sandipta,

    can we please discuss the scope of this issue? I think the title suggests changes that are too broad and it will make the review hard. Maybe we can restrict ourselves to plugins first.

    I see that you started to introduce changes in the merge request. But there are many other plugins that also make static calls to the container.

    From static analysis I can see:

    • GoogleAnalyticsCounterResultProcessorPluginBase fixed in MR (see comments)
    • /Plugin/GoogleAnalyticsCounterResultProcessor/DefaultResultProcessor
    • /Plugin/GoogleAnalyticsCounterResultProcessor/DefaultResultProcessor
    • /Plugin/GoogleAnalyticsCounterResultProcessor/UrlAliasResultProcessor
    • /Plugin/QueueWorker/GoogleAnalyticsCounterQueueBase

    It is a little bit unusual that the plugin base is not in the Plugin/ namespace.

    Also, I think it is best practice to make your changes in the issue branch (in this case 3388151-avoid-static-drupal) rather than directly in the fork. And then make a merge request from the issue branch to the actual repository. But maybe it doesn't matter.

  • 🇮🇳India sandipta

    Sure , will keep that in mind @simonbaese.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    7 pass, 2 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    7 pass, 2 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update about 1 year ago
    8 pass
  • 🇩🇪Germany simonbaese Berlin

    @sandipta What is the status on this issue? Do you need help to progress?

  • 🇮🇳India sandipta

    Checked everything,two files remain where DI hasn't been implemented yet:

    1. src/Plugin/GoogleAnalyticsCounterResultProcessor/UrlAliasResultProcessor.php
    2. src/GoogleAnalyticsCounterHelper.php

    I'll begin working on them soon.
    @simonbaese

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update 11 months ago
    8 pass
  • First commit to issue fork.
  • Status changed to Needs review 7 months ago
  • 🇮🇳India Sapnakhatri

    I have updated the MR, added DI in the remaining files. Please review.

  • First commit to issue fork.
  • Pipeline finished with Failed
    5 months ago
    Total: 145s
    #218851
  • 🇮🇳India manish-31

    MR rebased for conflict resolution.

  • Status changed to Needs work 4 months ago
  • 🇮🇳India sourav_paul Kolkata

    Mr applied successfully but please check why the PHPUnit is failing...

    Also please check this phpcs report :

    FILE: /home/sourav/projects/my_site/web/modules/contrib/google_analytics_counter/src/GoogleAnalyticsCounterAppManager.php
    -------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
    -------------------------------------------------------------------------------------------------------------------------
      18 | WARNING | [x] Unused use statement
     202 | ERROR   | [x] Multi-line function declarations must have a trailing comma after the last parameter
    -------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: ...jects/my_site/web/modules/contrib/google_analytics_counter/src/Plugin/GoogleAnalyticsCounterResultProcessor/DefaultResultProcessor.php
    --------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    --------------------------------------------------------------------------------------------------------------------------------------------
     35 | ERROR | [x] Expected 1 blank line after function; 0 found
    --------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    --------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: ...ects/my_site/web/modules/contrib/google_analytics_counter/src/Plugin/GoogleAnalyticsCounterResultProcessor/UrlAliasResultProcessor.php
    --------------------------------------------------------------------------------------------------------------------------------------------
    FOUND 3 ERRORS AND 3 WARNINGS AFFECTING 6 LINES
    --------------------------------------------------------------------------------------------------------------------------------------------
      5 | WARNING | [x] Unused use statement
      9 | WARNING | [x] Unused use statement
     12 | WARNING | [x] Unused use statement
     77 | ERROR   | [ ] Doc comment for parameter $database does not match actual variable name $language
     85 | ERROR   | [ ] Doc comment for parameter $route_match does not match actual variable name $language
     87 | ERROR   | [ ] Superfluous parameter comment
    --------------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    --------------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/sourav/projects/my_site/web/modules/contrib/google_analytics_counter/src/GoogleAnalyticsCounterMessageManager.php
    -----------------------------------------------------------------------------------------------------------------------------
    FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
    -----------------------------------------------------------------------------------------------------------------------------
     12 | WARNING | [x] Unused use statement
    -----------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    -----------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/sourav/projects/my_site/web/modules/contrib/google_analytics_counter/src/GoogleAnalyticsCounterHelper.php
    ---------------------------------------------------------------------------------------------------------------------
    FOUND 6 ERRORS AND 1 WARNING AFFECTING 6 LINES
    ---------------------------------------------------------------------------------------------------------------------
       9 | WARNING | [x] Unused use statement
      84 | ERROR   | [x] Expected 1 blank line after function; 0 found
     137 | ERROR   | [x] Separate the @param and @return sections by a blank line.
     170 | ERROR   | [x] Whitespace found at end of line
     216 | ERROR   | [x] Expected 1 space between comma and type hint "TimeInterface"; 0 found
     216 | ERROR   | [x] Expected one space after the comma, 0 found
     350 | ERROR   | [x] Separate the @param and @return sections by a blank line.
    ---------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/sourav/projects/my_site/web/modules/contrib/google_analytics_counter/src/Controller/GoogleAnalyticsCounterController.php
    ------------------------------------------------------------------------------------------------------------------------------------
    FOUND 2 ERRORS AFFECTING 2 LINES
    ------------------------------------------------------------------------------------------------------------------------------------
     109 | ERROR | [x] Multi-line function declarations must have a trailing comma after the last parameter
     110 | ERROR | [x] Multi-line function declaration not indented correctly; expected 2 spaces but found 4
    ------------------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ------------------------------------------------------------------------------------------------------------------------------------
    
    
    FILE: /home/sourav/projects/my_site/web/modules/contrib/google_analytics_counter/src/Form/ConfirmClearQueueForm.php
    -------------------------------------------------------------------------------------------------------------------
    FOUND 2 ERRORS AFFECTING 2 LINES
    -------------------------------------------------------------------------------------------------------------------
     32 | ERROR | Parameter $queue is not described in comment
     37 | ERROR | Missing parameter name
    -------------------------------------------------------------------------------------------------------------------
    
    Time: 425ms; Memory: 14MB
Production build 0.71.5 2024