Make WordPress Core

Changeset 53365


Ignore:
Timestamp:
05/08/2022 12:27:41 AM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r52425 r53365  
    99// If gettext isn't available.
    1010if ( ! function_exists( '_' ) ) {
    11     function _( $string ) {
    12         return $string;
     11    function _( $message ) {
     12        return $message;
    1313    }
    1414}
     
    5050     * @see _mb_substr()
    5151     *
    52      * @param string      $str      The string to extract the substring from.
    53      * @param int         $start    Position to being extraction from in `$str`.
    54      * @param int|null    $length   Optional. Maximum number of characters to extract from `$str`.
     52     * @param string      $string   The string to extract the substring from.
     53     * @param int         $start    Position to being extraction from in `$string`.
     54     * @param int|null    $length   Optional. Maximum number of characters to extract from `$string`.
    5555     *                              Default null.
    5656     * @param string|null $encoding Optional. Character encoding to use. Default null.
    5757     * @return string Extracted substring.
    5858     */
    59     function mb_substr( $str, $start, $length = null, $encoding = null ) {
    60         return _mb_substr( $str, $start, $length, $encoding );
     59    function mb_substr( $string, $start, $length = null, $encoding = null ) {
     60        return _mb_substr( $string, $start, $length, $encoding );
    6161    }
    6262endif;
     
    6565 * Internal compat function to mimic mb_substr().
    6666 *
    67  * Only understands UTF-8 and 8bit.  All other character sets will be treated as 8bit.
    68  * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
    69  * The behavior of this function for invalid inputs is undefined.
     67 * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
     68 * For `$encoding === UTF-8`, the `$str` input is expected to be a valid UTF-8 byte
     69 * sequence. The behavior of this function for invalid inputs is undefined.
    7070 *
    7171 * @ignore
     
    117117    // Start with 1 element instead of 0 since the first thing we do is pop.
    118118    $chars = array( '' );
     119
    119120    do {
    120121        // We had some string left over from the last round, but we counted it in that last round.
     
    144145     * @see _mb_strlen()
    145146     *
    146      * @param string      $str      The string to retrieve the character length from.
     147     * @param string      $string   The string to retrieve the character length from.
    147148     * @param string|null $encoding Optional. Character encoding to use. Default null.
    148      * @return int String length of `$str`.
    149      */
    150     function mb_strlen( $str, $encoding = null ) {
    151         return _mb_strlen( $str, $encoding );
     149     * @return int String length of `$string`.
     150     */
     151    function mb_strlen( $string, $encoding = null ) {
     152        return _mb_strlen( $string, $encoding );
    152153    }
    153154endif;
     
    157158 *
    158159 * Only understands UTF-8 and 8bit.  All other character sets will be treated as 8bit.
    159  * For $encoding === UTF-8, the `$str` input is expected to be a valid UTF-8 byte
     160 * For `$encoding === UTF-8`, the `$str` input is expected to be a valid UTF-8 byte
    160161 * sequence. The behavior of this function for invalid inputs is undefined.
    161162 *
     
    200201    // Start at 1 instead of 0 since the first thing we do is decrement.
    201202    $count = 1;
     203
    202204    do {
    203205        // We had some string left over from the last round, but we counted it in that last round.
     
    236238     * @see _hash_hmac()
    237239     *
    238      * @param string $algo       Hash algorithm. Accepts 'md5' or 'sha1'.
    239      * @param string $data       Data to be hashed.
    240      * @param string $key        Secret key to use for generating the hash.
    241      * @param bool   $raw_output Optional. Whether to output raw binary data (true),
    242      *                           or lowercase hexits (false). Default false.
    243      * @return string|false The hash in output determined by `$raw_output`. False if `$algo`
    244      *                      is unknown or invalid.
    245      */
    246     function hash_hmac( $algo, $data, $key, $raw_output = false ) {
    247         return _hash_hmac( $algo, $data, $key, $raw_output );
     240     * @param string $algo   Hash algorithm. Accepts 'md5' or 'sha1'.
     241     * @param string $data   Data to be hashed.
     242     * @param string $key    Secret key to use for generating the hash.
     243     * @param bool   $binary Optional. Whether to output raw binary data (true),
     244     *                       or lowercase hexits (false). Default false.
     245     * @return string|false The hash in output determined by `$binary`.
     246     *                      False if `$algo` is unknown or invalid.
     247     */
     248    function hash_hmac( $algo, $data, $key, $binary = false ) {
     249        return _hash_hmac( $algo, $data, $key, $binary );
    248250    }
    249251endif;
     
    255257 * @since 3.2.0
    256258 *
    257  * @param string $algo       Hash algorithm. Accepts 'md5' or 'sha1'.
    258  * @param string $data       Data to be hashed.
    259  * @param string $key        Secret key to use for generating the hash.
    260  * @param bool   $raw_output Optional. Whether to output raw binary data (true),
    261  *                           or lowercase hexits (false). Default false.
    262  * @return string|false The hash in output determined by `$raw_output`. False if `$algo`
    263  *                      is unknown or invalid.
     259 * @param string $algo   Hash algorithm. Accepts 'md5' or 'sha1'.
     260 * @param string $data   Data to be hashed.
     261 * @param string $key    Secret key to use for generating the hash.
     262 * @param bool   $binary Optional. Whether to output raw binary data (true),
     263 *                       or lowercase hexits (false). Default false.
     264 * @return string|false The hash in output determined by `$binary`.
     265 *                      False if `$algo` is unknown or invalid.
    264266 */
    265 function _hash_hmac( $algo, $data, $key, $raw_output = false ) {
     267function _hash_hmac( $algo, $data, $key, $binary = false ) {
    266268    $packs = array(
    267269        'md5'  => 'H32',
     
    286288    $hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );
    287289
    288     if ( $raw_output ) {
     290    if ( $binary ) {
    289291        return pack( $pack, $hmac );
    290292    }
     293
    291294    return $hmac;
    292295}
     
    294297if ( ! function_exists( 'hash_equals' ) ) :
    295298    /**
    296      * Timing attack safe string comparison
     299     * Timing attack safe string comparison.
    297300     *
    298301     * Compares two strings using the same time whether they're equal or not.
     
    309312     * @since 3.9.2
    310313     *
    311      * @param string $a Expected string.
    312      * @param string $b Actual, user supplied, string.
     314     * @param string $known_string Expected string.
     315     * @param string $user_string Actual, user supplied, string.
    313316     * @return bool Whether strings are equal.
    314317     */
    315     function hash_equals( $a, $b ) {
    316         $a_length = strlen( $a );
    317         if ( strlen( $b ) !== $a_length ) {
     318    function hash_equals( $known_string, $user_string ) {
     319        $known_string_length = strlen( $known_string );
     320
     321        if ( strlen( $user_string ) !== $known_string_length ) {
    318322            return false;
    319323        }
     324
    320325        $result = 0;
    321326
    322327        // Do not attempt to "optimize" this.
    323         for ( $i = 0; $i < $a_length; $i++ ) {
    324             $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
     328        for ( $i = 0; $i < $known_string_length; $i++ ) {
     329            $result |= ord( $known_string[ $i ] ) ^ ord( $user_string[ $i ] );
    325330        }
    326331
     
    347352     * @since 4.9.6
    348353     *
    349      * @param mixed $var The value to check.
    350      * @return bool True if `$var` is countable, false otherwise.
    351      */
    352     function is_countable( $var ) {
    353         return ( is_array( $var )
    354             || $var instanceof Countable
    355             || $var instanceof SimpleXMLElement
    356             || $var instanceof ResourceBundle
     354     * @param mixed $value The value to check.
     355     * @return bool True if `$value` is countable, false otherwise.
     356     */
     357    function is_countable( $value ) {
     358        return ( is_array( $value )
     359            || $value instanceof Countable
     360            || $value instanceof SimpleXMLElement
     361            || $value instanceof ResourceBundle
    357362        );
    358363    }
     
    368373     * @since 4.9.6
    369374     *
    370      * @param mixed $var The value to check.
    371      * @return bool True if `$var` is iterable, false otherwise.
    372      */
    373     function is_iterable( $var ) {
    374         return ( is_array( $var ) || $var instanceof Traversable );
     375     * @param mixed $value The value to check.
     376     * @return bool True if `$value` is iterable, false otherwise.
     377     */
     378    function is_iterable( $value ) {
     379        return ( is_array( $value ) || $value instanceof Traversable );
    375380    }
    376381}
     
    385390     * @since 5.9.0
    386391     *
    387      * @param array $arr An array.
     392     * @param array $array An array.
    388393     * @return string|int|null The first key of array if the array
    389394     *                         is not empty; `null` otherwise.
    390395     */
    391     function array_key_first( array $arr ) {
    392         foreach ( $arr as $key => $value ) {
     396    function array_key_first( array $array ) {
     397        foreach ( $array as $key => $value ) {
    393398            return $key;
    394399        }
     
    405410     * @since 5.9.0
    406411     *
    407      * @param array $arr An array.
     412     * @param array $array An array.
    408413     * @return string|int|null The last key of array if the array
    409414     *.                        is not empty; `null` otherwise.
    410415     */
    411     function array_key_last( array $arr ) {
    412         if ( empty( $arr ) ) {
     416    function array_key_last( array $array ) {
     417        if ( empty( $array ) ) {
    413418            return null;
    414419        }
    415         end( $arr );
    416         return key( $arr );
     420
     421        end( $array );
     422
     423        return key( $array );
    417424    }
    418425}
     
    453460            return true;
    454461        }
     462
    455463        return 0 === strpos( $haystack, $needle );
    456464    }
     
    474482            return false;
    475483        }
     484
    476485        $len = strlen( $needle );
     486
    477487        return 0 === substr_compare( $haystack, $needle, -$len, $len );
    478488    }
Note: See TracChangeset for help on using the changeset viewer.