mglaman / phpstan-drupal

Extension for PHPStan to allow analysis of Drupal code.
https://phpstan-drupal.mglaman.dev/
MIT License
196 stars 76 forks source link

DrushStyle warnings with Drush 11 #420

Open neclimdul opened 2 years ago

neclimdul commented 2 years ago

Don't know if this is something for this project but it seems like it might be able to help. When using DrushStyle with recent versions of Drush 11 you'll run into this problem:

 ------ --------------------------------------------------------------------- 
  Line   web/modules/custom/my_custom/src/Commands/DrushCommandBase.php     
 ------ --------------------------------------------------------------------- 
  20     Instantiated class Drush\Style\DrushStyle not found.                 
         šŸ’” Learn more at https://phpstan.org/user-guide/discovering-symbols  
 ------ ---------------------------------------------------------------------

This is caused by a fix they added dynamically load different classes to support Symfony 6 and changed method signatures. 06eb4a45296e409c6af9b592d8bec0f555adedd8 Looking at the commit there are a number of other classes this would affect as well.

The problem seems to be that the class lives outside packagist autoloading so phpstan isn't picking it up. Drush is adding the root dynamically during preflight sort of like drupal module namespaces but based on the detected Symfony version which... could make things complicated.

neclimdul commented 2 years ago

If anyone one else runs into this, the immediate fix to get a analysis passing again was to add this to phpstan.neon.

  scanDirectories:
    - vendor/drush/drush/src-symfony-compatibility/v4

Replace v4 with v6 if you're testing things in D10.

mglaman commented 2 years ago

Ah yeah, the Symfony compatibility folder. I ran into this during a live stream with some other problem but cannot remember how it was worked around.

apotek commented 1 week ago

scanDirectories:

  • vendor/drush/drush/src-symfony-compatibility/v4

Thank you @neclimdul ! This saved my šŸ„“ . For others following this thread, add this under the parameters: section of your phpstan.neon file.