Problem/Motivation
\Drupal calls should be avoided in classes using dependency injection.
Steps to reproduce
FILE: ...dules/contrib/patternkit/src/Controller/PatternkitController.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
----------------------------------------------------------------------
111 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
112 | WARNING | Unused variable $pattern_key.
----------------------------------------------------------------------
FILE: ...p/modules/contrib/patternkit/src/Form/PatternkitSettingsForm.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
----------------------------------------------------------------------
82 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
98 | WARNING | Unused variable $namespace.
----------------------------------------------------------------------
FILE: ...p/modules/contrib/patternkit/src/Commands/PatternkitCommands.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 16 WARNINGS AFFECTING 16 LINES
----------------------------------------------------------------------
50 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
53 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
67 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
75 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
77 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
79 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
82 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
121 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
137 | WARNING | Unused variable $component_delta.
229 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
236 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
238 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
243 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
246 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
281 | WARNING | \Drupal calls should be avoided in classes, use
| | dependency injection instead
299 | WARNING | Unused variable $component_delta.
----------------------------------------------------------------------
Proposed resolution
Using Dependency injection need to add servicers in containers and use instead of \Drupal calls
| Comment | File | Size | Author |
|---|
Issue fork patternkit-3300226
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
Comment #2
akshaydalvi212 commentedHello,
I will work on this issue and provide the patch to resolve this warning.
Thanks and regards.
Comment #3
sluceroThanks for flagging this @akshaydalvi212. I posted a related issue (#3300299: Replace Static Method Calls with Dependency Injection) to standardize DI usage a bit further throughout the module and have uploaded some work there to move in that direction. Some, but not all of those should be addressed there, so there may be some overlap between these two issues.
Comment #4
jsricardo commentedComment #5
jsricardo commentedApplayed dependence injection
Comment #6
dipesh_goswamiHi jsricardo,
I am reviewing your patch.
Thanks
Comment #7
dipesh_goswamiHi jsricardo,
Your patch applied cleanly.
Only these two issues are left to be fixed ( shown below):
so, moving it to "Needs work".
Thanks
Comment #8
jsricardo commentedThank you dipesh_goswami
I will Work on it
Comment #9
jsricardo commentedFixed the two Dependency Injection warnings.
Follow new patch
Comment #10
alexanderj commentedi will review it.
Comment #11
alexanderj commented@jsricardo Your patch #9 only has changes referring to comment #7, the changes that were made in your patch #4 were not introduced in patch #9, please add the changes from before in your next patch.
Comment #12
nitin_lamaComment #13
nitin_lamaUpdated patch as per #11
Comment #14
alexanderj commentedi will review it.
Comment #15
alexanderj commentedI reviewed the new patch in comment #13, and found some things to fix:
- 3 docblock without information, in the file
/patternkit_media_library/src/Controller/PatternkitMediaLibraryController.php:- In the
patternkit/src/Commands/PatternkitCommands.phpfile, the constructor's docblock is missing the @params of the classes that were added with dependency injection.- In the
patternkit/src/Commands/PatternkitCommands.phpfile, thecreatemethod is missing the close curly bracket.Comment #16
jsricardo commentedI Will Work on it
Comment #17
jsricardo commentedsorry for the delay
Here is the patch with the fixes mentioned in comment #15
Comment #18
ramonvasconcelos commentedI'll review it.
Comment #19
ramonvasconcelos commentedIt seems the patch needs a reroll since there were a lot of commits since this issue was open. I'll do it.
Comment #20
ramonvasconcelos commented@jsricardo i could not apply your patch or reroll it. I think you applied @nitin_lama changes on a previous commit from his and it worked. But then when the maintainer commited more changes your patch broke some of them.
Example of the 2 patchs changes.

1st patch:
Your patch:

As you can see in these examples, there are things missing in your patch. Making it impossible for me to reroll it.
You need to apply @nitin_lama's to the right branch commit and then do your changes. After that the patch can be rerolled.
Comment #21
ramonvasconcelos commentedI took the liberty to fix the patch. Please, review it.
Comment #22
mpauloThere are a few errors on #21
1. A closing } is missing from the PatternkitCommands::create function.
2. PatternkitCommands extends from DrushCommands, and DI for these classes should happen trough drush service yml files, and since they don't implement ContainerInjectionInterface, they don't have an overridable ::create function.
3. PatternkitMediaLibraryController extends ControllerBase, which already implements ContainerInjectionInterface. The implementation redundancy should be fixed.
Comment #23
ramonvasconcelos commentedOkay, i'll take another look.
Comment #24
ramonvasconcelos commentedI opened a MR but there seems to be a problem with Druapal.org i don't know. The MR just didn't show in this issue. https://git.drupalcode.org/issue/patternkit-3300226/-/tree/3300226-drupa...
Anyway, i applied the changes requested in #22.
Please, review it.
Comment #25
ramonvasconcelos commentedComment #27
jsricardo commentedI will review it
Comment #28
jsricardo commentedI have reviewed this issue, it seems to me to be ok.
I will move to RTBC
Comment #29
sluceroThe work here looks good, but I added some notes for a couple more minor changes before we merge it in.
Comment #30
LeoAlcci commentedI am working on it!
Comment #31
LeoAlcci commentedI applied the changes requested, please kindly review it.
Comment #32
LeoAlcci commentedComment #33
sluceroThanks for the work on this everyone! This one especially was a great community effort with numerous people working together to push it along, and I greatly appreciate it!
The work looks good and the tests are all passing, so I'm going to go ahead and merge this in.
Comment #35
sluceroMerged for inclusion in the Beta 6 release. Thanks everyone!