Code Modernization: Rename parameters to match native PHP functions in `wp-includes/compat.php`.

This ensures that parameter names for PHP polyfills in WordPress core 100% match the native PHP parameter names. Otherwise using named parameters with those functions could cause fatal errors for installs where the polyfills kick in.

This commit:
* Renames the `$string` parameter to `$message` in `_()` polyfill.
* Renames the `$str` parameter to `$string` in `mb_substr()` and `mb_strlen()` polyfills.
* Renames the `$raw_output` parameter to `$binary` in `hash_hmac()` polyfill.
* Renames the `$a` and `$b` parameters to `$known_string` and `$user_string` in `hash_equals()` polyfill.
* Renames the `$var` parameter to `$value` in `is_countable()` and `is_iterable()` polyfills.
* Renames the `$arr` parameter to `$array` in `array_key_first()` and `array_key_last()` polyfills.

Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364].

Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55650.
Built from https://develop.svn.wordpress.org/trunk@53365


git-svn-id: http://core.svn.wordpress.org/trunk@52954 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-05-08 00:29:14 +00:00
parent 8c8b0a203a
commit 32c0cfe3b3
2 changed files with 71 additions and 61 deletions

View File

@ -8,8 +8,8 @@
// If gettext isn't available. // If gettext isn't available.
if ( ! function_exists( '_' ) ) { if ( ! function_exists( '_' ) ) {
function _( $string ) { function _( $message ) {
return $string; return $message;
} }
} }
@ -49,24 +49,24 @@ if ( ! function_exists( 'mb_substr' ) ) :
* *
* @see _mb_substr() * @see _mb_substr()
* *
* @param string $str The string to extract the substring from. * @param string $string The string to extract the substring from.
* @param int $start Position to being extraction from in `$str`. * @param int $start Position to being extraction from in `$string`.
* @param int|null $length Optional. Maximum number of characters to extract from `$str`. * @param int|null $length Optional. Maximum number of characters to extract from `$string`.
* Default null. * Default null.
* @param string|null $encoding Optional. Character encoding to use. Default null. * @param string|null $encoding Optional. Character encoding to use. Default null.
* @return string Extracted substring. * @return string Extracted substring.
*/ */
function mb_substr( $str, $start, $length = null, $encoding = null ) { function mb_substr( $string, $start, $length = null, $encoding = null ) {
return _mb_substr( $str, $start, $length, $encoding ); return _mb_substr( $string, $start, $length, $encoding );
} }
endif; endif;
/** /**
* Internal compat function to mimic mb_substr(). * Internal compat function to mimic mb_substr().
* *
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit. * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
* For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence. * For `$encoding === UTF-8`, the `$str` input is expected to be a valid UTF-8 byte
* The behavior of this function for invalid inputs is undefined. * sequence. The behavior of this function for invalid inputs is undefined.
* *
* @ignore * @ignore
* @since 3.2.0 * @since 3.2.0
@ -116,6 +116,7 @@ function _mb_substr( $str, $start, $length = null, $encoding = null ) {
// Start with 1 element instead of 0 since the first thing we do is pop. // Start with 1 element instead of 0 since the first thing we do is pop.
$chars = array( '' ); $chars = array( '' );
do { do {
// We had some string left over from the last round, but we counted it in that last round. // We had some string left over from the last round, but we counted it in that last round.
array_pop( $chars ); array_pop( $chars );
@ -143,12 +144,12 @@ if ( ! function_exists( 'mb_strlen' ) ) :
* *
* @see _mb_strlen() * @see _mb_strlen()
* *
* @param string $str The string to retrieve the character length from. * @param string $string The string to retrieve the character length from.
* @param string|null $encoding Optional. Character encoding to use. Default null. * @param string|null $encoding Optional. Character encoding to use. Default null.
* @return int String length of `$str`. * @return int String length of `$string`.
*/ */
function mb_strlen( $str, $encoding = null ) { function mb_strlen( $string, $encoding = null ) {
return _mb_strlen( $str, $encoding ); return _mb_strlen( $string, $encoding );
} }
endif; endif;
@ -156,7 +157,7 @@ endif;
* Internal compat function to mimic mb_strlen(). * Internal compat function to mimic mb_strlen().
* *
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit. * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
* For $encoding === UTF-8, the `$str` input is expected to be a valid UTF-8 byte * For `$encoding === UTF-8`, the `$str` input is expected to be a valid UTF-8 byte
* sequence. The behavior of this function for invalid inputs is undefined. * sequence. The behavior of this function for invalid inputs is undefined.
* *
* @ignore * @ignore
@ -199,6 +200,7 @@ function _mb_strlen( $str, $encoding = null ) {
// Start at 1 instead of 0 since the first thing we do is decrement. // Start at 1 instead of 0 since the first thing we do is decrement.
$count = 1; $count = 1;
do { do {
// We had some string left over from the last round, but we counted it in that last round. // We had some string left over from the last round, but we counted it in that last round.
$count--; $count--;
@ -235,16 +237,16 @@ if ( ! function_exists( 'hash_hmac' ) ) :
* *
* @see _hash_hmac() * @see _hash_hmac()
* *
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
* @param string $data Data to be hashed. * @param string $data Data to be hashed.
* @param string $key Secret key to use for generating the hash. * @param string $key Secret key to use for generating the hash.
* @param bool $raw_output Optional. Whether to output raw binary data (true), * @param bool $binary Optional. Whether to output raw binary data (true),
* or lowercase hexits (false). Default false. * or lowercase hexits (false). Default false.
* @return string|false The hash in output determined by `$raw_output`. False if `$algo` * @return string|false The hash in output determined by `$binary`.
* is unknown or invalid. * False if `$algo` is unknown or invalid.
*/ */
function hash_hmac( $algo, $data, $key, $raw_output = false ) { function hash_hmac( $algo, $data, $key, $binary = false ) {
return _hash_hmac( $algo, $data, $key, $raw_output ); return _hash_hmac( $algo, $data, $key, $binary );
} }
endif; endif;
@ -254,15 +256,15 @@ endif;
* @ignore * @ignore
* @since 3.2.0 * @since 3.2.0
* *
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
* @param string $data Data to be hashed. * @param string $data Data to be hashed.
* @param string $key Secret key to use for generating the hash. * @param string $key Secret key to use for generating the hash.
* @param bool $raw_output Optional. Whether to output raw binary data (true), * @param bool $binary Optional. Whether to output raw binary data (true),
* or lowercase hexits (false). Default false. * or lowercase hexits (false). Default false.
* @return string|false The hash in output determined by `$raw_output`. False if `$algo` * @return string|false The hash in output determined by `$binary`.
* is unknown or invalid. * False if `$algo` is unknown or invalid.
*/ */
function _hash_hmac( $algo, $data, $key, $raw_output = false ) { function _hash_hmac( $algo, $data, $key, $binary = false ) {
$packs = array( $packs = array(
'md5' => 'H32', 'md5' => 'H32',
'sha1' => 'H40', 'sha1' => 'H40',
@ -285,15 +287,16 @@ function _hash_hmac( $algo, $data, $key, $raw_output = false ) {
$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) ); $hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );
if ( $raw_output ) { if ( $binary ) {
return pack( $pack, $hmac ); return pack( $pack, $hmac );
} }
return $hmac; return $hmac;
} }
if ( ! function_exists( 'hash_equals' ) ) : if ( ! function_exists( 'hash_equals' ) ) :
/** /**
* Timing attack safe string comparison * Timing attack safe string comparison.
* *
* Compares two strings using the same time whether they're equal or not. * Compares two strings using the same time whether they're equal or not.
* *
@ -308,20 +311,22 @@ if ( ! function_exists( 'hash_equals' ) ) :
* *
* @since 3.9.2 * @since 3.9.2
* *
* @param string $a Expected string. * @param string $known_string Expected string.
* @param string $b Actual, user supplied, string. * @param string $user_string Actual, user supplied, string.
* @return bool Whether strings are equal. * @return bool Whether strings are equal.
*/ */
function hash_equals( $a, $b ) { function hash_equals( $known_string, $user_string ) {
$a_length = strlen( $a ); $known_string_length = strlen( $known_string );
if ( strlen( $b ) !== $a_length ) {
if ( strlen( $user_string ) !== $known_string_length ) {
return false; return false;
} }
$result = 0; $result = 0;
// Do not attempt to "optimize" this. // Do not attempt to "optimize" this.
for ( $i = 0; $i < $a_length; $i++ ) { for ( $i = 0; $i < $known_string_length; $i++ ) {
$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] ); $result |= ord( $known_string[ $i ] ) ^ ord( $user_string[ $i ] );
} }
return 0 === $result; return 0 === $result;
@ -346,14 +351,14 @@ if ( ! function_exists( 'is_countable' ) ) {
* *
* @since 4.9.6 * @since 4.9.6
* *
* @param mixed $var The value to check. * @param mixed $value The value to check.
* @return bool True if `$var` is countable, false otherwise. * @return bool True if `$value` is countable, false otherwise.
*/ */
function is_countable( $var ) { function is_countable( $value ) {
return ( is_array( $var ) return ( is_array( $value )
|| $var instanceof Countable || $value instanceof Countable
|| $var instanceof SimpleXMLElement || $value instanceof SimpleXMLElement
|| $var instanceof ResourceBundle || $value instanceof ResourceBundle
); );
} }
} }
@ -367,11 +372,11 @@ if ( ! function_exists( 'is_iterable' ) ) {
* *
* @since 4.9.6 * @since 4.9.6
* *
* @param mixed $var The value to check. * @param mixed $value The value to check.
* @return bool True if `$var` is iterable, false otherwise. * @return bool True if `$value` is iterable, false otherwise.
*/ */
function is_iterable( $var ) { function is_iterable( $value ) {
return ( is_array( $var ) || $var instanceof Traversable ); return ( is_array( $value ) || $value instanceof Traversable );
} }
} }
@ -384,12 +389,12 @@ if ( ! function_exists( 'array_key_first' ) ) {
* *
* @since 5.9.0 * @since 5.9.0
* *
* @param array $arr An array. * @param array $array An array.
* @return string|int|null The first key of array if the array * @return string|int|null The first key of array if the array
* is not empty; `null` otherwise. * is not empty; `null` otherwise.
*/ */
function array_key_first( array $arr ) { function array_key_first( array $array ) {
foreach ( $arr as $key => $value ) { foreach ( $array as $key => $value ) {
return $key; return $key;
} }
} }
@ -404,16 +409,18 @@ if ( ! function_exists( 'array_key_last' ) ) {
* *
* @since 5.9.0 * @since 5.9.0
* *
* @param array $arr An array. * @param array $array An array.
* @return string|int|null The last key of array if the array * @return string|int|null The last key of array if the array
*. is not empty; `null` otherwise. *. is not empty; `null` otherwise.
*/ */
function array_key_last( array $arr ) { function array_key_last( array $array ) {
if ( empty( $arr ) ) { if ( empty( $array ) ) {
return null; return null;
} }
end( $arr );
return key( $arr ); end( $array );
return key( $array );
} }
} }
@ -452,6 +459,7 @@ if ( ! function_exists( 'str_starts_with' ) ) {
if ( '' === $needle ) { if ( '' === $needle ) {
return true; return true;
} }
return 0 === strpos( $haystack, $needle ); return 0 === strpos( $haystack, $needle );
} }
} }
@ -473,7 +481,9 @@ if ( ! function_exists( 'str_ends_with' ) ) {
if ( '' === $haystack && '' !== $needle ) { if ( '' === $haystack && '' !== $needle ) {
return false; return false;
} }
$len = strlen( $needle ); $len = strlen( $needle );
return 0 === substr_compare( $haystack, $needle, -$len, $len ); return 0 === substr_compare( $haystack, $needle, -$len, $len );
} }
} }

View File

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