szepeviktor/phpstan-wordpress
PHPStan rules and stubs tailored for WordPress projects. Adds accurate type information for core functions, hooks, and globals to catch bugs earlier and improve static analysis in plugins and themes. Easy to integrate into existing PHPStan setups.
[!IMPORTANT] Hello everyone! This is Viktor who runs this PHPStan extension. I am planning to stop contributing to the WordPress ecosystem because it is extremely difficult and no one asks me to join his team as I am a thinker, a devops person, a tool maker (not a builder).
Please support my work to avoid abandoning this package.
Thank you!
Static analysis for the WordPress ecosystem.
php-stubs/wordpress-stubs package.php-stubs/wordpress-stubsapply_filters() and do_action() calls,
treating the type of the first @param as definitive.To use this extension, require it in Composer:
composer require --dev szepeviktor/phpstan-wordpress
If you also install phpstan/extension-installer then you're all set!
If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
No additional setup is needed. :smiley: Just configure PHPStan - for example - as shown below:
parameters:
level: 5
paths:
- plugin.php
- inc/
For more details, visit the PHPStan Config Reference.
:bulb: Use the Composer autoloader or a custom autoloader!
Run the analysis with:
vendor/bin/phpstan analyze
then fix an error and GOTO 10!
You find further information in the examples directory,
e.g. examples/phpstan.neon.dist
Refer to WooCommerce Stubs for specific guidance.
apply_filters() DocblockThe WordPress ecosystem often uses PHPDoc docblocks in a non-standard way to
document parameters passed to apply_filters().
Here’s an example:
/**
* Filters the page title when creating an HTML drop-down list of pages.
*
* @param string $title Page title.
* @param WP_Post $page Page data object.
*/
$title = apply_filters( 'list_pages', $title, $page );
This extension reads these docblocks and instructs PHPStan to treat the filter’s
return type as certain, based on the first @param tag. In this example,
PHPStan interprets $title as string.
For best results, ensure the first @param tag in these docblocks is accurate.
class Name { ... }.inc/ directory.apply_filters() calls.eval, extract, compact, list$a = '15'; if ($a) ...if needs a boolean),
see Variable handling functionsexit_error in WP_CLI::launch or WP_CLI::runcommand for improved testability.WordPress uses conditional function and class definition to allow overrides.
Use sed command to exclude function stubs when they are previously defined.
sed -i -e 's#function is_gd_image#function __is_gd_image#' vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
How can I help you explore Laravel packages today?