Review DI in classes and their construct methods

Created on 19 December 2024, 4 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
    4 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
    4 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

  • Status changed to Closed: won't fix about 2 months ago
  • 🇧🇪Belgium kristiaanvandeneynde Antwerp, Belgium

    Actually, we should still inject them because if we call the parent method without injecting them, they will use \Drupal::service() instead. We should absolutely avoid using that where we can.

    Closing as won't fix as I've just upgraded all constructors to use property promotion where possible instead. See 📌 Drop constructor documentation and use property promotion where possible Active

Production build 0.71.5 2024