Skip to content

Fix PHPStan errors in the WordPress 7.1 and 7.2 compat shims - #86100

Open
huzaifaalmesbah wants to merge 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-compat-shims
Open

huzaifaalmesbah wants to merge 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-compat-shims

Conversation

@huzaifaalmesbah

@huzaifaalmesbah huzaifaalmesbah commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

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, because WP_Block_List is an object. It now uses 0 === count( $instance->inner_blocks ), as WP_Block_List implements Countable. 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 array media_type and mime_type parameters are restored with $request->set_param() instead of $request[...] =. WP_REST_Request::offsetSet() calls set_param(), so the behavior is the same. (offsetAssign.dimType ×2, offsetAssign.valueType ×2)
  • lib/compat/wordpress-7.1/rest-api.php: dropped isset( $post->post_date ), since WP_Post::$post_date is 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 @return is now int instead of int|null. (return.unusedType)
  • Removed the 7 corresponding entries from tools/phpstan/baseline.neon.

There is no change in behavior.

Testing Instructions

  1. Run composer phpstan and confirm it passes with no unmatched ignored errors.
  2. Run composer phpstan:baselines and confirm tools/phpstan/baseline.neon has no further changes.
  3. Optional smoke test:
    • Request /wp/v2/media?media_type[]=image&media_type[]=video and confirm both images and videos are returned.
    • Bind a List Item's content to a source (for example, pattern overrides) while it has a nested list, and confirm the nested list still renders on the front end.

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.

@huzaifaalmesbah huzaifaalmesbah added [Type] Code Quality Issues or PRs that relate to code quality No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core labels Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: huzaifaalmesbah <huzaifaalmesbah@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #86284. With that merged, the changes here can be removed with the baselined errors still removed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that was merged. Merging trunk into this branch will allow you to revert these changes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the changes in class-gutenberg-rest-attachments-controller-7-1.php and rebased on trunk with the updated baseline. Thanks @westonruter!

@westonruter

westonruter commented Nov 3, 2026 •

Copy link
Copy Markdown
Member
  • lib/compat/wordpress-7.1/block-bindings.php: empty( $instance->inner_blocks ) is never true, because WP_Block_List is an object. It now uses 0 === count( $instance->inner_blocks ), as WP_Block_List implements Countable. 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)

In core, I see that WP_Block::$inner_blocks has an initial value of array():

	/**
	 * 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.

@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The deletion leaves duplicate keys in the PHPStan baseline by removing an entry’s list-item marker and message, preventing PHPStan from loading it.

1 open issue

Review effort: Balanced


-
message: '#^Method WP_Sync_Save_Server\:\:handle_request\(\) should return WP_Error\|WP_REST_Response but returns array\.$#'
identifier: return.type

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants