Skip to content

Commit 41a4f4f

Browse files
committed
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 WordPress#13430. Follow-up to r5234, r47429. See #65817, #65860. git-svn-id: https://develop.svn.wordpress.org/trunk@63522 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4035d38 commit 41a4f4f

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎src/wp-includes/functions.php‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5021,15 +5021,22 @@ function smilies_init() {
50215021
* This function is used throughout WordPress to allow for both string or array
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.
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>
50315038
*/
5032-
function wp_parse_args( $args, $defaults = array() ) {
5039+
function wp_parse_args( $args, $defaults = array() ): array {
50335040
if ( is_object( $args ) ) {
50345041
$parsed_args = get_object_vars( $args );
50355042
} elseif ( is_array( $args ) ) {

‎tests/phpstan/baselines/argument.type.neon‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ parameters:
11291129
count: 1
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
11351135
path: ../../../src/wp-includes/post-template.php
@@ -1248,6 +1248,11 @@ parameters:
12481248
identifier: argument.type
12491249
count: 1
12501250
path: ../../../src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php
1251+
-
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
12511256
-
12521257
message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int given\.$#'
12531258
identifier: argument.type

0 commit comments

Comments
 (0)