User::load calls should be avoided in classes, use dependency injection instead

Created on 28 June 2023, over 1 year ago
Updated 12 August 2023, over 1 year ago

FILE: D:\xampp\htdocs\drupal_10\web\modules\contrib\otp_service\src\Form\QrCodeForm.php
-------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 7 WARNINGS AFFECTING 4 LINES
-------------------------------------------------------------------------------------------------
29 | WARNING | User::load calls should be avoided in classes, use dependency injection instead
29 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
90 | WARNING | User::load calls should be avoided in classes, use dependency injection instead
90 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
95 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
106 | WARNING | User::load calls should be avoided in classes, use dependency injection instead
106 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
-------------------------------------------------------------------------------------------------

FILE: D:\xampp\htdocs\drupal_10\web\modules\contrib\otp_service\src\Plugin\Block\QRCodeBlock.php
------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
------------------------------------------------------------------------------------------------
25 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
------------------------------------------------------------------------------------------------

FILE: D:\xampp\htdocs\drupal_10\web\modules\contrib\otp_service\src\Service\OTPValidateService.php
--------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------
31 | WARNING | User::load calls should be avoided in classes, use dependency injection instead
--------------------------------------------------------------------------------------------------

📌 Task
Status

Fixed

Version

1.0

Component

Code

Created by

🇮🇳India arti_parmar

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

Comments & Activities

  • Issue created by @arti_parmar
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • 🇮🇳India arti_parmar

    Kindly review patch.

  • First commit to issue fork.
  • 🇮🇳India Raveen Kumar

    @arti, I have reviewed your patch And YES You're correct that using dependency injection instead of "User::load" calls can be beneficial in many cases. Dependency injection promotes loose coupling and improves the testability and maintainability of code.

    By using dependency injection, you can provide dependencies (such as database connections, repositories, or other services) to a class from the outside, rather than having the class create or access them directly. This makes the class more flexible, as it can work with different implementations of the dependencies without needing to modify its internal code.

    Here's an example to illustrate how dependency injection can be used instead of "User::load" calls in a class:
    class UserService {
    private $userRepository;

    public function __construct(UserRepository $userRepository) {
    $this->userRepository = $userRepository;
    }

    public function getUserById($userId) {
    return $this->userRepository->findById($userId);
    }

    }

  • @sonam_sharma opened merge request.
  • 🇵🇹Portugal nsalves

    Thanks for the feedback, all issues were fixed in the newest release

  • Status changed to Fixed over 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024