Code Modernization: Correct handling of null
in wp_parse_str()
.
This fixes parse_str(): Passing null to parameter #1 ($string) of type string is deprecated
notices on PHP 8.1, without change in behaviour.
Impact: 311 of the pre-existing tests are affected by this issue.
The PHP native parse_str()
function expects a string, however, based on the failing tests, it is clear there are functions in WordPress which passes a non-string – including null
– value to the wp_parse_str()
function, which would subsequently pass it onto the PHP native function without further input validation.
Most notable offender is the wp_parse_args()
function which special cases arrays and objects, but passes everything else off to wp_parse_str()
.
Several ways to fix this issue have been explored, including checking the received value with is_string()
or is_scalar()
before passing it off to the PHP native parse_str()
function.
In the end it was decided against these in favor of a string cast as:
is_string()
would significantly change the behavior for anything non-string.
is_scalar()
up to a point as well, as it does not take objects with a __toString()
method into account.
Executing a string cast on the received value before passing it on maintains the pre-existing behavior while still preventing the deprecation notice coming from PHP 8.1.
Reference: PHP Manual: parse_str()
Follow-up to [5709].
Props jrf, hellofromTonya, lucatume, SergeyBiryukov.
See #53635.