Make WordPress Core

Changeset 63522


Ignore:
Timestamp:
09/07/2026 11:14:20 PM (6 weeks ago)
Author:
westonruter
Message:

Code Quality: Improve typing for wp_parse_args().

Both the parameters and the return value have been documented as a bare array since the function entered trunk in r5234. They now describe the string-keyed arrays it is used with, and the signature gains a native array return type. Every caller treats those keys as strings, but the function cannot guarantee it, since get_object_vars() reports a numeric-string property under an integer key and parse_str() reads 0=a as one, so the @phpstan-return records what can actually come back.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13430.
Follow-up to r5234, r47429.

See #65817, #65860.

Location:
trunk
Files:
2 edited

Legend:

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

    r63511 r63522  
    50225022 * to be merged into another array.
    50235023 *
     5024 * The keys of the returned array are documented as strings, since that is what
     5025 * callers mean by them, but they are not promised as such to static analysis.
     5026 * Integer keys remain reachable through arguments that are perfectly valid:
     5027 * `json_decode( '{"0":"a"}' )` is an object whose properties `get_object_vars()`
     5028 * reports under an integer key, and `parse_str()` reads `0=a` as one as well.
     5029 *
    50245030 * @since 2.2.0
    50255031 * @since 2.3.0 `$args` can now also be an object.
    50265032 *
    5027  * @param string|array|object $args     Value to merge with $defaults.
    5028  * @param array               $defaults Optional. Array that serves as the defaults.
    5029  *                                      Default empty array.
    5030  * @return array Merged user defined values with defaults.
    5031  */
    5032 function wp_parse_args( $args, $defaults = array() ) {
     5033 * @param string|array<string, mixed>|object $args     Value to merge with $defaults.
     5034 * @param array<string, mixed>               $defaults Optional. Array that serves as the defaults.
     5035 *                                                     Default empty array.
     5036 * @return array<string, mixed> Merged user defined values with defaults.
     5037 * @phpstan-return array<array-key, mixed>
     5038 */
     5039function wp_parse_args( $args, $defaults = array() ): array {
    50335040        if ( is_object( $args ) ) {
    50345041                $parsed_args = get_object_vars( $args );
  • trunk/tests/phpstan/baselines/argument.type.neon

    r63510 r63522  
    11301130                        path: ../../../src/wp-includes/pluggable.php
    11311131                -
    1132                         message: '#^Parameter \#1 \$args of function get_pages expects array\{child_of\?\: int, sort_order\?\: string, sort_column\?\: string, hierarchical\?\: bool, exclude\?\: array\<int\>, include\?\: array\<int\>, meta_key\?\: string, meta_value\?\: string, \.\.\., \.\.\.\}\|string, non\-empty\-array given\.$#'
     1132                        message: '#^Parameter \#1 \$args of function get_pages expects array\{child_of\?\: int, sort_order\?\: string, sort_column\?\: string, hierarchical\?\: bool, exclude\?\: array\<int\>, include\?\: array\<int\>, meta_key\?\: string, meta_value\?\: string, \.\.\., \.\.\.\}\|string, non\-empty\-array\<mixed\> given\.$#'
    11331133                        identifier: argument.type
    11341134                        count: 1
    … …  
    12501250                        path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php
    12511251                -
     1252                        message: '#^Parameter \#1 \$args of function wp_parse_args expects array\<string, mixed\>\|object\|string, list\<array\> given\.$#'
     1253                        identifier: argument.type
     1254                        count: 1
     1255                        path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php
     1256                -
    12521257                        message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int given\.$#'
    12531258                        identifier: argument.type
Note: See TracChangeset for help on using the changeset viewer.