You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PHP 8.1: WP::parse_request(): prevent "passing null to non-nullable" notice
As per the PHP manual:
> If the component parameter is omitted, an associative array is returned.
> If the component parameter is specified, `parse_url()` returns a string (or an int, in the case of `PHP_URL_PORT`) instead of an array. If the requested component doesn't exist within the given URL, `null` will be returned.
Ref: https://www.php.net/manual/en/function.parse-url.php#refsect1-function.parse-url-returnvalues
In this case, `parse_url()` is called with the `PHP_URL_PATH` as `$component`. This will return `null` in the majority of cases, as - expect for subdirectory-based sites - `home_url()` will return a URL without trailing slash, like `http://example.org`.
The return value of `parse_url()` was subsequently passed to `trim()` leading to a `trim(): Passing null to parameter #1 ($string) of type string is deprecated` deprecation notice on PHP 8.1.
Fixed by adjusting the logic flow to:
* Only pass the return value of `parse_url()` to follow-on functions if it makes sense, i.e. if it isn't `null`, nor an empty string.
* Preventing calls to `preg_replace()` and `trim()` further down in the function logic flow, when `preg_replace()`/`trim()` would have nothing to do anyhow.
0 commit comments