Docs: Describe the shape of post type, taxonomy and capability data - #13220
Docs: Describe the shape of post type, taxonomy and capability data#13220swissspidy wants to merge 1 commit into
Conversation
Documents the types of several values that are currently only described as `array`, `stdClass` or `bool[]`, so static analysis can see their contents: - `get_post_type_labels()` returns its labels in the hash notation already used by `get_taxonomy_labels()`, instead of a prose list. Two labels the returned object always carries were missing from that list: `name_admin_bar`, always set by `_get_custom_object_labels()`, and `template_name`, added in 6.6.0. - `get_taxonomy_labels()` gains the same two, `menu_name` and `name_admin_bar`, which are always present but were never documented. Core reads `menu_name` off a taxonomy in `wp-admin/menu.php`. - Labels whose default is `null` for one of the two hierarchies, as recorded in `WP_Post_Type::get_default_labels()` and `WP_Taxonomy::get_default_labels()`, are documented as `string|null` rather than `string`. - `WP_Taxonomy::$cap` lists the four capabilities it holds, mirroring the `capabilities` argument of `register_taxonomy()`, and `WP_Post_Type::$cap` points at `get_post_type_capabilities()`, where its own list already lives. - `WP_User::$caps`, `WP_User::$allcaps` and `WP_Role::$capabilities` are keyed by capability name, so `array<string, bool>` rather than `bool[]`. - `register_setting()` and `get_registered_settings()` document `sanitize_callback` as `callable|null`, which is its default, and the latter adds the `group` key that `register_setting()` always stores. Docblock changes only, no behavior change. Refs szepeviktor/phpstan-wordpress#274 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0122HqysjDiq2jSHebbSSu6A
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. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @claude. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. 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. |
| * @since 4.6.0 | ||
| * @var stdClass $cap | ||
| */ | ||
| public $cap; |
There was a problem hiding this comment.
I really want to figure out a way to type this.
| * @type string $edit_terms Capability to edit terms. Default 'manage_categories'. | ||
| * @type string $delete_terms Capability to delete terms. Default 'manage_categories'. | ||
| * @type string $assign_terms Capability to assign terms. Default 'edit_posts'. | ||
| * } |
There was a problem hiding this comment.
I think we should add the PHPStan type as well, as otherwise this cannot help with static analysis.
|
Correcting the WP dev docs is good, but what would be greater is adding the PHPStan types. Or, I wonder if we could add an extension that would teach PHPStan how to interpret the WordPress flavor of phpdoc. |
|
Good point. To a degree it's possible. Normally the WP flavor is transformed into PHPStan types in the wordpress-stubs project, that's how I use it. But adding PHPStan types directly would help both internally and externally. And an extension could be reusable too and help reduce docs duplication. |
Documents the types of several values that are currently only described as
array,stdClassorbool[], so their contents are visible both to anyone reading the docs and to static analysis. Docblock changes only, no behavior change.Three of these look like genuine documentation bugs rather than gaps:
get_taxonomy_labels()omits two labels its object always carries._get_custom_object_labels()always setsname_admin_bar, and both label builders addmenu_nameto the defaults before merging. Core itself reads$taxonomy->labels->menu_nameinwp-admin/menu.php.get_post_type_labels()documentsmenu_name, but notname_admin_bar, and nottemplate_nameeither, which its own@since 6.6.0line mentions.nullare documented asstring.WP_Post_Type::get_default_labels()andWP_Taxonomy::get_default_labels()are both typed(string|null)[][], and for eight taxonomy labels and one post type label the default really isnullfor one of the two hierarchies:popular_itemson a hierarchical taxonomy,parent_item_colonon a non-hierarchical post type, and so on. Those becomestring|null.get_registered_settings()omits thegroupkey.register_setting()always stores it through its defaults. The same docblock, andregister_setting()itself, describesanitize_callbackascallablewhen its default isnull.Alongside those:
get_post_type_labels()describes its return value with the same hash notationget_taxonomy_labels()already uses, instead of a prose list, so the two read alike.WP_Taxonomy::$caplists the four capabilities it holds, mirroring thecapabilitiesargument ofregister_taxonomy().WP_Post_Type::$capgains a@see get_post_type_capabilities(), where its own list already lives.WP_User::$caps,WP_User::$allcapsandWP_Role::$capabilitiesare keyed by capability name, soarray<string, bool>rather thanbool[].Verified with
composer phpstan, at the configured level 5 with the existing baselines andreportUnmatchedIgnoredErrors: true: no new errors, and no baseline drift.Trac ticket: https://core.trac.wordpress.org/ticket/65817
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Used for: comparing the existing docblocks against the code that builds these objects, and drafting the documentation changes in this pull request.
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.