Make WordPress Core


Ignore:
Timestamp:
07/19/2026 04:53:46 PM (4 months 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/wpParseIdList.php

    r57737 r62797  
    1616         * @dataProvider data_wp_parse_id_list
    1717         * @dataProvider data_unexpected_input
     18         *
     19         * @param mixed[]|string $input_list
     20         * @param array<non-negative-int> $expected
    1821         */
    19         public function test_wp_parse_id_list( $input_list, $expected ) {
    20                 $this->assertSameSets( $expected, wp_parse_id_list( $input_list ) );
     22        public function test_wp_parse_id_list( $input_list, array $expected ): void {
     23                $parsed_list = wp_parse_id_list( $input_list );
     24                $this->assertThat(
     25                        $parsed_list,
     26                        $this->callback(
     27                                static fn ( array $arr ) => array_all(
     28                                        $arr,
     29                                        static fn ( $v ) => is_int( $v ) && $v >= 0
     30                                )
     31                        ),
     32                        'Array should contain only non-negative ints.'
     33                );
     34                $this->assertSame( $expected, $parsed_list );
    2135        }
    2236
    … …  
    2438         * Data provider.
    2539         *
    26          * @return array[]
     40         * @return array<string, array{ input_list: mixed[]|string, expected: array<non-negative-int> }>
    2741         */
    28         public function data_wp_parse_id_list() {
     42        public function data_wp_parse_id_list(): array {
    2943                return array(
    3044                        'regular'                  => array(
    … …  
    3852                        'duplicate id in a string' => array(
    3953                                'input_list' => '1,2,2,3,4',
    40                                 'expected'   => array( 1, 2, 3, 4 ),
     54                                'expected'   => array(
     55                                        0 => 1,
     56                                        1 => 2,
     57                                        3 => 3,
     58                                        4 => 4,
     59                                ),
    4160                        ),
    4261                        'duplicate id in an array' => array(
    … …  
    6281         * Data provider.
    6382         *
    64          * @return array[]
     83         * @return array<string, array{ input_list: mixed[]|string, expected: array<non-negative-int> }>
    6584         */
    66         public function data_unexpected_input() {
     85        public function data_unexpected_input(): array {
    6786                return array(
    6887                        'string with commas' => array(
    … …  
    98117                                'expected'   => array( 1, 2, 0 ),
    99118                        ),
     119                        'array with array'   => array(
     120                                'input_list' => array( 1, array(), 2 ),
     121                                'expected'   => array(
     122                                        0 => 1,
     123                                        2 => 2,
     124                                ),
     125                        ),
     126                        'passed assoc array' => array(
     127                                'input_list' => array(
     128                                        'one'   => 1,
     129                                        'two'   => '2',
     130                                        'three' => '3 is company',
     131                                ),
     132                                'expected'   => array(
     133                                        'one'   => 1,
     134                                        'two'   => 2,
     135                                        'three' => 3,
     136                                ),
     137                        ),
    100138                );
    101139        }
Note: See TracChangeset for help on using the changeset viewer.