szepeviktor/phpstan-wordpress
Composer 安装命令:
composer require --dev szepeviktor/phpstan-wordpress
包简介
WordPress extensions for PHPStan
README 文档
README
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!
WordPress Extensions for PHPStan
Static analysis for the WordPress ecosystem.
Features
- Enables PHPStan to analyze WordPress plugins and themes.
- Loads the
php-stubs/wordpress-stubspackage. - 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()anddo_action()calls, treating the type of the first@paramas 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:
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:
includes: - vendor/szepeviktor/phpstan-wordpress/extension.neon
Configuration
No 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!
Usage
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
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:
/** * 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) ...
- functions:
- If you need robust code try avoiding all kinds of type juggling (e.g.
ifneeds a boolean), see Variable handling functions - Avoid enabling
exit_errorinWP_CLI::launchorWP_CLI::runcommandfor 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.
sed -i -e 's#function is_gd_image#function __is_gd_image#' vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
szepeviktor/phpstan-wordpress 适用场景与选型建议
szepeviktor/phpstan-wordpress 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.03M 次下载、GitHub Stars 达 328, 最近一次更新时间为 2019 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「static analysis」 「code analysis」 「PHPStan」 「code analyse」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 szepeviktor/phpstan-wordpress 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 szepeviktor/phpstan-wordpress 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 szepeviktor/phpstan-wordpress 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
Sentiment analysis library for PHP.
Database Standardization and Analysis Tool for Laravel
Enforce architecture by defining modules with allowed dependencies. Detects forbidden, uncovered, missing and unused module dependencies in PHP projects.
Arc5 provides a set of static classes for the Mvc5 Framework
统计信息
- 总下载量: 10.03M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 330
- 点击次数: 22
- 依赖项目数: 847
- 推荐数: 33
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-04