Make WordPress Core


Ignore:
Timestamp:
07/19/2026 04:53:46 PM (7 weeks ago)
Author:
westonruter
Message:

Code Quality: Improve typing for wp_parse_list() et al.

This adds precise @param and @return typing for the wp_parse_list(), wp_parse_id_list(), and wp_parse_slug_list() functions; native array return types are also added. It also adds casting and type guarding to guarantee the types of the values involved. Descriptions are updated to indicate that lists may not be returned, which may be unexpected given the function names; instead, sparse arrays or even associative arrays may be returned. Additionally, typically invalid ID values like zero may be in the array returned by wp_parse_id_list() and an empty string may be in the array returned by wp_parse_slug_list().

Tests are added to ensure existing behavior is preserved. This fixes 6 PHPStan errors at rule level 10.

Developed in https://github.com/WordPress/wordpress-develop/pull/12588.
Follow-up to r38832, r44546, r57737, r62647, r62771.

See #64898.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions/wpParseList.php

    r57737 r62797  
    1414         *
    1515         * @dataProvider data_wp_parse_list
     16         *
     17         * @param mixed[]|string $input_list
     18         * @param array<scalar> $expected
    1619         */
    17         public function test_wp_parse_list( $input_list, $expected ) {
    18                 $this->assertSameSets( $expected, wp_parse_list( $input_list ) );
     20        public function test_wp_parse_list( $input_list, array $expected ): void {
     21                $parsed_list = wp_parse_list( $input_list );
     22                $this->assertThat(
     23                        $parsed_list,
     24                        $this->callback(
     25                                static fn ( array $arr ) => array_all(
     26                                        $arr,
     27                                        static fn ( $v ) => is_scalar( $v )
     28                                )
     29                        ),
     30                        'Array should contain only scalars.'
     31                );
     32                $this->assertSame( $expected, $parsed_list );
    1933        }
    2034
     
    2236         * Data provider.
    2337         *
    24          * @return array[]
     38         * @return array<string, array{ input_list: mixed[]|string, expected: array<scalar> }>
    2539         */
    26         public function data_wp_parse_list() {
     40        public function data_wp_parse_list(): array {
    2741                return array(
    28                         'ids only'           => array(
     42                        'ids only'            => array(
    2943                                'input_list' => '1,2,3,4',
    3044                                'expected'   => array( '1', '2', '3', '4' ),
    3145                        ),
    32                         'slugs only'         => array(
     46                        'slugs only'          => array(
    3347                                'input_list' => 'apple,banana,carrot,dog',
    3448                                'expected'   => array( 'apple', 'banana', 'carrot', 'dog' ),
    3549                        ),
    36                         'ids and slugs'      => array(
     50                        'ids and slugs'       => array(
    3751                                'input_list' => '1,2,apple,banana',
    3852                                'expected'   => array( '1', '2', 'apple', 'banana' ),
    3953                        ),
    40                         'space after comma'  => array(
     54                        'space after comma'   => array(
    4155                                'input_list' => '1, 2,apple,banana',
    4256                                'expected'   => array( '1', '2', 'apple', 'banana' ),
    4357                        ),
    44                         'double comma'       => array(
     58                        'double comma'        => array(
    4559                                'input_list' => '1,2,apple,,banana',
    4660                                'expected'   => array( '1', '2', 'apple', 'banana' ),
    4761                        ),
    48                         'leading comma'      => array(
     62                        'leading comma'       => array(
    4963                                'input_list' => ',1,2,apple,banana',
    5064                                'expected'   => array( '1', '2', 'apple', 'banana' ),
    5165                        ),
    52                         'trailing comma'     => array(
     66                        'trailing comma'      => array(
    5367                                'input_list' => '1,2,apple,banana,',
    5468                                'expected'   => array( '1', '2', 'apple', 'banana' ),
    5569                        ),
    56                         'space before comma' => array(
     70                        'space before comma'  => array(
    5771                                'input_list' => '1,2 ,apple,banana',
    5872                                'expected'   => array( '1', '2', 'apple', 'banana' ),
    5973                        ),
    60                         'empty string'       => array(
     74                        'empty string'        => array(
    6175                                'input_list' => '',
    6276                                'expected'   => array(),
    6377                        ),
    64                         'comma only'         => array(
     78                        'comma only'          => array(
    6579                                'input_list' => ',',
    6680                                'expected'   => array(),
    6781                        ),
    68                         'double comma only'  => array(
     82                        'double comma only'   => array(
    6983                                'input_list' => ',,',
    7084                                'expected'   => array(),
     85                        ),
     86                        'passed scalar array' => array(
     87                                'input_list' => array( 'foo', true, false, 1, 3.14 ),
     88                                'expected'   => array( 'foo', true, false, 1, 3.14 ),
     89                        ),
     90                        'passed mixed array'  => array(
     91                                'input_list' => array( null, 'foo', array(), true, new stdClass(), false, 1, 3.14 ),
     92                                'expected'   => array(
     93                                        1 => 'foo',
     94                                        3 => true,
     95                                        5 => false,
     96                                        6 => 1,
     97                                        7 => 3.14,
     98                                ),
     99                        ),
     100                        'passed assoc array'  => array(
     101                                'input_list' => array(
     102                                        'foo' => 1,
     103                                        'bar' => true,
     104                                        'baz' => 3.14,
     105                                ),
     106                                'expected'   => array(
     107                                        'foo' => 1,
     108                                        'bar' => true,
     109                                        'baz' => 3.14,
     110                                ),
    71111                        ),
    72112                );
Note: See TracChangeset for help on using the changeset viewer.