Getting started

Last updated on
25 April 2025

This documentation needs review. See "Help improve this page" in the sidebar.

To install PHPStan, you must use Composer.

The following command will install PHPStan and necessary extensions as development dependencies (this needs to be run in the base folder of your project).

composer require --dev phpstan/phpstan \
  phpstan/extension-installer \
  mglaman/phpstan-drupal \
  phpstan/phpstan-deprecation-rules

The mglaman/phpstan-drupal allows PHPStan to understand how to read code in a Drupal codebase and understand how its dynamic return types operate.

The phpstan/extension-installer package autoconfigures PHPStan to load the phpstan-drupal package and phpstan/phpstan-deprecation-rules. This is the package that enables reporting usages of deprecated code.

Next, you need to create a phpstan.neon file, and save it in the base folder of your project. This specifies the analysis level and paths you want to scan. The following assumes you'd like to analyze custom modules in your Drupal site.

parameters:
    level: 1
    paths:
        - web/modules/custom

You can then run PHPStan and begin analyzing your codebase!

php vendor/bin/phpstan.phar

If you run out of memory, try using a higher memory limit as documented https://phpstan.org/user-guide/command-line-usage#--memory-limit

php vendor/bin/phpstan.phar --memory-limit=256M

Using ddev to run inspection.

To run inspection on specific module using ddev:

ddev exec vendor/bin/phpstan analyze web/modules/custom/my-module -l 5

-l 5  -  Level of rule options - the higher the stricter.

Help improve this page

Page status: Needs review

You can: