mglaman / phpstan-drupal

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

Improve argument checks to constructor with `new static()` #470

Open KalleVuorjoki opened 2 years ago

KalleVuorjoki commented 2 years ago

How to reproduce the issue:

Make following change to Drupal core 10.1.x and run ./vendor/bin/phpstan analyze -c phpstan.neon core/modules/user/src/Form/EntityPermissionsForm.ph command and it doesn't report missing parameter on create function.

diff --git a/core/modules/user/src/Form/EntityPermissionsForm.php b/core/modules/user/src/Form/EntityPermissionsForm.php
index a787fe3239..6444896913 100644
--- a/core/modules/user/src/Form/EntityPermissionsForm.php
+++ b/core/modules/user/src/Form/EntityPermissionsForm.php
@@ -74,7 +74,6 @@ public static function create(ContainerInterface $container) {
       $container->get('entity_type.manager')->getStorage('user_role'),
       $container->get('module_handler'),
       $container->get('config.manager'),
-      $container->get('entity_type.manager')
     );
   }

phpstan.neon used:

parameters:
  customRulesetUsed: true
  level: 5
  ignoreErrors:
    - '#\Drupal calls should be avoided in classes, use dependency injection instead#'
    - '#Plugin definitions cannot be altered.#'
    - '#Missing cache backend declaration for performance.#'
    - '#Plugin manager has cache backend specified but does not declare cache tags.#'
  excludePaths:
    - */tests/Drupal/Tests/Listeners/Legacy/*
    - */tests/fixtures/*.php
    - */settings*.php
    - */bower_components/*
    - */node_modules/*

Expected result

Catch error with Class Drupal\user\Form\EntityPermissionsForm constructor invoked with 4 parameters, 5 required.

mglaman commented 2 years ago

Note: this catches when using new self but not new static. This is due to how PHPStan treats static as maybe in its trinary logic.

mglaman commented 1 year ago

Labeling as an enhancement because we need to see if we can make PHPStan work the same for new static and new self in these instances.