Repository navigation
Conversation
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.
|
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. |
| * @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. |
There was a problem hiding this comment.
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:
wordpress-develop/src/wp-includes/meta.php
Lines 1229 to 1232 in 72050b6
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:
wordpress-develop/src/wp-includes/meta.php
Lines 712 to 716 in f7555ee
Here it seems an is_array() check is warranted.
There was a problem hiding this comment.
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.
| * '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 |
There was a problem hiding this comment.
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().
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| * @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. |
There was a problem hiding this comment.
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:
| * @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. |
There was a problem hiding this comment.
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.
This resolves the only default
return.typePHPStan 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 returningarray|false, but it can also returntrue. Theupdate_{$meta_type}_metadata_cachefilter can short-circuit the function, and when it does the function returns(bool) $check. PHPStan reportsshould return array|false but returns bool.The fix
The
@returnofupdate_meta_cache()is nowarray<int, array<string, string[]>>|bool: the cache keyed by object ID, then by meta key, holding the list of values, orfalseon 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()andupdate_sitemeta_cache()return the result ofupdate_meta_cache()unchanged, so their@returnis 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 givesnullwithout a notice.Testing
composer phpstan:defaultandcomposer phpstan:strictreport no errors.composer phpstan:baselines -- --identifier=return.typeleaves 22 errors in the baseline, one fewer than the 23 ontrunk, and only theupdate_meta_cache()entry is removed.meta.php,post.php,taxonomy.phpandms-site.php.vendor/bin/phpunit --group metapasses (511 tests), as doestests/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.