You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHPStan检测WordPress Plugin Boilerplate代码时add_filter/add_action回调参数类型不匹配错误的解决方法

PHPStan检测WordPress Plugin Boilerplate代码时add_filter/add_action回调参数类型不匹配错误的解决方法

嘿,我来帮你搞定这个PHPStan的类型错误问题!当你把PHPStan的规则等级升到7后,它会对callable类型做更严格的校验,这就是为什么之前没问题的代码现在报错了——因为PHPStan没法确定你传给add_filter和add_action的array($hook['component'], $hook['callback'])是一个有效的可调用结构。

咱们可以通过精确化类型注释的方式来解决这个问题,让PHPStan清楚知道你的$actions和$filters数组里每个元素的具体结构,这样它就能识别出那组数组是合法的callable了。

具体解决步骤

1. 更新$actions和$filters的类型注释

把原来的mixed[]替换成更具体的**数组形状(array shape)**定义,明确每个字段的类型:

/**
 * The array of actions registered with WordPress.
 *
 * @since    1.0.0
 * @access   protected
 * @var      array<int, array{
 *     hook: string,
 *     component: object,
 *     callback: string,
 *     priority: int,
 *     accepted_args: int
 * }>    $actions    The actions registered with WordPress to fire when the plugin loads.
 */
protected $actions;

/**
 * The array of filters registered with WordPress.
 *
 * @since    1.0.0
 * @access   protected
 * @var      array<int, array{
 *     hook: string,
 *     component: object,
 *     callback: string,
 *     priority: int,
 *     accepted_args: int
 * }>    $filters    The filters registered with WordPress to fire when the plugin loads.
 */
protected $filters;

2. 更新add方法的参数和返回值类型

同样,把add方法里的mixed[]也替换成对应的数组形状类型,保持类型一致:

/**
 * A utility function that is used to register the actions and hooks into a single
 * collection.
 *
 * @since    1.0.0
 * @access   private
 * @param    array<int, array{
 *     hook: string,
 *     component: object,
 *     callback: string,
 *     priority: int,
 *     accepted_args: int
 * }>                $hooks            The collection of hooks that is being registered (that is, actions or filters).
 * @param    string               $hook             The name of the WordPress filter that is being registered.
 * @param    object               $component        A reference to the instance of the object on which the filter is defined.
 * @param    string               $callback         The name of the function definition on the $component.
 * @param    int                  $priority         The priority at which the function should be fired.
 * @param    int                  $accepted_args    The number of arguments that should be passed to the $callback.
 * @return   array<int, array{
 *     hook: string,
 *     component: object,
 *     callback: string,
 *     priority: int,
 *     accepted_args: int
 * }>                                  The collection of actions and filters registered with WordPress.
 */
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
    $hooks[] = array(
        'hook'          => $hook,
        'component'     => $component,
        'callback'      => $callback,
        'priority'      => $priority,
        'accepted_args' => $accepted_args
    );

    return $hooks;
}

3. (可选)安装PHPStan WordPress扩展

如果你经常开发WordPress插件,推荐安装phpstan/wordpress扩展,它专门适配了WordPress的函数和类型系统,能减少很多不必要的类型错误提示:

composer require --dev phpstan/wordpress

安装后,PHPStan会更准确地识别add_action和add_filter的参数要求,配合上面的类型注释,就能彻底解决这个问题啦!

为什么这样有效?

之前你用mixed[]虽然解决了“没有指定值类型”的错误,但这个类型太宽泛了——PHPStan根本不知道数组里的component是一个对象,callback是这个对象上的方法名。通过数组形状定义,你告诉了PHPStan每个字段的精确类型,它就能验证array($component, $callback)是符合WordPress要求的callable格式了。

备注:内容来源于stack exchange,提问作者MasterRed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 14:49:09