Skip to content

Code Quality: Resolve the strict return.type PHPStan error - #16448

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

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

Conversation

@hmbashar

@hmbashar hmbashar commented Dec 3, 2026 •

Copy link
Copy Markdown

This resolves the last strict return.type PHPStan error and removes its baseline. Only a docblock changes; there is no runtime change.

The error

WP_Ability_Categories_Registry::get_all_registered() is documented as returning array<string, WP_Ability_Category>, but it returns $this->registered_categories, which is documented as WP_Ability_Category[]. At level 10 PHPStan does not accept that array, whose keys are of any type, as one keyed by string, and reports should return array<string, WP_Ability_Category> but returns array<WP_Ability_Category>.

The fix

The slug check in register() (/^[a-z0-9]+(?:-[a-z0-9]+)*$/) accepts all-numeric slugs such as '123', and PHP stores those under integer keys ($this->registered_categories[ $slug ] = $category;). The keys can therefore be integers or strings, so the string key in the return annotation was too narrow. The @return of get_all_registered() is now array<array-key, WP_Ability_Category>, which matches the property. The property docblock is unchanged. The @return was added in r61032.

Thanks to the review comments for pointing out the numeric slug case; an earlier revision documented the property as string-keyed.

Testing

  • composer phpstan:default and composer phpstan:strict report no errors.
  • composer phpstan:baselines -- --identifier=return.type --config=phpstan-strict.neon.dist reports no errors, so tests/phpstan/baselines-strict/return.type.neon is deleted. The default return.type baseline is unchanged.
  • PHPCS reports no issues on class-wp-ability-categories-registry.php.
  • vendor/bin/phpunit --group abilities-api passes (1146 tests).

Follow-up to r61032, r65668.

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.

@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, lancewillett.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

PHP converts numeric category slugs to integer array keys, so the registry annotations must allow int|string keys and the baseline must be regenerated.

1 open issue
What changed in this PR

This PR addresses the remaining strict PHPStan return.type error in the ability categories registry without changing runtime behavior.

Changes:

  • Documents the registry property as a string-keyed array to match the method’s return annotation.
  • Deletes the strict return.type baseline.
File Description
tests/​phpstan/​baselines-strict/​return.type.neon Removes the strict return-type suppression.
src/​wp-includes/​abilities-api/​class-wp-ability-categories-registry.php Updates the property’s PHPDoc key type.

Review effort: Balanced

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

Could you widen the return annotation on get_all_registered() instead? I registered the valid slugs '123', '007' and 'tools' through wp_register_ability_category(). The public getter returned the integer key 123 and the string keys '007' and 'tools'. A caller iterating those keys can receive either type.

I kept the existing property annotation, changed the method to @return array<array-key, WP_Ability_Category>, and removed the same baseline. Strict PHPStan passed with that change.

AI review · gpt-6.2-astra

…s annotations.

Numeric slugs such as `123` pass the slug validation and PHP stores them under integer keys.
…ray-key.

Numeric slugs such as `123` are stored under integer keys, so the keys can be integers or strings. The property annotation is left as it was.
@hmbashar

hmbashar commented Dec 3, 2026

Copy link
Copy Markdown
Author

Thanks @lancewillett, fixed in ddf52b7. get_all_registered() now returns array<array-key, WP_Ability_Category>, the property annotation is unchanged, and the strict return.type baseline stays removed. Strict and default PHPStan report no errors. I also updated the PR description.

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

Verified the revised getter annotation against the public registry. It covers both the integer key for '123' and the string key for '007'. Strict PHPStan passes with the baseline removed. Thanks for keeping the fix to the getter.

AI review · gpt-6.2-astra

Copilot AI left a comment

Copy link
Copy Markdown

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 remain for the return-annotation update and obsolete baseline removal.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

3 participants