Problem/Motivation

PHPStan is reporting a number of errors due to \Drupal calls from classes.

I believe all PHPStan issues were fixed during the GitLabCI migration, and these lines seem to have been added before it, so it could be a change to the PHPStan rules in GitLabCI:

 ------ -------------------------------------------------------------------------------------------------------- 
  Line   modules/project_browser_source_example/src/Plugin/ProjectBrowserSource/ProjectBrowserSourceExample.php  
 ------ -------------------------------------------------------------------------------------------------------- 
  47     \Drupal calls should be avoided in classes, use dependency injection                                    
         instead                                                                                                 
 ------ -------------------------------------------------------------------------------------------------------- 
 ------ ---------------------------------------------------------------------- 
  Line   src/Commands/UpdateFixtureCommands.php                                
 ------ ---------------------------------------------------------------------- 
  87     \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
 ------ ---------------------------------------------------------------------- 
 ------ ---------------------------------------------------------------------- 
  Line   src/Plugin/ProjectBrowserSource/DrupalCore.php                        
 ------ ---------------------------------------------------------------------- 
  180    \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
 ------ ---------------------------------------------------------------------- 
 ------ ---------------------------------------------------------------------- 
  Line   src/Plugin/ProjectBrowserSource/MockDrupalDotOrg.php                  
 ------ ---------------------------------------------------------------------- 
  328    \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
 ------ ---------------------------------------------------------------------- 
 ------ ---------------------------------------------------------------------- 
  Line   src/ProjectBrowserFixtureHelper.php                                   
 ------ ---------------------------------------------------------------------- 
  386    \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
  521    \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
 ------ ---------------------------------------------------------------------- 
 ------ ---------------------------------------------------------------------- 
  Line   tests/modules/project_browser_test/src/Datetime/TestTime.php          
 ------ ---------------------------------------------------------------------- 
  37     \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
 ------ ---------------------------------------------------------------------- 
 ------ ---------------------------------------------------------------------- 
  Line   tests/modules/project_browser_test/src/DrupalOrgClientMiddleware.php  
 ------ ---------------------------------------------------------------------- 
  94     \Drupal calls should be avoided in classes, use dependency injection  
         instead                                                               
 ------ ---------------------------------------------------------------------- 
 [ERROR] Found 8 errors  

Steps to reproduce

Check a GitLabCI run of PHPStan, such as: https://git.drupalcode.org/issue/project_browser-3437721/-/jobs/1345914

Proposed resolution

Replace \Drupal calls with dependency injection.

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

lostcarpark created an issue. See original summary.

fjgarlin’s picture

I made some comments in the MR about forcing services into all plugins. It's great that the base class implements the dependency injection interface., but I think that unless the base plugin needs a service, it shouldn't be included. Then each plugin will bring the services that would be needed instead.

lostcarpark’s picture

Status: Active » Needs review

I have changed all \Drupal calls with dependency injections, and PHPStan is now passing.

Moving to Needs Review.

fjgarlin’s picture

Status: Needs review » Needs work

I left a comment in the MR. Hopefully it makes sense.

Good work so far.

lostcarpark’s picture

Status: Needs work » Needs review

I don't think the constructor is needed, because there's already one in one of the inherited classes. I have added a create function.

fjgarlin’s picture

Status: Needs review » Reviewed & tested by the community

The changes look good to me, the phpstan job is now green and testing via DrupalPod works as expected.
Thanks!

chrisfromredfin made their first commit to this issue’s fork.

chrisfromredfin’s picture

Status: Reviewed & tested by the community » Fixed

Thank you for this!

Status: Fixed » Closed (fixed)

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