Code Modernization: Fix "passing null to non-nullable" deprecation in `_mb_substr()`.

The `_mb_substr()` function expects a string for the `$str` parameter, but does not do input validation. This function contains a `preg_match_all()` which also expects a string type for the given subject (i.e. `$str`). 

Passing `null` to this parameter results in `preg_match_all(): Passing null to parameter #2 ($subject) of type string is deprecated` notice on PHP 8.1.

To maintain the same behaviour as before, a guard clause is added to bail out early when `$str` is passed as `null`. The outcome will, in that case, only ever be an empty string.

Note: this does mean that the `_mb_substr()` function now has a subtle difference in behaviour compared to the PHP native `mb_substr()` function as the latter ''will'' throw the deprecation notice.

The existing tests already cover this issue.

Follow-up to [17621], [36017], [32364].

Props jrf, hellofromTonya.
See #53635.
Built from https://develop.svn.wordpress.org/trunk@51853


git-svn-id: http://core.svn.wordpress.org/trunk@51452 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2021-09-22 23:34:57 +00:00
parent f52bf291cb
commit 847b7d8690
2 changed files with 5 additions and 1 deletions

View File

@ -79,6 +79,10 @@ endif;
* @return string Extracted substring.
*/
function _mb_substr( $str, $start, $length = null, $encoding = null ) {
if ( null === $str ) {
return '';
}
if ( null === $encoding ) {
$encoding = get_option( 'blog_charset' );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-51852';
$wp_version = '5.9-alpha-51853';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.