phpstan-drupal icon indicating copy to clipboard operation
phpstan-drupal copied to clipboard

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

Open KalleVuorjoki opened this issue 3 years ago • 2 comments

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.

KalleVuorjoki avatar Aug 16 '22 13:08 KalleVuorjoki

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 avatar Aug 16 '22 17:08 mglaman

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.

mglaman avatar May 10 '23 20:05 mglaman