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

«Undefined array key» when using autowiring with decorator

Open Niklan opened this issue 1 year ago • 0 comments

Bug report

On ^1.2 version, decorating a service using autowiring feature without explicit class for decorator, leads to an error:

[19-Dec-2023 10:55:19 UTC] PHP Warning:  Undefined array key "Drupal\phpstan_example\BarInterface" in /var/www/html/vendor/mglaman/phpstan-drupal/src/Drupal/ServiceMap.php on line 65
Warning: Undefined array key "Drupal\phpstan_example\BarInterface" in /var/www/html/vendor/mglaman/phpstan-drupal/src/Drupal/ServiceMap.php on line 65
TypeError thrown in /var/www/html/vendor/mglaman/phpstan-drupal/src/Drupal/DrupalServiceDefinition.php on line 130 while loading bootstrap file /var/www/html/vendor/mglaman/phpstan-drupal/drupal-autoloader.php: mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition::addDecorator(): Argument #1 ($definition) must be of type mglaman\PHPStanDrupal\Drupal\DrupalServiceDefinition, null given, called in /var/www/html/vendor/mglaman/phpstan-drupal/src/Drupal/ServiceMap.php on line 65

There is no issue on ~1.1 version, only on 1.2.0+

Code snippet that reproduces the problem

example.services.yml:

  Drupal\phpstan_example\Foo:
  Drupal\phpstan_example\BarInterface: '@Drupal\phpstan_example\Bar'
  Drupal\phpstan_example\Bar:
    decorates: Drupal\phpstan_example\Foo

This definition is valid and works, but PHPStan fails on them. If I add explicit class for decorator, it would work:

  Drupal\phpstan_example\Foo:
  Drupal\phpstan_example\BarInterface: '@Drupal\phpstan_example\Bar'
  Drupal\phpstan_example\Bar:
+   class: Drupal\phpstan_example\Bar
    decorates: Drupal\phpstan_example\Foo

Looks like related to #578, but still an issue.

phpstan_example.zip — an example module with a problem.


Basically, I have an interface defined in services.yml file which is an alias for a specific class that implements that interface. But that class is another service which decorates contrib one. In example above: Foo is contrib services, defined in third-party code, BarInterface — an interface for my decorator used as an alias for its definition and Bar is an actual decorator.

Niklan avatar Dec 19 '23 11:12 Niklan