Skip to content

Commit

Permalink
Users: Set correct default value for $user_login in `retrieve_passw…
Browse files Browse the repository at this point in the history
…ord()`.

This resolves a "passing null to non-nullable" deprecation notice on PHP 8.1+:
{{{
Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated
}}}

Follow-up to [50129], [54477].

Props afragen, peterwilsoncc, SergeyBiryukov.
Fixes #62298.

git-svn-id: https://develop.svn.wordpress.org/trunk@59312 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Oct 28, 2024
1 parent 58b5c63 commit 6bc26d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -3106,7 +3106,7 @@ function check_password_reset_key( $key, $login ) {
* Defaults to `$_POST['user_login']` if not set.
* @return true|WP_Error True when finished, WP_Error object on error.
*/
function retrieve_password( $user_login = null ) {
function retrieve_password( $user_login = '' ) {
$errors = new WP_Error();
$user_data = false;

Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/tests/user/retrievePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,17 @@ public function test_retrieve_password_should_fetch_user_by_login_if_not_found_b
$this->assertTrue( retrieve_password( '[email protected]' ), 'Fetching user by login failed.' );
$this->assertTrue( retrieve_password( '[email protected]' ), 'Fetching user by email failed.' );
}

/**
* Tests that PHP 8.1 "passing null to non-nullable" deprecation notice
* is not thrown when the `$user_login` parameter is empty.
*
* The notice that we should not see:
* `Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated`.
*
* @ticket 62298
*/
public function test_retrieve_password_does_not_throw_deprecation_notice_with_default_parameters() {
$this->assertWPError( retrieve_password() );
}
}

0 comments on commit 6bc26d5

Please sign in to comment.