szepeviktor/phpstan-wordpress

Alert when add_filter is used with a function with type hinting

Mte90 opened this issue · 4 comments

Mte90 commented

So after a discussion on #core-conding-standards it is clear that is important to suggest to the developers when a function is used in a filter to not use type hinting in the function definition.
This because there are other developers that doesn't follow the standards and this can create fatal errors because a filter like the_excerpt return null instead of string.

So ideally there should be a rule that check if add_filter is used, get the second parameter and find that function to see if is using type hinting. After this add an alert in console with information about it.

I tried but I cannot find a phpstan documentation about how to do extensions or example to achieve this.

👋 Hello Daniele!
After reading your Slack chat I've realized: add_filter and apply_filters are mixed in the source code so there is no way to learn all apply_filters types then evaluate types in add_filter calls.

What do you think?

Mte90 commented

I was thinking to track all the add_filter calls, in this way we don't need to check wordpress or others but just the code itself.
In this way we can alert about the use of type hinting and we don't care what is the type from the apply_filters definition.

I see! PHPCS is your friend. It can forbid type hinting easily.