PHPStan QQ 的 WordPress 扩展名
[!IMPORTANT]
Need help?
I build and maintain reliable web applications, with a focus on PHP, WordPress, and software quality.
If this package helps your team, feel free to reach out for consulting, development work, or sponsorship.
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. :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 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暂无开放 Issues,或尚未同步最近议题。