WordPress / WordPress/WordPress-Coding-Standards

Alert when add_filter is used with a function with type hinting

Open
#2,014 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Focus: Code analysis Type: Enhancement
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating how PHPCS handles add_filter calls and function or method type declarations; the issue does not name specific files or tests. Review the linked WordPress and PHPStan context, then define completion as an alert when a filter callback with type declarations is detected, while accounting for callbacks in other files.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.