-
-
Notifications
You must be signed in to change notification settings - Fork 27
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! |
Just chiming in here to note how I believe this would have caught a fatal error from occurring in a Performance Lab plugin: WordPress/performance#1755. The issue is that the Image Placeholders plugin added a /**
* Filters the attachment data prepared for JavaScript.
*
* @since 3.5.0
*
* @param array $response Array of prepared attachment data. See {@see wp_prepare_attachment_for_js()}.
* @param WP_Post $attachment Attachment object.
* @param array|false $meta Array of attachment meta data, or false if there is none.
*/
return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta ); However, the filter callback in Image Placeholders incorrectly presumed the third - function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachment, array $meta ): array {
+ function dominant_color_prepare_attachment_for_js( $response, WP_Post $attachment, $meta ): array { Do I understand correctly that this issue here of type checking all of the callback args is in scope? |
It should be in scope yes. I haven't had a chance to work on this recently, but I did work on the prerequisite PHP API to the wp-hooks info. |
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: