wp-phpstan
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWP PHPStan
WP PHPStan
When to use
适用场景
Use this skill when working on PHPStan in a WordPress codebase, for example:
- setting up or updating /
phpstan.neonphpstan.neon.dist - generating or updating
phpstan-baseline.neon - fixing PHPStan errors via WordPress-friendly PHPDoc (REST requests, hooks, query results)
- handling third-party plugin/theme classes safely (stubs/autoload/targeted ignores)
当你在WordPress代码库中处理PHPStan相关工作时,可以使用本技能,例如:
- 配置或更新/
phpstan.neon文件phpstan.neon.dist - 生成或更新文件
phpstan-baseline.neon - 通过WordPress友好的PHPDoc(REST请求、钩子、查询结果)修复PHPStan错误
- 安全处理第三方插件/主题类(存根/自动加载/定向忽略)
Inputs required
所需输入
- output (run first if you haven't)
wp-project-triage - Whether adding/updating Composer dev dependencies is allowed (stubs).
- Whether changing the baseline is allowed for this task.
- 的输出结果(如果尚未运行,请先执行该工具)
wp-project-triage - 是否允许添加/更新Composer开发依赖(存包)
- 是否允许本次任务修改基准文件
Procedure
操作步骤
0) Discover PHPStan entrypoints (deterministic)
0) 发现PHPStan入口点(确定性步骤)
- Inspect PHPStan setup (config, baseline, scripts):
node skills/wp-phpstan/scripts/phpstan_inspect.mjs
Prefer the repo’s existing script (e.g. ) when present.
composercomposer run phpstan- 检查PHPStan配置(配置文件、基准文件、脚本):
- 执行
node skills/wp-phpstan/scripts/phpstan_inspect.mjs
- 执行
如果仓库中已有脚本(例如),优先使用该脚本。
composercomposer run phpstan1) Ensure WordPress core stubs are loaded
1) 确保加载WordPress核心存根
szepeviktor/phpstan-wordpressphp-stubs/wordpress-stubs- Confirm the package is installed (see in the inspect report).
composer.dependencies - Ensure the PHPStan config references the stubs (see ).
references/third-party-classes.md
对于大多数WordPress插件/主题仓库,或是必需的。如果没有它们,会出现大量关于未知WordPress核心函数的错误。
szepeviktor/phpstan-wordpressphp-stubs/wordpress-stubs- 确认该包已安装(查看检查报告中的)。
composer.dependencies - 确保PHPStan配置文件引用了这些存根(参考)。
references/third-party-classes.md
2) Ensure a sane phpstan.neon
for WordPress projects
phpstan.neon2) 为WordPress项目配置合理的phpstan.neon
phpstan.neon- Keep focused on first-party code (plugin/theme directories).
paths - Exclude generated and vendored code (,
vendor/, build artifacts, tests unless explicitly analyzed).node_modules/ - Keep entries narrow and documented.
ignoreErrors
See:
references/configuration.md
- 确保仅聚焦于自有代码(插件/主题目录)。
paths - 排除生成的代码和依赖代码(、
vendor/、构建产物、测试代码,除非明确需要分析)。node_modules/ - 保持条目范围明确并添加文档说明。
ignoreErrors
参考:
references/configuration.md
3) Fix errors with WordPress-specific typing (preferred)
3) 使用WordPress特定类型修复错误(优先方案)
Prefer correcting types over ignoring errors. Common WP patterns that need help:
- REST endpoints: type request parameters using
WP_REST_Request<...> - Hook callbacks: add accurate types for callback args
@param - Database results and iterables: use array shapes or object shapes for query results
- Action Scheduler: type array shapes for job callbacks
$args
See:
references/wordpress-annotations.md
优先修正类型而非忽略错误。常见的需要处理的WordPress模式:
- REST端点:使用对请求参数进行类型标注
WP_REST_Request<...> - 钩子回调:为回调参数添加准确的类型
@param - 数据库结果和可迭代对象:为查询结果使用数组结构或对象结构类型
- Action Scheduler:为任务回调的数组添加结构类型
$args
参考:
references/wordpress-annotations.md
4) Handle third-party plugin/theme classes (only when needed)
4) 处理第三方插件/主题类(仅在需要时)
When integrating with plugins/themes not present in the analysis environment:
- First, confirm the dependency is real (installed/required).
- Prefer plugin-specific stubs already used in the repo (common examples: ,
php-stubs/woocommerce-stubs).php-stubs/acf-pro-stubs - If PHPStan still cannot resolve classes, add targeted patterns for the specific vendor prefix.
ignoreErrors
See:
references/third-party-classes.md
当与分析环境中不存在的插件/主题集成时:
- 首先,确认该依赖是真实存在的(已安装/必需的)。
- 优先使用仓库中已有的插件特定存根(常见示例:、
php-stubs/woocommerce-stubs)。php-stubs/acf-pro-stubs - 如果PHPStan仍无法解析类,为特定的供应商前缀添加定向的规则。
ignoreErrors
参考:
references/third-party-classes.md
5) Baseline management (use as a migration tool, not a trash bin)
5) 基准文件管理(作为迁移工具使用,而非“垃圾桶”)
- Generate a baseline once for legacy code, then reduce it over time.
- Do not “baseline” newly introduced errors.
See:
references/configuration.md
- 仅为遗留代码生成一次基准文件,之后逐步缩减其内容。
- 不要将新引入的错误加入基准文件。
参考:
references/configuration.md
Verification
验证步骤
- Run PHPStan using the discovered command (or
composer run ...).vendor/bin/phpstan analyse - Confirm the baseline file (if used) is included and didn’t grow unexpectedly.
- Re-run after changing to ensure patterns are not masking unrelated issues.
ignoreErrors
- 使用发现的命令运行PHPStan(或
composer run ...)。vendor/bin/phpstan analyse - 确认基准文件(如果使用)已被包含,且内容没有意外增加。
- 修改后重新运行,确保规则没有掩盖无关问题。
ignoreErrors
Failure modes / debugging
故障模式与调试
- “Class not found”:
- confirm autoloading/stubs, or add a narrow ignore pattern
- Huge error counts after enabling PHPStan:
- reduce , add
paths, start at a lower level, then ratchet upexcludePaths
- reduce
- Inconsistent types around hooks / REST params:
- add explicit PHPDoc (see references) rather than runtime guards
- “类未找到”:
- 确认自动加载/存根,或添加范围明确的忽略规则
- 启用PHPStan后错误数量过多:
- 缩小范围,添加
paths,从较低分析级别开始,逐步提升excludePaths
- 缩小
- 钩子/REST参数周围类型不一致:
- 添加明确的PHPDoc(参考相关文档),而非运行时检查
Escalation
升级处理
- If a type depends on a third-party plugin API you can’t confirm, ask for the dependency version or source before inventing types.
- If fixing requires adding new Composer dependencies (stubs/extensions), confirm it with the user first.
- 如果类型依赖于你无法确认的第三方插件API,在自定义类型前先询问依赖版本或来源。
- 如果修复需要添加新的Composer依赖(存根/扩展),请先与用户确认。