Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#44697 closed defect (bug) (fixed)

Argument type does not match: user.php

Reported by: subrataemfluence Owned by: pento
Milestone: 5.1 Priority: normal
Severity: normal Version: 5.1
Component: Users Keywords: has-patch needs-refresh
Focuses: coding-standards Cc:


File location: wp-includes/user.php

* ...
* @param int $for_user_id Optional. User ID to set up global data.
function setup_userdata( $for_user_id = '' ) {

Attachments (2)

44697.diff (622 bytes) - added by subrataemfluence 3 years ago.
44697-1.diff (701 bytes) - added by subrataemfluence 3 years ago.

Download all attachments as: .zip

Change History (9)

#1 @subrataemfluence
3 years ago

  • Keywords has-patch added; needs-patch removed

#2 @SergeyBiryukov
3 years ago

  • Keywords needs-refresh added
  • Milestone changed from Awaiting Review to 5.0

Hi @subrataemfluence, thanks for the ticket!

The existing type is correct, $for_user_id should be an integer, not a string.

That said, it could use a better default value, i.e. 0 instead of an empty string. Given the non-strict check a few lines below, changing it to 0 should not affect the logic.

#3 @subrataemfluence
3 years ago

I had the same initial feeling but got a kinda confused!

Now, since the default value set for $for_user_id is 0, can this be better idea to rewrite the comparison block like this?

if ( '' == $for_user_id || 0 == $for_user_id ) {
   $for_user_id = get_current_user_id();

$user = get_userdata( $for_user_id );

This ensures $for_user_id gets the current user id for default values.

And if fails to satisfy the above condition, rest will be executed as tNB: Ire.

And if the default value for $for_user_id is set to 0, do we really need to compare it with an empty string?

#4 @pento
3 years ago

  • Milestone changed from 5.0 to 5.1

#5 @pento
3 years ago

  • Owner set to pento
  • Resolution set to fixed
  • Status changed from new to closed

In 44603:

Users: Correct the default value in setup_userdata().

The $for_user_id parameter is an int, not a string, so the default value should be 0, rather than an empty string.

Props subrataemfluence.
Fixes #44697.

#7 @pento
3 years ago

In 44605:

Docs: Tweak the docblock for setup_userdata().

This is a followup to [44603]. The default value should be last, not somewhere in the middle.

Props swissspidy.
Fixes #44697.

Note: See TracTickets for help on using tickets.