wp-media/phpstan-wp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

wp-media/phpstan-wp

Composer 安装命令:

composer require --dev wp-media/phpstan-wp

包简介

WP Media extensions for PHPStan

README 文档

README

A PHPStan extension providing custom rules for WordPress projects, specifically designed for WP Media projects

Overview

This extension adds static analysis rules to ensure proper implementation of WordPress hooks through subscriber classes. It validates that your event subscribers follow best practices for WordPress actions and filters.

Custom Rules

SubscriberCallbackRule

Validates get_subscribed_events() implementations in classes that implement subscriber interfaces. This rule ensures:

  1. Required Annotations: All hook registrations must have @action or @filter PHPDoc annotations
  2. Filter Return Types: Filter callbacks must return non-void values
  3. Action Return Types: Action callbacks must return void
  4. Parameter Count Validation: When accepted_args is specified, the callback method must have the correct number of parameters

Supported Subscriber Interfaces

  • WP_Rocket\Event_Management\Subscriber_Interface
  • Imagify\EventManagement\SubscriberInterface
  • WPMedia\EventManager\SubscriberInterface
  • WPMedia\BackWPup\EventManagement\SubscriberInterface

Installation

Install via Composer:

composer require --dev wp-media/phpstan-wp

If you have phpstan/extension-installer installed, the extension will be automatically registered.

Manual Configuration

If you don't use the extension installer, add this to your phpstan.neon or phpstan.neon.dist:

includes:
    - vendor/wp-media/phpstan-wp/extension.neon

Usage

Valid Subscriber Example

<?php

use WPMedia\EventManager\SubscriberInterface;

class MySubscriber implements SubscriberInterface
{
    public function get_subscribed_events(): array
    {
        return [
            // @filter - Filters must return a value
            'the_content' => 'filterContent',
            
            // @action - Actions must return void
            'init' => 'onInit',
            
            // @filter - With priority
            'the_title' => ['filterTitle', 10],
            
            // @action - With priority and accepted_args
            'save_post' => ['onSavePost', 10, 2],
        ];
    }

    /**
     * Filter callback - returns modified content
     */
    public function filterContent(string $content): string
    {
        return $content . ' - filtered';
    }

    /**
     * Action callback - returns void
     */
    public function onInit(): void
    {
        // Perform initialization
    }

    /**
     * Filter callback with priority
     */
    public function filterTitle(string $title): string
    {
        return strtoupper($title);
    }

    /**
     * Action callback with 2 parameters (matching accepted_args)
     */
    public function onSavePost(int $postId, \WP_Post $post): void
    {
        // Handle post save
    }
}

Common Errors and How to Fix Them

Missing Annotation

Invalid - Missing @filter or @action annotation:

public function get_subscribed_events(): array
{
    return [
        'the_content' => 'filterContent', // ERROR: Missing annotation
    ];
}

Valid - Add the annotation before the array element:

public function get_subscribed_events(): array
{
    return [
        // @filter
        'the_content' => 'filterContent',
    ];
}

Filter with Void Return Type

Invalid - Filter callbacks must return a value:

public function get_subscribed_events(): array
{
    return [
        // @filter
        'the_content' => 'filterContent',
    ];
}

public function filterContent(string $content): void // ERROR: Returns void
{
    echo $content;
}

Valid - Filter callbacks must return the filtered value:

public function get_subscribed_events(): array
{
    return [
        // @filter
        'the_content' => 'filterContent',
    ];
}

public function filterContent(string $content): string
{
    return $content . ' - filtered';
}

Action with Non-Void Return Type

Invalid - Action callbacks must return void:

public function get_subscribed_events(): array
{
    return [
        // @action
        'init' => 'onInit',
    ];
}

public function onInit(): int // ERROR: Returns non-void
{
    return 42;
}

Valid - Action callbacks must return void:

public function get_subscribed_events(): array
{
    return [
        // @action
        'init' => 'onInit',
    ];
}

public function onInit(): void
{
    // Perform action
}

Parameter Count Mismatch

Invalid - Parameter count doesn't match accepted_args:

public function get_subscribed_events(): array
{
    return [
        // @action - accepted_args=3 but method has only 1 parameter
        'save_post' => ['onSavePost', 10, 3], // ERROR
    ];
}

public function onSavePost(int $postId): void
{
    // Missing 2 parameters
}

Valid - Parameter count matches accepted_args:

public function get_subscribed_events(): array
{
    return [
        // @action
        'save_post' => ['onSavePost', 10, 3],
    ];
}

public function onSavePost(int $postId, \WP_Post $post, bool $update): void
{
    // Now has 3 parameters matching accepted_args
}

Development

Running Tests

composer test-unit

Code Style

Check code style:

composer phpcs

Fix code style issues:

composer phpcbf

Requirements

  • PHP 7.4 or higher
  • PHPStan 2.0 or higher

License

GPL-3.0-or-later

Contributing

Contributions are welcome! Please ensure your code follows the WordPress Coding Standards and includes appropriate tests.

wp-media/phpstan-wp 适用场景与选型建议

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

它主要适用于以下技术方向: 「wordpress」 「static analysis」 「code analysis」 「PHPStan」 「code analyse」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 wp-media/phpstan-wp 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2026-03-10