定制 lucasbustamante/stubz 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lucasbustamante/stubz

Composer 安装命令:

composer require lucasbustamante/stubz

包简介

A command-line PHP stub generator using BetterReflection.

README 文档

README

PHPStan - Level 10 PHPUnit

Stubz for WordPress Plugins

Stubz is a command-line utility designed to generate PHP stub files specifically tailored for WordPress plugins, although it supports any PHP codebase. PHP stubs simplify static analysis by providing a lightweight representation of your plugin's classes, traits, interfaces, functions, and constants, excluding implementation logic.

Stubz was initially developed to support the Quality Insights Toolkit (QIT) at Automattic, helping to enhance the reliability and accuracy of static code analysis within WordPress plugin ecosystems.

WordPress operates as an event-driven framework, meaning many plugins define classes and functions within event callbacks (hooks). A common pattern in WordPress plugins that trips static code analysis involves defining classes inside action callbacks. For example:

add_action('plugins_loaded', function() {
    class MyPluginMainClass {
        // class logic
    }
});

In static analysis tools like PHPStan, there's no guarantee the plugins_loaded action will ever be invoked. As a result, the tool fails to recognize dynamically defined classes, producing false positives or "class not found" errors.

Stubz resolves this issue by "flattening" the code definitions. It moves class, function, and constant definitions out of callbacks, making them readily discoverable for static analysis.

Recommended Usage

Using QIT (WooCommerce Marketplace Extensions)

For WooCommerce extensions distributed via the WooCommerce.com Marketplace, the recommended approach is to use the PHPStan Managed Tests provided by QIT. QIT's managed PHPStan tests automatically leverage Stubz, greatly simplifying your workflow.

Using PHPStan Directly (General Plugins)

If you're working with general WordPress plugins, you can manually configure PHPStan to work with Stubz as follows:

PHPStan differentiates between two important concepts:

  • Scan: PHPStan scans files to discover classes, functions, constants, and other code structures.
  • Analyse: PHPStan analyses the scanned files for potential errors, type mismatches, incorrect method calls, and other code quality issues.

Configure PHPStan manually by:

  • Analysing your plugin's actual source code for code quality:
./vendor/bin/phpstan analyse ./src
  • Scanning the generated stubs to ensure dynamically defined entities are recognized:
parameters:
    scanDirectories:
        - ./src-stubs

This configuration ensures accurate static analysis while eliminating common WordPress-specific false positives.

Features

  • Generates stubs for classes, interfaces, traits, enums, functions, and constants.
  • Supports complex, nested WordPress-specific patterns.
  • Handles modern PHP features including:
    • Final & abstract classes and methods
    • Readonly properties (PHP 8.1+)
    • Enums (PHP 8.1+), including cases
    • Attributes (PHP 8+), with reflection on arguments
    • Intersection & union types, typed properties, constructor promotion
  • Exclude specific directories/files using --exclude <dir>.
  • Custom Finder mode (--finder <file.php>) for advanced queries.

Requirements

  • PHP 8.2 or later
  • Composer dependencies (roave/better-reflection, symfony/finder)

Quick Start Guide

  1. Install Stubz using Composer:

    composer require lucasbustamante/stubz --dev
  2. Generate stubs for your plugin:

    ./vendor/bin/stubz ./src ./src-stubs

Excluding Directories

Exclude directories like vendor, tests, or build artifacts:

./vendor/bin/stubz --exclude vendor --exclude tests ./src ./src-stubs

Using a Custom Finder

For more complex inclusion/exclusion logic, create a finder.php:

use Symfony\Component\Finder\Finder;

$finder = Finder::create()
    ->files()
    ->in(__DIR__ . '/src')
    ->name('*.php')
    ->exclude('tests');

return $finder;

Then run Stubz:

./vendor/bin/stubz --finder finder.php ./src-stubs

lucasbustamante/stubz 适用场景与选型建议

lucasbustamante/stubz 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.63k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lucasbustamante/stubz 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lucasbustamante/stubz 我们能提供哪些服务?
定制开发 / 二次开发

基于 lucasbustamante/stubz 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-01