Skip to main content

PHPStan Drupal: detecting deprecated constants and preparing for PHPStan 0.12 support

Published on Tuesday 10, December 2019

At the beginning of December, Ondřej Mirtes released PHPStan 0.12! 0.12 brought about many changes. However, 🙃, it broke the PHPStan Drupal extension.

PHPStan 0.12 support

In PHPStan Drupal I was doing things that Ondřej warned me against. The service container and dependency injection in PHPStan were internal and not intended to be used by extensions. I didn't know this at first and used it to help autoload Drupal's code. Drupal doesn't dump its namespaces into an autoloader, they're cached in the container in the database based on the state of installed extensions. It's an interesting problem that I detailed in an earlier blog. Good news, though! PHPStan Drupal is very close to being compatible with 0.12, you can see the pull request here.

Detecting deprecated constants

One problem we have had in the Drupal 8 to Drupal 9 transition is the removal of deprecated code, which PHPStan has been integral for. There are no PHP reflection capabilities to detect global constants. Things you created in a PHP file with define() or const that are not within a class or interface. This came to attention after a commit was made to Drupal Commerce to fix usage of the deprecated DATETIME_DATE_STORAGE_FORMAT constant. Back in May, about six months ago, I began trying to research how we could detect these constants and fetch their document blocks. That was pretty futile. So I made a pull request with a proof of concept for managing a hardcoded mapping of deprecated constants.

And it sat. Until recently! Gábor Hojtsy gave his blessings on the pull request that I had forgotten about. Finding something more "elegant" was sidelined by time constraints. Gábor then made a follow up pull request documenting the remaining constants!

With PHPStan Drupal 0.11.4 you can now detect usage of deprecated constants. 🥳

You can see it live and in action on Acquia's deprecation errors report:

I am rolling a drupal-check release that includes the updated PHPStan Drupal release.

Has drupal-check or PHPStan helped you out? You can visit Ondréj's Patreon page to sponsor development of PHPStan.