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

Issue fork patternkit-3300226

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

akshaydalvi212 created an issue. See original summary.

akshaydalvi212’s picture

Hello,

I will work on this issue and provide the patch to resolve this warning.

Thanks and regards.

slucero’s picture

Thanks 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.

jsricardo’s picture

Assigned: akshaydalvi212 » jsricardo
Status: Active » Needs work
jsricardo’s picture

Assigned: jsricardo » Unassigned
Status: Needs work » Needs review
StatusFileSize
new10.47 KB

Applayed dependence injection

dipesh_goswami’s picture

Assigned: Unassigned » dipesh_goswami

Hi jsricardo,
I am reviewing your patch.
Thanks

dipesh_goswami’s picture

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

Hi jsricardo,
Your patch applied cleanly.

Only these two issues are left to be fixed ( shown below):

FILE: ...modules\contrib\patternkit-3300226\modules\patternkit_media_library\src\Controller\PatternkitMediaLibraryController.php
-----------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
-----------------------------------------------------------------------------------------------------------------------------
 35 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
 39 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead

so, moving it to "Needs work".

Thanks

jsricardo’s picture

Assigned: Unassigned » jsricardo

Thank you dipesh_goswami
I will Work on it

jsricardo’s picture

Assigned: jsricardo » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.18 KB

Fixed the two Dependency Injection warnings.
Follow new patch

alexanderj’s picture

Assigned: Unassigned » alexanderj

i will review it.

alexanderj’s picture

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

@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.

nitin_lama’s picture

Assigned: Unassigned » nitin_lama
nitin_lama’s picture

Assigned: nitin_lama » Unassigned
Status: Needs work » Needs review
StatusFileSize
new12.86 KB

Updated patch as per #11

alexanderj’s picture

Assigned: Unassigned » alexanderj

i will review it.

alexanderj’s picture

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

I 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:

  /**
   *
   */
  public function __construct(MediaLibraryUiBuilder $media_library_builder) {
    $this->mediaLibraryUiBuilder = $media_library_builder

  }

  /**
   *
   */
  public static function create(ContainerInterface $containerInterface) {
    return new static(
    $container->get('media_library.ui_builder')
    );

  }

  /**
   *
   */
  public function mediaLibrary(Request $request): array {

- In the patternkit/src/Commands/PatternkitCommands.php file, the constructor's docblock is missing the @params of the classes that were added with dependency injection.
- In the patternkit/src/Commands/PatternkitCommands.php file, the create method is missing the close curly bracket.

jsricardo’s picture

Assigned: Unassigned » jsricardo

I Will Work on it

jsricardo’s picture

Assigned: jsricardo » Unassigned
Status: Needs work » Needs review
StatusFileSize
new12.95 KB

sorry for the delay
Here is the patch with the fixes mentioned in comment #15

ramonvasconcelos’s picture

Assigned: Unassigned » ramonvasconcelos

I'll review it.

ramonvasconcelos’s picture

Status: Needs review » Needs work

It seems the patch needs a reroll since there were a lot of commits since this issue was open. I'll do it.

ramonvasconcelos’s picture

Assigned: ramonvasconcelos » Unassigned
StatusFileSize
new101.22 KB
new98.59 KB

@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:
Print1

Your patch:
Print2

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.

ramonvasconcelos’s picture

Status: Needs work » Needs review
StatusFileSize
new13.42 KB

I took the liberty to fix the patch. Please, review it.

mpaulo’s picture

Status: Needs review » Needs work

There 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.

ramonvasconcelos’s picture

Assigned: Unassigned » ramonvasconcelos

Okay, i'll take another look.

ramonvasconcelos’s picture

Status: Needs work » Needs review
StatusFileSize
new13.09 KB

I 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.

ramonvasconcelos’s picture

Assigned: ramonvasconcelos » Unassigned

jsricardo’s picture

Assigned: Unassigned » jsricardo

I will review it

jsricardo’s picture

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

I have reviewed this issue, it seems to me to be ok.
I will move to RTBC

slucero’s picture

Status: Reviewed & tested by the community » Needs work
Related issues: +#3284744: Need to run pattern updater without Drush

The work here looks good, but I added some notes for a couple more minor changes before we merge it in.

LeoAlcci’s picture

Assigned: Unassigned » LeoAlcci

I am working on it!

LeoAlcci’s picture

Assigned: LeoAlcci » Unassigned

I applied the changes requested, please kindly review it.

LeoAlcci’s picture

Status: Needs work » Needs review
slucero’s picture

Status: Needs review » Reviewed & tested by the community

Thanks 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.

slucero’s picture

Status: Reviewed & tested by the community » Fixed
Parent issue: » #3308212: Beta 6 Release Plan

Merged for inclusion in the Beta 6 release. Thanks everyone!

Status: Fixed » Closed (fixed)

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