Skip to content

Commit 4293f64

Browse files
committed
Code Quality: Fix the $args types of wp_get_recent_posts().
The `$args` parameter was documented as accepting only an array, even though a deprecated integer number of posts is still accepted. PHPStan therefore concluded that the deprecated branch could never run, and reported the value passed to `absint()` there as an unresolvable type. Documenting the integer form, as was done for `wp_get_typography_font_size_value()` in r64163, empties the `argument.unresolvableType` baseline, so it is removed. For static analysis the integer is narrowed to `positive-int`, since `absint()` silently turns a negative number into a positive count and `0` falls back to the `posts_per_page` option. A hash now documents the arguments whose defaults differ from those of `get_posts()`. At level 10 the parsed arguments could not be passed to `get_posts()`, because the inline `@var` typed only `fields`. It now types the keys that `get_posts()` documents. Instead of being asserted to be `null`, `fields` is limited to `''` or `'all'`, since a caller asking for IDs would make `get_object_vars()` throw a `TypeError` with the default `ARRAY_A` output. The `include` and `exclude` defaults become empty arrays to match their documented type, which has no runtime effect. Developed in #15450. Follow-up to r62648, r63024, r64163. Props mukesh27, westonruter. See #66739. git-svn-id: https://develop.svn.wordpress.org/trunk@65783 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 20f9614 commit 4293f64

2 files changed

Lines changed: 31 additions & 32 deletions

File tree

‎src/wp-includes/post.php‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4708,13 +4708,37 @@ function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array(
47084708
*
47094709
* @see get_posts()
47104710
*
4711-
* @param array $args Optional. Arguments to retrieve posts. Default empty array.
4712-
* @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which
4713-
* correspond to a WP_Post object or an associative array, respectively.
4714-
* Default ARRAY_A.
4711+
* @param array|int $args {
4712+
* Optional. Arguments to retrieve posts. See {@see get_posts()} and
4713+
* {@see WP_Query::parse_query()} for all available arguments. Those listed here
4714+
* have defaults that differ. Passing an integer number of posts is deprecated
4715+
* since 3.1.0. Default empty array.
4716+
*
4717+
* @type int $numberposts Total number of posts to retrieve. Accepts -1 for all.
4718+
* Default 10.
4719+
* @type int $offset The number of posts to offset before retrieval. Default 0.
4720+
* @type string|array $orderby Sort retrieved posts by parameter. Default 'post_date'.
4721+
* @type string|string[] $post_status A post status (string) or array of post statuses.
4722+
* Default 'draft, publish, future, pending, private'.
4723+
* }
4724+
* @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which
4725+
* correspond to a WP_Post object or an associative array, respectively.
4726+
* Default ARRAY_A.
47154727
* @return array|false Array of recent posts, where the type of each element is determined
47164728
* by the `$output` parameter. Empty array on failure.
47174729
*
4730+
* @phpstan-param positive-int|array{
4731+
* numberposts?: int,
4732+
* offset?: int,
4733+
* orderby?: string|array<mixed>,
4734+
* post_status?: string|string[],
4735+
* category?: int|string,
4736+
* include?: int[],
4737+
* exclude?: int[],
4738+
* suppress_filters?: bool,
4739+
* fields?: ''|'all',
4740+
* ...
4741+
* } $args
47184742
* @phpstan-param 'OBJECT'|'ARRAY_A' $output
47194743
* @phpstan-return (
47204744
* $output is 'ARRAY_A' ? array<int, non-empty-array<string, mixed>> : WP_Post[]|false
@@ -4734,16 +4758,16 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
47344758
'category' => 0,
47354759
'orderby' => 'post_date',
47364760
'order' => 'DESC',
4737-
'include' => '',
4738-
'exclude' => '',
4761+
'include' => array(),
4762+
'exclude' => array(),
47394763
'meta_key' => '',
47404764
'meta_value' => '',
47414765
'post_type' => 'post',
47424766
'post_status' => 'draft, publish, future, pending, private',
47434767
'suppress_filters' => true,
47444768
);
47454769

4746-
/** @var array{ fields: null, ... } $parsed_args */
4770+
/** @var array{ numberposts: int, category: int|string, include: int[], exclude: int[], suppress_filters: bool, fields?: ''|'all', ... } $parsed_args */
47474771
$parsed_args = wp_parse_args( $args, $defaults );
47484772

47494773
$results = get_posts( $parsed_args );

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)