Getting started
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-rulesThe 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/customYou can then run PHPStan and begin analyzing your codebase!
php vendor/bin/phpstan.pharIf 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=256MUsing 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.
Using DDEV for contributed Drupal development
If you're using PHPStan for contributed Drupal development, you may wish to look at DDEV Drupal Contrib, a DDEV add-on that ships with PHPStan support via a ddev phpstan command.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion