Add static analysis with PHPStan to WordPress plugin development
PHPStan reads PHP code and finds bugs without running it: calling a method on something that can be null or a WP_Error, passing the wrong type to a function, using an undefined variable, a missing array key. It infers types from native declarations, docblocks and function signatures and checks every call and return against them. For WordPress code it needs to know the types of WordPress functions too, which is what the WordPress extension adds.
Problem: A plugin has an intermittent fatal error in production: get_post() returns null for an ID from a URL whose post was deleted, and the code reads a property of it without a check. It is hard to reproduce, because in testing the post always exists.
Solution: Add PHPStan with the szepeviktor/phpstan-wordpress extension, start at a moderate level with a baseline for the existing errors, raise the level until nullable returns are checked, and run it in CI.
Install
composer require --dev phpstan/phpstan szepeviktor/phpstan-wordpress phpstan/extension-installer
phpstan-wordpress brings the stubs of all WordPress functions and classes (get_post() returns WP_Post|null, get_term() returns WP_Term|WP_Error|null…) and knowledge of hooks such as apply_filters(). Without it PHPStan does not know WordPress at all and reports hundreds of “function not found” errors. The extension installer loads it automatically.
Configuration
phpstan.neon in the plugin root:
parameters:
level: 5
paths:
- my-plugin.php
- includes
excludePaths:
- vendor
- node_modules
Run it:
vendor/bin/phpstan analyse
Levels
PHPStan has levels from 0 to 9, each adding checks to the previous one. Level 0 finds unknown functions and classes and wrong argument counts; level 5 checks the types of arguments; level 6 requires types for everything; level 8 reports method calls and property access on nullable types; level 9 is strict about mixed.
The bug from the problem is found at level 8:
<?php
// Level 8: Cannot access property $post_title on WP_Post|null.
function myplugin_post_title( $id ) {
$post = get_post( $id );
return $post->post_title;
}
// Fixed
function myplugin_post_title_safe( $id ) {
$post = get_post( $id );
return $post instanceof WP_Post ? $post->post_title : '';
}
A similar case that a lower level already reports, because the result is passed to a function that expects a term:
<?php
$term = get_term( $term_id, 'category' );
// Parameter #1 $term of function get_term_link expects int|string|WP_Term, WP_Error|WP_Term|null given.
echo esc_url( get_term_link( $term ) );
// Fixed
$term = get_term( $term_id, 'category' );
if ( $term instanceof WP_Term ) {
echo esc_url( get_term_link( $term ) );
}
A baseline for existing code
On an existing plugin the first run at a high level gives a long list. Record it as a baseline and enforce only that no new errors appear:
vendor/bin/phpstan analyse --level=8 --generate-baseline
This writes phpstan-baseline.neon; add it to phpstan.neon with includes: [ phpstan-baseline.neon ]. From now on new code is checked strictly, and the old errors are fixed gradually. get_option() returns mixed, so values from it need a cast or a check before typed use; this is the most common entry in a WordPress baseline.
Docblocks for arrays and hooks
PHPStan reads generic array types from docblocks, which helps with filter callbacks:
<?php
/**
* @param array<string, mixed> $query_args
* @return array<string, mixed>
*/
function myplugin_filter_query( array $query_args ) {
if ( isset( $query_args['post_type'] ) && 'product' === $query_args['post_type'] ) {
$query_args['meta_key'] = '_featured';
}
return $query_args;
}
NOTE: PHPStan and tests catch different problems: PHPStan finds type and logic errors in code paths that tests never execute, tests check that the behavior is right. Both belong in CI, so a pull request with a new error cannot be merged. For tests see unit tests with PHPUnit.