Repository navigation
Code Quality: Fix the argument.unresolvableType PHPStan error and remove the baseline - #15450
mukeshpanchal27 wants to merge 6 commits into
Conversation
…()`. `wp_get_recent_posts()` still accepts an integer number of posts, which has been deprecated since 3.1.0. The `@param` tag documented `$args` as `array` only, so PHPStan concluded that `is_numeric( $args )` can never be true. It then reported that the `absint()` call in that branch receives an unresolvable type. This change documents `$args` as `array|int`, states the deprecation in its description, deletes the now empty `argument.unresolvableType` baseline, and removes its `includes` line from `phpstan.neon.dist`. There is no runtime change. See #65817. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
argument.unresolvableType PHPStan error and remove the baselineargument.unresolvableType PHPStan error and remove the baseline
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain, and the documented supported argument type aligns with removing the obsolete PHPStan baseline and configuration entry.
No open issues 🎉
What changed in this PR
This pull request fixes the remaining PHPStan argument.unresolvableType error by documenting the deprecated integer form accepted by wp_get_recent_posts(). Runtime behavior is unchanged.
Changes:
- Documents
$argsasarray|intand notes the deprecated integer usage. - Removes the resolved PHPStan baseline.
- Removes the deleted baseline from
phpstan.neon.dist.
| File | Description |
|---|---|
tests/phpstan/baselines/argument.unresolvableType.neon |
Removes the obsolete PHPStan error baseline. |
src/wp-includes/post.php |
Documents the supported deprecated integer argument form. |
phpstan.neon.dist |
Removes the deleted baseline from the PHPStan includes. |
Review effort: Lite
…vabletype-baseline # Conflicts: # phpstan.neon.dist # tests/phpstan/baselines/argument.unresolvableType.neon
argument.unresolvableType PHPStan error and remove the baselineargument.unresolvableType PHPStan error and remove the baseline
Add a hash for `$args` listing the keys whose defaults differ from those of `get_posts()`, and a `@phpstan-param` that narrows the deprecated integer form to `positive-int`. The function passes an integer through `absint()`, so a caller passing a negative number silently gets that many posts as a positive count, and one passing `0` gets the number from the `posts_per_page` option. PHPStan now flags those calls. The shape is written by hand rather than derived from the hash, since the hash cannot express `positive-int`, as in `wp_delete_term()`. It is left open with `...`, because any `WP_Query` argument is accepted. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: afd4d078-9c23-4da0-9ee0-7b1e32f41dd6
…et_posts()`. The inline `@var` on the parsed arguments only typed `fields`, so every other key was `mixed`, and at level 10 PHPStan reported that the array could not be passed to `get_posts()`, whose hash types `numberposts`, `category`, `include`, `exclude`, and `suppress_filters`. The `@var` now types those keys too. The `$args` shape gains the same keys, so callers are checked against the same contract as callers of `get_posts()`. The `include` and `exclude` defaults change from `''` to `array()`, matching `get_posts()` and the documented `int[]`. There is no runtime change, since `get_posts()` only reads either one when it is not empty. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: afd4d078-9c23-4da0-9ee0-7b1e32f41dd6
…objects. The inline `@var` on the parsed arguments pinned `fields` to `null` so that the conditional return type of `get_posts()` resolves to `WP_Post[]`. Nothing checked that claim, so a caller could pass `'fields' => 'ids'` unflagged, and with the default `ARRAY_A` output `get_object_vars()` then receives an integer and throws a `TypeError`. The `$args` shape now limits `fields` to `''` or `'all'`, so PHPStan flags callers that ask for IDs, and the `@var` repeats that type in place of `null`. There is no runtime change. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: afd4d078-9c23-4da0-9ee0-7b1e32f41dd6
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
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 WordPress/wordpress-develop#15450. Follow-up to r62648, r63024, r64163. Props mukesh27, westonruter. See #66739. Built from https://develop.svn.wordpress.org/trunk@65783 git-svn-id: http://core.svn.wordpress.org/trunk@64812 1a063a9b-81f0-0310-95a4-ce76da25c4cd
✅ Committed in r65783 (4293f64).
This PR fixes the only
argument.unresolvableTypeerror and deletestests/phpstan/baselines/argument.unresolvableType.neon. It also documents the arguments ofwp_get_recent_posts()and fixes the level 10 error on its call toget_posts(). There is no runtime change.Since r65566, PHPStan loads the baselines from
tests/phpstan/baselines/automatically, so deleting the file is enough andphpstan.neon.distdoes not change.The error
wp_get_recent_posts()still accepts an integer number of posts, which has been deprecated since 3.1.0:The
@paramtag documented$argsasarrayonly. PHPStan trusts the docblock, so it concluded thatis_numeric( $args )can never be true. Inside that branch,$argshas no possible type, and theabsint()call receives an unresolvable type.This error is not a false positive. The docblock was incomplete, because it left out a form that the function still accepts.
The fix
$argsis documented asarray|int, with the deprecation stated in the description. This followswp_get_typography_font_size_value(), whose@paramstill lists the deprecatedbool(r64163). The code does not change, because plugins can still pass an integer. No caller in core passes one.get_posts():numberposts,offset,orderby, andpost_status. It refers toget_posts()andWP_Query::parse_query()for the rest.@phpstan-paramnarrows the deprecated integer topositive-int.absint()would silently turn a negative number into a positive count, and0falls back to theposts_per_pageoption, so PHPStan now flags calls passing0, a negative number, or a numeric string. The shape is written by hand, as inwp_delete_term(), because the hash cannot expresspositive-int. It stays open with..., since anyWP_Queryargument is accepted.get_posts()are typed. The inline@varon$parsed_argstyped onlyfields, so at level 10 the other keys weremixedand did not match the shape derived from theget_posts()hash. The@varand the$argsshape now also typecategory,include,exclude, andsuppress_filters. Theincludeandexcludedefaults change from''toarray(), matchingget_posts()and its documentedint[]. This makes no runtime difference, becauseget_posts()only reads them when they are not empty.fieldsis limited to''|'all'. The@varused to pinfieldstonullso that the conditional return type ofget_posts()resolves toWP_Post[], but nothing checked it. A caller passing'fields' => 'ids'with the defaultARRAY_Aoutput makesget_object_vars()receive an integer and throw aTypeError. The$argsshape now rejects that, and the@varrepeats the same type.Testing
composer phpstan:defaultandcomposer phpstan:strictboth report no errors with the baseline deleted.wp_get_recent_posts()and its core caller inclass-wp-xmlrpc-server.phpreport no errors.wp_get_recent_posts( 5 )and array arguments pass, while0,-3,'5',array( 'numberposts' => 'x' ), andarray( 'fields' => 'ids' )are flagged. The return type still resolves toarray<int, non-empty-array<string, mixed>>forARRAY_Aandarray<WP_Post>|falseforOBJECT.src/wp-includes/post.phpas ontrunk.Trac ticket: https://core.trac.wordpress.org/ticket/66739
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (medium effort for the follow-up commits)
Used for: Finding the cause of the error, editing the docblock and baseline, writing the follow-up commits that document
$argsand type the arguments passed toget_posts(), and drafting the PR. The changes were reviewed by a human.This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.