Make WordPress Core

Changeset 63366


Ignore:
Timestamp:
08/27/2026 07:00:51 PM (2 months ago)
Author:
westonruter
Message:

Code Quality: Support passing an int to wp_parse_list() et al.

Passing an integer has worked since r44546 consolidated this parsing on preg_split(), which coerces it to a string, but the declared mixed[]|string parameter type never allowed for it. The type is widened to mixed[]|string|int for wp_parse_list(), wp_parse_id_list(), and wp_parse_slug_list(). The wp_parse_list() function now converts an integer explicitly instead of relying on that coercion. Behavior is unchanged, and tests are added for integer input. A float or a bool is also coerced today, but neither is meaningful for a list of IDs or slugs, so both are left undocumented.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13297.
Follow-up to r44546, r62797, r62835.

See #65817.

Location:
trunk
Files:
4 edited

Legend:

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

    r63365 r63366  
    50285028 *
    50295029 * @since 5.1.0
    5030  *
    5031  * @param mixed[]|string $input_list List of values.
     5030 * @since 7.2.0 Added explicit support for passing an integer.
     5031 *
     5032 * @param mixed[]|string|int $input_list List of values.
    50325033 * @return array Array of scalar values. A string is split into a list, while an array
    50335034 *               keeps its keys, so the result is not necessarily a list.
    50345035 * @phpstan-return (
    5035  *     $input_list is string ? list<string> : (
     5036 *     $input_list is string|int ? list<string> : (
    50365037 *         $input_list is array<string> ? array<string> : array<scalar>
    50375038 *     )
    … …  
    50395040 */
    50405041function wp_parse_list( $input_list ): array {
    5041         if ( ! is_array( $input_list ) ) {
     5042        if ( is_int( $input_list ) ) {
     5043                $input_list = array( (string) $input_list );
     5044        } elseif ( ! is_array( $input_list ) ) {
    50425045                $parsed_list = preg_split( '/[\s,]+/', $input_list, -1, PREG_SPLIT_NO_EMPTY );
    50435046                return is_array( $parsed_list ) ? $parsed_list : array();
    … …  
    50545057 *
    50555058 * @since 3.0.0
    5056  * @since 5.1.0 Refactored to use wp_parse_list().
    5057  *
    5058  * @param mixed[]|string $input_list List of IDs.
     5059 * @since 5.1.0 Refactored to use {@see wp_parse_list()}.
     5060 * @since 7.2.0 Added explicit support for passing an integer.
     5061 *
     5062 * @param mixed[]|string|int $input_list List of IDs.
    50595063 * @return int[] Sanitized array of IDs. May include zero. Keys are preserved
    50605064 *               from the input and `array_unique()` may leave gaps, so the
    … …  
    50725076 *
    50735077 * @since 4.7.0
    5074  * @since 5.1.0 Refactored to use wp_parse_list().
    5075  *
    5076  * @param mixed[]|string $input_list List of slugs.
     5078 * @since 5.1.0 Refactored to use {@see wp_parse_list()}.
     5079 * @since 7.2.0 Added explicit support for passing an integer.
     5080 *
     5081 * @param mixed[]|string|int $input_list List of slugs.
    50775082 * @return string[] Sanitized array of slugs. May include an empty string. Keys
    50785083 *                  are preserved from the input and `array_unique()` may leave
  • trunk/tests/phpunit/tests/functions/wpParseIdList.php

    r62797 r63366  
    1717         * @dataProvider data_unexpected_input
    1818         *
    19          * @param mixed[]|string $input_list
     19         * @param mixed[]|string|int $input_list
    2020         * @param array<non-negative-int> $expected
    2121         */
    … …  
    3838         * Data provider.
    3939         *
    40          * @return array<string, array{ input_list: mixed[]|string, expected: array<non-negative-int> }>
     40         * @return array<string, array{ input_list: mixed[]|string|int, expected: array<non-negative-int> }>
    4141         */
    4242        public function data_wp_parse_id_list(): array {
    … …  
    7575                                'expected'   => array( 1, 2, 3, 4 ),
    7676                        ),
     77                        'positive int'             => array(
     78                                'input_list' => 5,
     79                                'expected'   => array( 5 ),
     80                        ),
     81                        'negative int'             => array(
     82                                'input_list' => -5,
     83                                'expected'   => array( 5 ),
     84                        ),
     85                        'zero'                     => array(
     86                                'input_list' => 0,
     87                                'expected'   => array( 0 ),
     88                        ),
    7789                );
    7890        }
    … …  
    8193         * Data provider.
    8294         *
    83          * @return array<string, array{ input_list: mixed[]|string, expected: array<non-negative-int> }>
     95         * @return array<string, array{ input_list: mixed[]|string|int, expected: array<non-negative-int> }>
    8496         */
    8597        public function data_unexpected_input(): array {
    … …  
    99111                        'array with spaces'  => array(
    100112                                'input_list' => array( '1 2 string with spaces' ),
     113                                'expected'   => array( 1 ),
     114                        ),
     115                        'comma in array'     => array(
     116                                'input_list' => array( '1,2' ),
    101117                                'expected'   => array( 1 ),
    102118                        ),
  • trunk/tests/phpunit/tests/functions/wpParseList.php

    r62797 r63366  
    1515         * @dataProvider data_wp_parse_list
    1616         *
    17          * @param mixed[]|string $input_list
     17         * @param mixed[]|string|int $input_list
    1818         * @param array<scalar> $expected
    1919         */
    … …  
    3636         * Data provider.
    3737         *
    38          * @return array<string, array{ input_list: mixed[]|string, expected: array<scalar> }>
     38         * @return array<string, array{ input_list: mixed[]|string|int, expected: array<scalar> }>
    3939         */
    4040        public function data_wp_parse_list(): array {
    … …  
    8484                                'expected'   => array(),
    8585                        ),
     86                        'positive int'        => array(
     87                                'input_list' => 5,
     88                                'expected'   => array( '5' ),
     89                        ),
     90                        'negative int'        => array(
     91                                'input_list' => -5,
     92                                'expected'   => array( '-5' ),
     93                        ),
     94                        'zero'                => array(
     95                                'input_list' => 0,
     96                                'expected'   => array( '0' ),
     97                        ),
     98                        'comma in array item' => array(
     99                                'input_list' => array( '1,2' ),
     100                                'expected'   => array( '1,2' ),
     101                        ),
    86102                        'passed scalar array' => array(
    87103                                'input_list' => array( 'foo', true, false, 1, 3.14 ),
  • trunk/tests/phpunit/tests/functions/wpParseSlugList.php

    r62797 r63366  
    1717         * @dataProvider data_unexpected_input
    1818         *
    19          * @param mixed[]|string $input_list
     19         * @param mixed[]|string|int $input_list
    2020         * @param array<string> $expected
    2121         */
    … …  
    3838         * Data provider.
    3939         *
    40          * @return array<string, array{ input_list: mixed[]|string, expected: array<string> }>
     40         * @return array<string, array{ input_list: mixed[]|string|int, expected: array<string> }>
    4141         */
    4242        public function data_wp_parse_slug_list(): array {
    … …  
    7676                                'expected'   => array( 'apple', 'banana-carrot', 'd-o-g' ),
    7777                        ),
     78                        'positive int'               => array(
     79                                'input_list' => 5,
     80                                'expected'   => array( '5' ),
     81                        ),
     82                        'negative int'               => array(
     83                                'input_list' => -5,
     84                                'expected'   => array( '5' ),
     85                        ),
     86                        'zero'                       => array(
     87                                'input_list' => 0,
     88                                'expected'   => array( '0' ),
     89                        ),
    7890                        'passed assoc array'         => array(
    7991                                'input_list' => array(
    … …  
    94106         * Data provider.
    95107         *
    96          * @return array<string, array{ input_list: mixed[]|string, expected: array<string> }>
     108         * @return array<string, array{ input_list: mixed[]|string|int, expected: array<string> }>
    97109         */
    98110        public function data_unexpected_input(): array {
Note: See TracChangeset for help on using the changeset viewer.