Make WordPress Core


Ignore:
Timestamp:
08/27/2026 07:00:51 PM (6 weeks 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 ),
Note: See TracChangeset for help on using the changeset viewer.