Repository navigation
Fix PHPStan errors in the WordPress 7.1 and 7.2 compat shims - #86100
huzaifaalmesbah wants to merge 1 commit into
Conversation
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. 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:
Updated as activity occurs, without notifying anyone named here. Add the |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the PHPStan compatibility-shim cleanup.
No open issues 🎉
What changed in this PR
Addresses seven baselined PHPStan errors in Gutenberg’s WordPress 7.1 and 7.2 compatibility shims, advancing #86088 without intended behavior changes.
Changes:
- Replaces redundant property checks and restores REST request parameters through
set_param(). - Corrects the note-count function’s documented return type.
- Removes the corresponding PHPStan baseline entries.
| File | Description |
|---|---|
tools/phpstan/baseline.neon |
Removes suppressions for seven resolved errors. |
lib/compat/wordpress-7.2/note-reactions.php |
Documents the return type as int. |
lib/compat/wordpress-7.1/rest-api.php |
Removes the redundant post_date existence check. |
lib/compat/wordpress-7.1/class-gutenberg-rest-attachments-controller-7-1.php |
Restores array parameters using set_param(). |
lib/compat/wordpress-7.1/block-bindings.php |
Checks the inner-block count instead of object emptiness. |
Review effort: Balanced
There was a problem hiding this comment.
Let's revert these changes. They're a false positive, the same as #86110 which should be fixed in PHPStan core: phpstan/phpstan-src#6903.
If you want, it may make sense to add a separate ignoreErrors in phpstan.neon.dist like we have in core to ignore all such offsetAssign.dimType errors which have an error message which matches #^Cannot assign offset .+ to WP_REST_Request\.$#.
This can include a comment that links to the PHPStan core PR, so that once PHPStan ships with the fix, we can remove that one intentional error from being ignored.
There was a problem hiding this comment.
See #86284. With that merged, the changes here can be removed with the baselined errors still removed.
There was a problem hiding this comment.
Ok, that was merged. Merging trunk into this branch will allow you to revert these changes.
There was a problem hiding this comment.
Reverted the changes in class-gutenberg-rest-attachments-controller-7-1.php and rebased on trunk with the updated baseline. Thanks @westonruter!
In core, I see that /**
* List of inner blocks (of this same class)
*
* @since 5.5.0
* @var WP_Block_List
*/
public $inner_blocks = array();This is wrong, of course. It gets set as: $this->inner_blocks = new WP_Block_List( $this->parsed_block['innerBlocks'], $child_context, $this->registry );So it is an object, as you found. Something to fix in core as well. |
- Check whether a List Item has inner blocks with `count()`, since `empty()` is never true for a `WP_Block_List` object. - Drop the `isset()` check on `WP_Post::$post_date`, which is always set. - Document that `gutenberg_exclude_notes_from_comment_count_7_2()` always returns an int. Remove the 3 resolved entries from the PHPStan baseline. See WordPress#86088.
105674f to
f0f7d03
Compare
|
|
||
| - | ||
| message: '#^Method WP_Sync_Save_Server\:\:handle_request\(\) should return WP_Error\|WP_REST_Response but returns array\.$#' | ||
| identifier: return.type |

What?
See #86088
Fixes PHPStan errors in the WordPress 7.1 and 7.2 compat shims, and removes the resolved entries from the PHPStan baseline.
Why?
These shims run when the plugin is active on a WordPress version that does not have the corresponding Core change yet, so they are still analyzed. Their Core counterparts are implemented differently (or natively), so there is nothing to sync from or to Core. This resolves 7 of the 226 errors initially baselined in #66693.
How?
lib/compat/wordpress-7.1/block-bindings.php:empty( $instance->inner_blocks )is never true, becauseWP_Block_Listis an object. It now uses0 === count( $instance->inner_blocks ), asWP_Block_ListimplementsCountable. The output is unchanged: a List Item without inner blocks already returned early further down, when the rendered inner blocks HTML was empty. (empty.property)lib/compat/wordpress-7.1/class-gutenberg-rest-attachments-controller-7-1.php: the arraymedia_typeandmime_typeparameters are restored with$request->set_param()instead of$request[...] =.WP_REST_Request::offsetSet()callsset_param(), so the behavior is the same. (offsetAssign.dimType×2,offsetAssign.valueType×2)lib/compat/wordpress-7.1/rest-api.php: droppedisset( $post->post_date ), sinceWP_Post::$post_dateis always set. (isset.property)lib/compat/wordpress-7.2/note-reactions.php:gutenberg_exclude_notes_from_comment_count_7_2()always returns an int, so its@returnis nowintinstead ofint|null. (return.unusedType)tools/phpstan/baseline.neon.There is no change in behavior.
Testing Instructions
composer phpstanand confirm it passes with no unmatched ignored errors.composer phpstan:baselinesand confirmtools/phpstan/baseline.neonhas no further changes./wp/v2/media?media_type[]=image&media_type[]=videoand confirm both images and videos are returned.Testing Instructions for Keyboard
Not applicable. There are no UI changes.
Screenshots or screencast
Not applicable.
Use of AI Tools
I used Claude Code to identify the affected occurrences and draft the change. I reviewed the diff and take responsibility for it.