WordPress/WordPress-Coding-Standards

Alert when add_filter is used with a function with type hinting

Opened this issue · 1 comments

Mte90 commented

Is your feature request related to a problem?

The issue is that type declaration in function/method when are used with filters can be a nightmare. Specifically because your code can get a fatal error if the other code from by plugins/themes doesn't pass as the documentation says.
So the culprit is you instead of the original author.

Describe the solution you'd like

After discussing on slack and PHPstan-wordpress the best solution is:

  • Get the function/method on add_filter
  • Check if has type declaration
  • In that case add an alert to the developer to avoid for those reason

The only issue is that PHPCS doesn't check multiple files so the function is not in the same file cannot do this check. Anyway usually filters are together with the function or code in the same file as "standard" so I think that this case is very minimal to worry about. It is better to start with this detection and see if improve the code quality of plugins etc before thinking to add this support on other tool like phpstan.

Additional context (optional)

jrfnl commented

Note for when writing this sniff:

  • This sniff should look for all *filter*() functions which take a $callback argument to find the method/function name to find in the current file. Calls to the *action*() hook functions can be safely ignored as those functions don't alter (or at least shouldn't alter) the original parameters.
    Ref: https://codex.wordpress.org/Plugin_API