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
Add type checking for hook callbacks in add_action
and add_filter
#106
Comments
High expectations! |
This was completed, wasn't it? 🎉 |
I do not follow the development :) |
This was only partially completed, I'll open a follow-up issue with the details. |
Hi there, Awesome that this feature is added! We've just updated to the latest version for some of our projects. This happens on a function like this: /**
*
* @param mixed $some_mixed_var
* @return mixed
*/
function some_function_with_mixed_return_type ( $some_mixed_var ) {
$some_mixed_var = is_string( $some_mixed_var ) ? 'I am a string' : $some_mixed_var;
return $some_mixed_var;
}
add_filter( 'some_filter_name', 'some_function_with_mixed_return_type' ); I hope you guys can take a look if we can resolve this :). |
Also one extra thing, which is more an opinion i would like to start a little discussion about. When creating a callback function for an action, with this new set of rules, a return type of An example below: add_action( 'save_post', 'set_custom_meta' );
function set_custom_meta( int $post_id ): bool {
return (bool) update_post_meta( $post_id, 'some_meta', 'some_value' );
}
function some_function_somewhere_else_in_the_website(): void {
$meta_updated = set_custom_meta( 1 );
if ( $meta_updated ) {
// Do something
} else {
// Do something
}
} As you can see, this example is not possible, because we are now forced to return a void for the |
@menno-ll Are you using version 1.1.5? Issues with Regarding returning a value from an action, I can see how it's beneficial to reuse functions but at the same time a developer looking at your |
Hi @johnbillion Thank you for the quick response! You were right, we made the discovery with the Regarding the reuse of a function used as an action callback, the way you describe with creating a wrapper function was also what i had in mind to bypass the phpstan error. I just wanted to make you aware of this scenario we found in our code, so it could be taken into consideration. If writing a wrapper function is the way to go, we will modify it to comply with the new rule, or ignore that rule for our projects, depending on the opinion of my collegues. Thanks again for helping out! |
I'm planning on working on some rules that check the use of
add_action()
andadd_filter()
when the action or filter is from WordPress core. The wp-hooks library facilitates this, and the wp-hooks-generator library facilitates this for hooks in third party plugins and themes (although I'm only concentrating on the WordPress core hooks for now).I'll open a PR once I have something working.
When I call
add_action( 'foo', $callback )
oradd_filter( 'foo', $callback )
:$accepted_args
parameter should match the number of parameters that the callback acceptsadd_filter
must return a value with a type which is compatible with the type passed to itadd_action
must return voidNotes:
$accepted_args
)mixed
, etcplugin_action_links_{$plugin_file}
and see if we get a match for dynamic hook names (stretch goal)The text was updated successfully, but these errors were encountered: