Skip to content

Code Quality: Resolve the return.type PHPStan error in update_meta_cache() - #16453

Open
hmbashar wants to merge 3 commits into
WordPress:trunkfrom
hmbashar:66739-phpstan-return-type-meta-cache
Open

hmbashar wants to merge 3 commits into
WordPress:trunkfrom
hmbashar:66739-phpstan-return-type-meta-cache

Conversation

@hmbashar

@hmbashar hmbashar commented Dec 3, 2026 •

Copy link
Copy Markdown

This resolves the only default return.type PHPStan error in the metadata cache functions and removes its entry from the baseline. Only docblocks change; there is no runtime change.

The error

update_meta_cache() is documented as returning array|false, but it can also return true. The update_{$meta_type}_metadata_cache filter can short-circuit the function, and when it does the function returns (bool) $check. PHPStan reports should return array|false but returns bool.

The fix

The @return of update_meta_cache() is now array<int, array<string, string[]>>|bool: the cache keyed by object ID, then by meta key, holding the list of values, or false on failure, or a boolean when the filter short-circuits the update. The short-circuit and the (bool) cast were added together with the filter in r43982 (#44467), so the documented type has been too narrow since then. The array shape was checked by printing the real return value for a post with several values per key.

update_postmeta_cache(), update_termmeta_cache() and update_sitemeta_cache() return the result of update_meta_cache() unchanged, so their @return is updated in the same way.

The (bool) cast and the function bodies are left as they are, so existing callers and filters behave exactly as before. Callers that read the result with $cache[ $object_id ] ?? null (get_metadata_raw(), metadata_exists()) are not affected when the filter short-circuits, because ?? on a boolean gives null without a notice.

Testing

  • composer phpstan:default and composer phpstan:strict report no errors.
  • composer phpstan:baselines -- --identifier=return.type leaves 22 errors in the baseline, one fewer than the 23 on trunk, and only the update_meta_cache() entry is removed.
  • PHPCS reports no issues on meta.php, post.php, taxonomy.php and ms-site.php.
  • vendor/bin/phpunit --group meta passes (511 tests), as does tests/phpunit/tests/comment/metaCache.php (11 tests), which covers the short-circuit filter.

Follow-up to r43982.

Trac ticket: https://core.trac.wordpress.org/ticket/66739

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Written with AI and manually reviewed by me.


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.

The `update_{$meta_type}_metadata_cache` filter can short-circuit the function, which then returns a boolean. Update the `@return` of the function and of the post, term and site wrappers, and remove the matching `return.type` baseline entry.
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

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:

Props hmbashar, westonruter.

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 or WordPress.org 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.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/meta.php Outdated
Comment on lines +1185 to +1186
* @return array|false Metadata cache for the specified objects, or false on failure.
* @return array|bool Metadata cache for the specified objects, false on failure, or a boolean
* if the update was short-circuited by the filter.

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.

We should do some code archeology on this. Granted, it has been in WP for a long time now, so we probably can't change it, but given this:

$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
if ( null !== $check ) {
return (bool) $check;
}

I wonder if a filter should be able to return an array itself and for this to be returned instead of bool.

In looking at calls to update_meta_cache(), I can see the deprecated get_user_metavalues() assumes that the return value is always an array which will fail in the foreach.

I can also see that get_metadata_raw() is seemingly assuming that an array is returned:

// A cached value that is not an array is unusable, treat it as a cache miss.
if ( ! $meta_cache || ! is_array( $meta_cache ) ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[ $object_id ] ?? null;
}

Here it seems an is_array() check is warranted.

@hmbashar hmbashar Dec 3, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Archaeology: the filter and the (bool) $check return were added together in r43982 (Core-44467), so the function has always returned a boolean when the filter short-circuits, whatever the filter returned. I left that behavior as it is and documented it. On the callers: get_metadata_raw() and metadata_exists() read the result as $meta_cache[ $object_id ] ?? null, and ?? on a boolean gives null without a notice (checked with PHP), so an is_array() check is not needed there. Only the deprecated get_user_metavalues() loops over the result, and it would warn only if a filter short-circuits the user meta cache. I did not touch it.

Comment thread src/wp-includes/meta.php Outdated
* 'user', or any other object type with an associated meta table.
* @param string|int[] $object_ids Array or comma delimited list of object IDs to update cache for.
* @return array|false Metadata cache for the specified objects, or false on failure.
* @return array|bool Metadata cache for the specified objects, false on failure, or a boolean

@westonruter westonruter Dec 3, 2026 •

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.

We should further narrow array while we're here. It's not specific enough to be helpful. I think it is array<int, string[]>. Needs to be double-checked, and then also applied to the return types for the callers of update_meta_cache().

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.

Could be further narrowed as array<positive-int, string[]>, since I believe the top level array keys are all $mpid, which are auto-incremented IDs. But this is probably too much specificity needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Narrowed in db7c591. The real shape is one level deeper than array<int, string[]>: object ID, then meta key, then the list of values. I printed the return value for a post with several values per key, for example {"4":{"a":["x","y"],"b":[""]}}. The @return is now array<int, array<string, string[]>>|bool, applied to update_meta_cache() and to update_postmeta_cache(), update_termmeta_cache() and update_sitemeta_cache(). Strict and default PHPStan report no errors.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed that positive-int is more specific than needed here, so I kept int for the object ID keys.

The returned cache is keyed by object ID and holds the values of each meta key as strings.
Comment thread src/wp-includes/meta.php Outdated
Comment on lines +1185 to +1186
* @return array<int, array<string, string[]>>|bool Metadata cache for the specified objects, false on failure,
* or a boolean if the update was short-circuited by the filter.

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.

Is string for the keys right here? Given that a meta key can be any string, including digits like '123', PHP will automatically coerce them into integers. So this would be more accurate as:

Suggested change
* @return array<int, array<string, string[]>>|bool Metadata cache for the specified objects, false on failure,
* or a boolean if the update was short-circuited by the filter.
* @return array<int, array<string|int, string[]>>|bool Metadata cache for the specified objects, false on failure,
* or a boolean if the update was short-circuited by the filter.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, fixed in ba9a0a4. The meta key level is now string|int in update_meta_cache() and in the three wrappers, since a numeric meta key such as '123' is stored under an integer key. Strict and default PHPStan report no errors.

…rn type.

A numeric meta key such as `123` is stored under an integer array key.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants