SonarCloud dealing with WordPress hooks

  • ALM used: GitHub
  • PHP (WordPress)
  • Remove the unused function parameter "$var".sonarlint(php:S1172)
  • Steps to reproduce:
  • Create a callback for any WP Hook that has more than one parameter and use only the second or further parameter in your callback.
  • Potential workaround
  • Ignore SonarCloud on that line

Assume you have a hook in WP that you write a callback for. For example:
add_action( 'profile_update', 'do_things_when_profile_updates', 10, 3 )
This hook offers three parameters: int $user_id, WP_User $old_user_data, array $userdata

Thus your callback may look like this:

function do_things_when_profile_updates( $user_id, $old_user_data, $user_data ) {

    $old_user_email = $old_user_data->data->user_email;
    $new_user_email = $user_data['user_email'];
    //do further things

You will not always use all three parameter. Of course, you can omit any subsequent parameter, however you cannot omit preceding parameters, AFAIK.

What is the suggested way to deal with this?

Hello @smileBeda ,

thanks for the report!
You’re right; it does not make sense to raise it here.

We will investigate how we can improve the rule, to not exhibit this behavior.


1 Like