Handling "Unsafe usage of new static()" PHPStan Errors: Four Solutions Explored

DrupalEasy

Drupal module developers often encounter the "Unsafe usage of new static()" PHPStan error, typically arising when injecting dependencies into a class using ContainerFactoryPluginInterface or ContainerInjectionInterface. In his comprehensive article on DrupalEasy, Michael Anello explores four potential solutions with their respective pros and cons. Anello discusses the options of ignoring the error, preventing class extension, changing "new static" to "new self," and making the constructor final. Each approach is thoroughly evaluated, highlighting its impact on code quality, extensibility, and potential limitations. Anello leans towards using the "final" keyword in most cases, acknowledging the trade-offs and the need for a contextual approach to resolving the PHPStan error effectively.  

Title: How best to handle "Unsafe usage of new static()" PHPStan errors
Byline: Michael Anello
Date of Publication: 13 December 2023
Organization: DrupalEasy
URL: https://www.drupaleasy.com/blogs/ultimike/2023/12/how-best-handle-unsafe-usage-new-static-phpstan-errors

Note: The vision of this web portal is to help promote news and stories around the Drupal community and promote and celebrate the people and organizations in the community. We strive to create and distribute our content based on these content policy. If you see any omission/variation on this please let us know in the comments below and we will try to address the issue as best we can.

Related Organizations

Advertisement Here

Upcoming Events

Advertisement Here