 [ ← blog / drupal ](/) Jan 06, 2022  ·  \#drupal  ·  2 min read 

# Documenting PHPStan + Drupal!



In the early days of Drupal 9, most folks were only using PHPStan on their Drupal sites via [drupal-check](https://github.com/mglaman/drupal-check) for deprecation checks. I am excited to see more folks using PHPStan directly with [phpstan-drupal](https://github.com/mglaman/phpstan-drupal) to perform static analysis and deprecation checks. However, folks are starting to hit some of the DrupalWTF's when performing static analysis on a magical code base

MAGIC?! Yes, Drupal has a lot of magical return types that are not typed. Inside phpstan-drupal, we try to help out as much as possible, but we cannot know everything.

Take a typical implementation of `hook_ENTITY_TYPE_insert`.

```php
function mymodule_node_insert(\Drupal\Entity\EntityInterface $node) {
  // Do some node stuff
}
```

Notice something? The `$node` parameter is type hinted at `EntityInterface`. This is true because `Node` implements `NodeInterface,` which extends `EntityInterface`. However, anything on the `NodeInterface` (or its other parent interfaces) is technically unavailable.

Your hook *should* be clear on the entity it receives.

```php
function mymodule_node_insert(\Drupal\node\NodeInterface $node) {
  // Do some node stuff
}
```

Then PHPStan will understand what kind of object `$node` is.

The problem I am facing is that there are *many more* of these support requests coming in, saying that static analysis is broken. There hasn't been a user guide for phpstan-drupal. A significant part of that is because documentation collaboration for projects on GitHub is horrible. Wikis are either public or modified by contributors, and contributors can manage code. No thanks, GitHub.

But! We do have Drupal.org. I have created a new [PHPStan guide under the Developer Tools section](https://www.drupal.org/docs/develop/development-tools/phpstan) to document Drupal + PHPStan. I will do my best to begin adding information here. That would be amazing if the community would like to help step in with flushing out tips for type hinting. I am mostly happy to have somewhere to link folks now.

 [PHPStan

PHPStan is a static analysis tool that allows you to find bugs in your codebase without running the code. The phpstan-drupal extension for PHPStan makes it possible to perform static analysis of Drupal code.

https://www.drupal.org/docs/develop/development-tools/phpstan

 

 ![PHPStan](https://www.drupal.org/files/styles/drupalorg_user_picture/public/user-pictures/picture-2416470-1526187268.jpg?itok=rPkQQs9O) 

 

 

 ](https://www.drupal.org/docs/develop/development-tools/phpstan) 

The other fantastic news is that folks have been pushing **hard** [to get Drupal core running against PHPStan at level 0](https://www.drupal.org/project/drupal/issues/3178534). So the documentation will help out with Drupal core as well.

 

  ![](/themes/custom/mglaman/images/headshot.webp)Thanks for reading

I write about Drupal, PHP, and the quiet infrastructure behind large sites — caching, config, and testing. New posts land every week or two.

 [Subscribe via RSS →](/rss.xml) [About me →](/about-me) 

 



 

  [← Older

Ensuring Drupal route lookups by path are cached by domain

 ](/blog/ensuring-drupal-route-lookups-path-are-cached-domain) [Newer →

hook_update_N or hook_post_update_NAME

 ](/blog/hookupdaten-or-hookpostupdatename)