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-wordpressIf 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.neonNo additional setup is needed. ? Just configure PHPStan - for example - as shown below:
parameters:
level: 5
paths:
- plugin.php
- inc/For more details, visit the PHPStan Config Reference.
Use the Composer autoloader or a custom autoloader!
Run the analysis with:
vendor/bin/phpstan analyzethen 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