Perform static analysis of Drupal PHP code with phpstan-drupal.

Overview

CircleCI

Perform static analysis of Drupal PHP code with PHPStan and PHPStan-Drupal on Drupal using PHP 8.

For example:

docker run --rm \
  -v $(pwd)/example01/modules_i_want_to_test:/var/www/html/modules/custom \
  dcycle/phpstan-drupal:4 /var/www/html/modules/custom
docker run --rm \
  -v $(pwd)/example02/modules_i_want_to_test:/var/www/html/modules/custom \
  dcycle/phpstan-drupal:4 /var/www/html/modules/custom
docker run --rm \
  -v $(pwd)/example03/modules_i_want_to_test:/var/www/html/modules/custom \
  -v $(pwd)/example03/phpstan-drupal:/phpstan-drupal \
  dcycle/phpstan-drupal:4 /var/www/html/modules/custom
docker run --rm \
  -v $(pwd)/example04/some_module:/var/www/html/modules/custom/some_module \
  -v $(pwd)/example04/phpstan-drupal:/phpstan-drupal \
  dcycle/phpstan-drupal:4 /var/www/html/modules/custom

Ignoring a single line of code

PHPStan by itself supports ignoring an error on a single line, like this:

// @phpstan-ignore-next-line
...some offending code...;

or even:

// Whatever whatever @phpstan-ignore-next-line whatever whatever.
...some offending code...;

Obviously, we generally want to fix the underlying problem, but if for whatever reason you need to ignore an error, you can now do so.

Version history and migrating from one version to another

  • dcycle/phpstan-drupal:4 is based on Drupal 9, and PHP 8; Drupal 8-specific code will trigger errors; the deprecated autoload_files has been replaced with bootstrapFiles in docker-resources/phpstan.neon. Version 4 now also includes functionalty from dcycle/docker-drupal-check.
  • dcycle/phpstan-drupal:3 is based on Drupal 9; Drupal 8-specific code might trigger errors.
  • dcycle/phpstan-drupal:2 uses @phpstan-ignore-next-line to ignore the next line of code.
  • dcycle/phpstan-drupal:1 uses phpstan:ignoreError to ignore the next line of code.

Custom config file

If you need a custom config file, for example if you want a different level, or to tell PHPStan to ignore certain files, you can do so by including the provided config file. See example05 for details; it can be run using:

docker run --rm \
  -v $(pwd)/example05/modules_i_want_to_test:/var/www/html/modules/custom \
  dcycle/phpstan-drupal:4 /var/www/html/modules/custom \
  -c /var/www/html/modules/custom/phpstan.neon

If you look at the custom config file in example05, it looks like this:

# See https://github.com/dcycle/docker-phpstan-drupal/blob/master/README.md#custom-config-file
parameters:
  excludePaths:
    - */tests/*
includes:
  - /var/www/html/phpstan.neon

This tells PHPStan that we want our custom configuration exclude paths like */tests/* from PHPStan analysis, and use the default /var/www/html/phpstan.neon for everything else. /var/www/html/phpstan.neon is not in your own code, it is in the dcycle/phpstan-drupal container. Its contents can be found here.

Deprecation testing

In the ./docker-resources/composer.json file you will find the following requirement as per the PHPStan-Drpual documentation:

phpstan/phpstan-deprecation-rules

In the ./docker-resources/phpstan.neon file you will find the following include, again as per the documentaiton:

vendor/phpstan/phpstan-deprecation-rules/rules.neon

This means that when you have deprecated code in your codebase, it will be detected.

Troubleshooting

Out of memory errors can be fixed by adding --memory-limit=-1 to the end of your call, for example:

docker run --rm \
  -v $(pwd)/example01/modules_i_want_to_test:/var/www/html/modules/custom \
  dcycle/phpstan-drupal:4 /var/www/html/modules/custom \
  --memory-limit=-1

Speed increase if using the M1 chip

Resources

You might also like...
⚗️ Adds code analysis to Laravel improving developer productivity and code quality.
⚗️ Adds code analysis to Laravel improving developer productivity and code quality.

⚗️ About Larastan Larastan was created by Can Vural and Nuno Maduro, got artwork designed by @Caneco, is maintained by Can Vural, Nuno Maduro, and Vik

Allows installing Drupal extensions event if not compatible with installed drupal/core package

mglaman/composer-drupal-lenient Lenient with it, Drupal 10 with it. Why? The Drupal community introduced a lenient Composer facade that modified the d

PHPStan extension to support #[Readonly] constructor properties

icanhazstring/phpstan-readonly-property Support #[Readonly] promoted constructor properties for PHPStan. This library is used to have a full transitio

Analyzes PHPStan baseline files and creates aggregated error trend-reports

Analyzes phpstan baseline files Analyzes PHPStan baseline files and creates aggregated error trend-reports.

Magento specific extension for phpstan

bitexpert/phpstan-magento This package provides some additional features for PHPStan to make it work for Magento 2 projects. Installation The preferre

Sandbox project for the PHPStan workshop

Sandbox project for a PHPStan workshop Installation Requirements Docker Engine Docker Compose Git Bash Getting started Clone this repository (git clon

A PHPStan package that supports working with Extbase

PHPStan for Extbase This package provides a couple of stubs and services to make your life easier when working with PHPStan and Extbase. Examples clas

Custom PHPStan rules

phpstan-rules Provides additional rules for phpstan/phpstan. Installation Run $ composer require --dev alister/phpstan-rules Usage All of the rules pr

The main scope of this extension is to help phpstan to detect the type of object after the Assert\Assertion validation.

PHPStan beberlei/assert extension PHPStan beberlei/assert Description The main scope of this extension is to help phpstan to detect the type of object

Comments
Owner
Dcycle
Specializing in continuous integration and automated tests for Drupal projects.
Dcycle
The SensioLabs DeprecationDetector runs a static code analysis against your project's source code to find usages of deprecated methods, classes and interfaces

SensioLabs DeprecationDetector CAUTION: This package is abandoned and will no longer receive any updates. The SensioLabs DeprecationDetector runs a st

QOSSMIC GmbH 390 Sep 26, 2022
A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

Felix Becker 1.1k Sep 9, 2022
An extension for PHPStan for adding analysis for PHP Language Extensions.

PHPStan PHP Language Extensions (currently in BETA) This is an extension for PHPStan for adding analysis for PHP Language Extensions. Language feature

Dave Liddament 8 Jun 27, 2022
Docker image that provides static analysis tools for PHP

Static Analysis Tools for PHP Docker image providing static analysis tools for PHP. The list of available tools and the installer are actually managed

Jakub Zalas 1.1k Sep 26, 2022
Beautiful and understandable static analysis tool for PHP

PhpMetrics PhpMetrics provides metrics about PHP project and classes, with beautiful and readable HTML report. Documentation | Twitter | Contributing

PhpMetrics 2.3k Sep 16, 2022
Find undefined and unused variables with the PHP Codesniffer static analysis tool.

PHP_CodeSniffer VariableAnalysis Plugin for PHP_CodeSniffer static analysis tool that adds analysis of problematic variable use. Warns if variables ar

Payton Swick 113 Sep 23, 2022
Attributes to define PHP language extensions (to be enforced by static analysis)

PHP Language Extensions (currently in BETA) This library provides attributes for extending the PHP language (e.g. adding package visibility). The inte

Dave Liddament 44 Sep 20, 2022
Tool based on deployer.org to perform zero downtime deployments of Magento 2 projects

Magento 2 Deployer Plus Reliable fully-automated deployments tool for Magento 2. Zero downtime deployments on Magento versions >= 2.2 Automating your

Juan Alonso 193 Apr 28, 2022
Dockerise Symfony Application (Symfony 6 + Clean Architecture+ DDD+ CQRS + Docker + Xdebug + PHPUnit + Doctrine ORM + JWT Auth + Static analysis)

Symfony Dockerise Symfony Application Install Docker Install Docker Compose Docker PHP & Nginx Create Symfony Application Debugging Install Xdebug Con

null 29 Sep 27, 2022
WooCommerce function and class declaration stubs for static analysis.

WooCommerce Stubs This package provides stub declarations for WooCommerce functions, classes and interfaces. These stubs can help plugin and theme dev

PHP Stubs Library 51 Sep 21, 2022