百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

phpstan-wordpress

> 编程语言
开源

PHPStan QQ 的 WordPress 扩展名

330 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

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.

WordPress Extensions for PHPStan

Static analysis for the WordPress ecosystem.

  • PHPStan
  • WordPress

Features

  • Enables PHPStan to analyze WordPress plugins and themes.
  • Loads the php-stubs/wordpress-stubs package.
  • Provides dynamic return type extensions for functions that are not covered in php-stubs/wordpress-stubs
  • Defines some WordPress core constants.
  • Validates optional docblocks before apply_filters() and do_action() calls, treating the type of the first @param as definitive.

Requirements

  • PHPStan 2.0 or higher
  • PHP 7.4 or higher (tested up to PHP 8.3)

Installation

To use this extension, require it in Composer:

bash
composer require --dev szepeviktor/phpstan-wordpress

If you also install phpstan/extension-installer then you're all set!

Manual Installation

If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:

neon
includes:
    - vendor/szepeviktor/phpstan-wordpress/extension.neon

Configuration

No additional setup is needed. :smiley: Just configure PHPStan - for example - as shown below:

neon
parameters:
    level: 5
    paths:
        - plugin.php
        - inc/

For more details, visit the PHPStan Config Reference.

:bulb: Use the Composer autoloader or a custom autoloader!

Usage

Run the analysis with:

bash
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

Usage in WooCommerce Webshops

Refer to WooCommerce Stubs for specific guidance.

Usage of an apply_filters() Docblock

The WordPress ecosystem often uses PHPDoc docblocks in a non-standard way to document parameters passed to apply_filters(). Here’s an example:

php
/**
 * 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.

Make Your Code Testable

  • Write clean OOP code: 1 class per file, and no additional code outside class Name { ... }.
  • Use consistent class naming (WPCS or PSR-4) and store classes in a dedicated inc/ directory.
  • Add precise PHPDoc blocks to classes, properties, methods, functions, and apply_filters() calls.
  • Choose your main plugin file parts.
  • Avoid using core constants, use core functions.
  • Avoid bad PHP practices, such as:
    • functions: eval, extract, compact, list
    • type juggling: $a = '15'; if ($a) ...
  • If you need robust code try avoiding all kinds of type juggling (e.g. if needs a boolean), see Variable handling functions
  • Avoid enabling exit_error in WP_CLI::launch or WP_CLI::runcommand for improved testability.

Dirty Corner (FAQ)

WordPress uses conditional function and class definition to allow overrides. Use sed command to exclude function stubs when they are previously defined.

bash
sed -i -e 's#function is_gd_image#function __is_gd_image#' vendor/php-stubs/wordpress-stubs/wordpress-stubs.php

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPphpstanphpstan-extensionstatic-analysiswordpress

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言