Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sniff for correct usage of add_action vs add_filter #713

Open
johnbillion opened this issue Nov 14, 2016 · 11 comments
Open

Sniff for correct usage of add_action vs add_filter #713

johnbillion opened this issue Nov 14, 2016 · 11 comments
Labels
Focus: Code analysis Sniffs to prevent common mistakes and improve code in general Type: Enhancement

Comments

@johnbillion
Copy link
Member

Today we discovered that some code under development was breaking functionality related to the attachment_fields_to_save hook.

It turns out the bug was caused by a callback being incorrectly registered as an action (with add_action()) instead of a filter (with add_filter()). This meant that all filters after this particular callback were receiving a null value instead of the WP_Post object.

It's a bit of a long shot, but it would be great if WPCS could be aware of which hooks in WordPress are actions, and which are filters, and raise an error if add_action() is used instead of add_filter().

@grappler
Copy link
Member

grappler commented Nov 14, 2016

I think the sniff could be relatively simple if we have list of WordPress filters and actions. It would need to be decided what happens if the hook is not part of the list as it is not part of WordPress core. Maybe https://github.com/WordPress/phpdoc-parser could help with the list.

@jrfnl
Copy link
Member

jrfnl commented Nov 14, 2016

Just checking - was the actual issue caused by the use of add_action() or was it caused by the hooked in function not returning the expected value ... ?

@JDGrimes
Copy link
Contributor

Just checking - was the actual issue caused by the use of add_action() or was it caused by the hooked in function not returning the expected value ... ?

Yeah, I've been bitten by that before. But that would really be beyond WPCS's ability to detect in some cases.

@johnbillion
Copy link
Member Author

Yes the actual bug was caused because the action callback was not returning a value. The same problem would occur if a filter callback didn't return a value, but it would be more likely to be picked up during code review in that case, I think.

A sniff which could detect when a filter callback doesn't contain a return statement on the final line would be great, but that's probably a bit too much to ask of a static analyser.

@jrfnl jrfnl added Type: Enhancement Focus: Code analysis Sniffs to prevent common mistakes and improve code in general labels Apr 1, 2019
@johnbillion
Copy link
Member Author

Gonna close this off as something that realistically isn't practical to implement.

@johnbillion
Copy link
Member Author

I'm going to re-open this as the original issue can be quite difficult for a developer to spot. I don't think WPCS should go so far as detecting return values, but detecting incorrect usage of add_action() versus add_filter() for core hooks ought to be doable.

I recently created a library that lists core's actions and filters that could be used by WPCS: https://github.com/johnbillion/wp-hooks

@johnbillion johnbillion reopened this Nov 5, 2019
@dingo-d
Copy link
Member

dingo-d commented Nov 6, 2019

That would be awesome! I've run into this problem yesterday, where I ended up one hour debugging the issue where I typed add_action( 'wp_handle_upload', ... instead of add_filter( 'wp_handle_upload', ..., and my image uploads were failing and I had no idea why.

👍 from me 🙂

@jrfnl
Copy link
Member

jrfnl commented Nov 6, 2019

Loosely related to #1803.

Any sniff for this would need a list of hooks and that list could be generated by running PHPCS over Core.

A sister-sniff checking for use of deprecated hooks would also be good.

@johnbillion
Copy link
Member Author

Any sniff for this would need a list of hooks and that list could be generated by running PHPCS over Core.

This is what the wp-hooks library provides.

A sister-sniff checking for use of deprecated hooks would also be good.

Yep, agreed. Let's keep this focused for now though.

@jrfnl
Copy link
Member

jrfnl commented Jul 22, 2020

Just saw a comment somewhere in a Slack about someone having to fix some bugs related to exactly this. Good reminder that a sniff which checks that the correct hook-in function is used would be a good addition, if for no other reason than to remind the developer that actions !== filters.

@johnbillion
Copy link
Member Author

If someone wants to pick this up, my wp-hooks library is actively maintained and would be a good source for the list of core actions and filters.

Aside: I'm considering how best to provide common aliases for hooks that include dynamic portions in their names, eg. {$old_status}_to_{$new_status}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Focus: Code analysis Sniffs to prevent common mistakes and improve code in general Type: Enhancement
Projects
None yet
Development

No branches or pull requests

5 participants