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

View File

@ -16,7 +16,7 @@
*
* @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.