Skip to content

WP hooks callback issues #29

@gmazzap

Description

@gmazzap

The check for type declarations has an handy exceptions that don't require type declaration when the callback is used for a WordPress hook.

It works when a closure is directly added to an hook, e.g.:

add_action('init', function () { /* ... */ });

or when the @wp-hook annotation is used:

/**
 * @wp-hook init
 */
function fooBar() { /* ... */ }

However, there are two issues currently:

  1. Closures are not recognized when they are static
  2. Class methods are not recognized if not public

Regarding second point, it is true that only public methods can be used directly in a hook, but it is quite common to have a closure that acts as a proxy to a private method, something like:

add_action('init', function ($args) { $this->doSomething($args) });

This allows to use separate methods, without exposing them as public. However, in the example above the closure will not be required to have type, but the doSomething method will be required, even if the @wp-hook annotation is used, so only chance is to either make it public (at that point having the closure does not make sense anymore) or manually excluding the type declaration check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions