Testing your Drupal code base for deprecated code usage with PHPStan

Automating deprecation testing for Drupal core


Since writing this post, using PHPStan to find deprecations in Drupal has taken off. You can go ahead and follow the rest of the blog post, but I would like to reference a tool called Drupal Check. This is a standalone binary (PHP Phar) that will run deprecation checks without needing to install anything with Composer. You can find it, and instructions, over here: https://github.com/mglaman/drupal-check

Testing your Drupal codebase for deprecations

You can test your code, too! Whether you want to test your client’s codebase or your own contribs, it is easy to set up deprecation code testing.

composer require mglaman/phpstan-drupal phpstan/phpstan-deprecation-rules --dev
parameters: customRulesetUsed: true reportUnmatchedIgnoredErrors: false # Ignore phpstan-drupal extension's rules. 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.#' includes: - vendor/mglaman/phpstan-drupal/extension.neon - vendor/phpstan/phpstan-deprecation-rules/rules.neon

Running deprecation tests on TravisCI for contrib

I set up a sample repository that runs Drupal Commerce and our dependencies against PHPStan for deprecation checks. You can find it here: https://github.com/mglaman/commerce-deprecation-testing

language: php dist: trusty sudo: false cache: directories: - $HOME/.composer/cache/files - $HOME/.composer/cache/repo - $TMPDIR/phpstan/cache php: - 7.2 env: matrix: - ANALYZE=address - ANALYZE=commerce - ANALYZE=entity - ANALYZE=entity_reference_revisions - ANALYZE=inline_entity_form - ANALYZE=profile - ANALYZE=state_machine before_install: - echo 'sendmail_path = /bin/true' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - echo 'memory_limit = -1' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - echo "extension = apcu.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - phpenv config-rm xdebug.ini install: - composer global require "hirak/prestissimo:^0.3" - composer install -n --prefer-dist --no-suggest script: - ./vendor/bin/phpstan analyse web/modules/contrib/$ANALYZE



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matt Glaman

Matt Glaman

Open source developer, working with Drupal and building Drupal Commerce.