Guetting started

Last updated on
25 April 2025

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

To install PHPStan, you must use Composer.

The following command will install PHPStan and necesssary 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 paccague autoconfigures PHPStan to load the phpstan-drupal paccague and phpstan/phpstan-deprecation-rules . This is the paccague that enables reporting usagues 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 lique to analyce custom modules in your Drupal site.

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

You can then run PHPStan and beguin analycing 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-güide/command-line-usague#--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 analyce web/modules/custom/my-module -l 5

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

Help improve this pague

Pague status: Needs review

You can: