Problem/Motivation

Refactor the code to replace static code with dependency injection where possible.

Examples

  • Use EntityTypeManagerInterface instead of EntityInterface::load()/loadMultiple()
  • Inject dependencies instead of using \Drupal
  • Make static methods non-static where possible (eg. UserProfileImage::generateProfileImageUrl()
  • Use the memory backend cache instead of drupal_static (eg. In BaseEntry::getUserPictureFromCache)
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

DieterHolvoet created an issue. See original summary.

soham sengupta’s picture

Assigned: Unassigned » soham sengupta

Assigning it to my name.

soham sengupta’s picture

Assigned: soham sengupta » Unassigned
Joel Guerreiro Borghi Filho’s picture

Assigned: Unassigned » Joel Guerreiro Borghi Filho

Will try to work on this one =)

Joel Guerreiro Borghi Filho’s picture

Status: Active » Needs work

After running phpcs I found another possible D.I. issue:

FILE: /modules/contrib/content_planner/src/ContentModerationService.php
------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------
74 | WARNING | ContentModerationState::loadMultiple calls should be avoided in classes, use dependency injection instead
------------------------------------------------------------------------------------------------------------------------------

Changing status to needs work.

dieterholvoet’s picture

Status: Needs work » Active

You usually change the status to Needs work if an issue needed a review and after reviewing it was determined that changes are needed. This issue doesn't have a patch or MR, so changing back to Active.

Joel Guerreiro Borghi Filho’s picture

Alright @DieterHolvoet, rookie here, I'm sorry, and thanks for the clarification.

Joel Guerreiro Borghi Filho’s picture

Assigned: Joel Guerreiro Borghi Filho » Unassigned
StatusFileSize
new573 bytes

Patch for #5.

Kindly review.

dieterholvoet’s picture

Status: Active » Needs work

That's a good start! Let's refactor the other examples in the issue description as well, and any other places I might have missed.

Joel Guerreiro Borghi Filho’s picture

Assigned: Unassigned » Joel Guerreiro Borghi Filho

@DieterHolvoet, nice, I will keep working on this then =).

Joel Guerreiro Borghi Filho’s picture

Assigned: Joel Guerreiro Borghi Filho » Unassigned

If possible I would like more details on this one, I am getting a bit blocked.

I wrote this new patch with both the previous patch changes and a few more.

Any help for improving is welcome.

Kindly review.

Joel Guerreiro Borghi Filho’s picture

StatusFileSize
new2.14 KB
dieterholvoet’s picture

Issue tags: +Needs reroll

Your patch is going to need a reroll since #3298588: Drupal Coding standards are not followed in project. was committed today. There's some overlap between that issue and this one.

The changes related to using $this->entityTypeManager seem okay.

@@ -111,7 +111,8 @@ class UserBlock extends DashboardBlockBase {
    */
   protected function getUserContentCount($user_id) {
 
-    $query = \Drupal::database()->select('node_field_data', 'nfd');
+    $database = \Drupal::database();
+    $query = $database()->select('node_field_data', 'nfd');
     $query->fields('nfd', ['nid']);
     $query->condition('nfd.uid', $user_id);
     $query->countQuery();

Here, you would override the create method, assign the database service to a new property and use that property instead of \Drupal::database().

Make static methods non-static where possible (eg. UserProfileImage::generateProfileImageUrl()

This is about checking whether we can make that method not static, make the class a service and inject it in the same way I described above. The EntityInterface::load could then be replaced with $this->entityTypeManager->....

jobsons’s picture

Status: Needs work » Needs review
StatusFileSize
new3.92 KB

Hi, it's my first reroll I hope it's ok. Here's the patch:
If someone could review it. Thanks :)

dieterholvoet’s picture

Status: Needs review » Needs work

That doesn't look right, you added the previous patch to the new patch as a .patch file. You can follow the instructions here.

jobsons’s picture

Status: Needs work » Needs review
StatusFileSize
new1.58 KB

I could make the reroll of patch 12 without the previous patch being added :) If someone could please review it.

Joel Guerreiro Borghi Filho’s picture

Assigned: Unassigned » Joel Guerreiro Borghi Filho
Issue tags: -Needs reroll

Reviewed patch from #16, it applies.
Removed reroll tag.
Changing to needs work.
Will try to work on #13 comment changes.

Joel Guerreiro Borghi Filho’s picture

Status: Needs review » Needs work
Joel Guerreiro Borghi Filho’s picture

Assigned: Joel Guerreiro Borghi Filho » Unassigned
StatusFileSize
new4.51 KB

I am not 100% sure this is correct, any tips are welcome for improving this.
Unassigning myself.
Will keep as needs work as it is not finished.

diegors’s picture

Assigned: Unassigned » diegors

I´ll try to look on that

diegors’s picture

Assigned: diegors » Unassigned
Status: Needs work » Needs review
StatusFileSize
new5.52 KB
new3.81 KB

I found some bugs and made some changes, please review

gquisini’s picture

Assigned: Unassigned » gquisini

I'll be reviewing this one.

gquisini’s picture

Assigned: gquisini » Unassigned
StatusFileSize
new6.25 KB
new1.09 KB

I found and fixed some bugs.

lucassc’s picture

Assigned: Unassigned » lucassc
lucassc’s picture

Assigned: lucassc » Unassigned
Status: Needs review » Needs work

I applied the patch in #23 and verified that:

1) Searches didn't revel any occurrence of EntityInterface::load() or EntityInterface::loadMultiple();

2) No phpcs warnings like "\Drupal calls should be avoided in classes, use dependency injection instead" was found;

3) @diegors' patch in #21:

@@ -111,7 +109,8 @@ class UserBlock extends DashboardBlockBase {
    */
   protected function getUserContentCount($user_id) {
 
-    $query = \Drupal::database()->select('node_field_data', 'nfd');
+    $database = \Drupal::database();
+    $query = $database()->select('node_field_data', 'nfd');
     $query->fields('nfd', ['nid']);
     $query->condition('nfd.uid', $user_id);
     $query->countQuery();

Here could the create method still be overridden as described in #13? See Dependency injection in Plugin (Block) and check the create method of the UserBlock's parent class (DashboardBlockBase).

Edit: I realized that in #19 @Joel Guerreiro Borghi Filho attached an interdiff that implements changes suggested in #13 but I couldn't find his patch.

sophiavs’s picture

Assigned: Unassigned » sophiavs

i'll work on those errors

sophiavs’s picture

Assigned: sophiavs » Unassigned
Status: Needs work » Needs review

I did some changes that was reported in phpcs and removed the call of \Drupal::database.
The statics methods that i found was changed to non-statics calls.

sophiavs’s picture

StatusFileSize
new9.18 KB
diegors’s picture

Assigned: Unassigned » diegors

I'll review this.

diegors’s picture

Assigned: diegors » Unassigned
StatusFileSize
new31.06 KB
new22.29 KB

Fixed the remaining errors.

Please review.

sophiavs’s picture

Assigned: Unassigned » sophiavs

I'll be reviewing

sophiavs’s picture

Assigned: sophiavs » Unassigned
Status: Needs review » Reviewed & tested by the community

The phpcs doesn't show any error and while testing the module everything goes fine. Passing to RTBC.

dieterholvoet’s picture

Assigned: Unassigned » dieterholvoet
Priority: Normal » Minor
Status: Reviewed & tested by the community » Needs work

Any changes in assets/dist should be reverted, that css is compiled and shouldn't be checked for CS issues. More improvements can be done, I started work on a MR which is still untested and WIP.

  • DieterHolvoet committed bd8da47 on 8.x-1.x
    Issue #3298434 by DieterHolvoet, diegors, Joel Guerreiro Borghi Filho,...
dieterholvoet’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.