Review DI in classes and their construct methods

Created on 19 December 2024, about 2 months ago

Problem/Motivation

I have noticed that some classes that extend from ControllerBase still have `entity_type.manager` (or similar) injected but later they are not used, as the function provided by the parent class is used instead.
therefore, we can avoid inject them.

In addition, the construct params and variables can be declared directly, so we can use this issue to review them.

Remaining tasks

code it, push it, review it and merge :)

🐛 Bug report
Status

Needs work

Version

4.0

Component

Code

Created by

🇧🇪Belgium gorkagr

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

Merge Requests

Comments & Activities

  • Issue created by @gorkagr
  • Merge request !206Resolve #3494986 "Review di in" → (Open) created by gorkagr
  • Pipeline finished with Canceled
    about 2 months ago
    Total: 86s
    #373721
  • 🇧🇪Belgium gorkagr

    I am not sure if you have also planned to do smth in the constructors and declare the variables directly in the params:

    smth like
    original:

    
      /**
       * The private store factory.
       *
       * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
       */
      protected $privateTempStoreFactory;
    
      /**
       * The renderer.
       *
       * @var \Drupal\Core\Render\RendererInterface
       */
      protected $renderer;
    
      /**
       * Constructs a new GroupController.
       *
       * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
       *   The private store factory.
       * @param \Drupal\Core\Render\RendererInterface $renderer
       *   The renderer.
       */
      public function __construct(PrivateTempStoreFactory $temp_store_factory, RendererInterface $renderer) {
        $this->privateTempStoreFactory = $temp_store_factory;
        $this->renderer = $renderer;
      }
    
    

    idea:

    
      /**
       * Constructs a new GroupController.
       *
       * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
       *   The private store factory.
       * @param \Drupal\Core\Render\RendererInterface $renderer
       *   The renderer.
       */
      public function __construct(
        protected PrivateTempStoreFactory $privateTempStoreFactory,
        protected RendererInterface $renderer,
      ) {
      }
    
    

    But then i assume that should be done to all files that have a __construct(), so all of them are unified

  • Pipeline finished with Success
    about 2 months ago
    Total: 1101s
    #373724
  • 🇧🇪Belgium kristiaanvandeneynde Antwerp, Belgium

    Uh yeah, property promotion and the omission of constructor docblocks is also on my todo list

Production build 0.71.5 2024