Skip to content

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

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

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

Conversation

@hmbashar

@hmbashar hmbashar commented Dec 3, 2026 •

Copy link
Copy Markdown

✅ Committed in r65895 (28c99aa).


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 plain PHPDoc type stays WP_Ability_Category[] for both the property and get_all_registered(). The key type is described with the PHPStan-specific tags, so the PHPDoc itself does not use PHPStan-only types:

@phpstan-return array<(lowercase-string&non-empty-string)|decimal-int-string, WP_Ability_Category>
@phpstan-var    array<(lowercase-string&non-empty-string)|decimal-int-string, WP_Ability_Category>

The @return was added in r61032.

Thanks to the review comments for pointing out the numeric slug case and the @phpstan- tags; earlier revisions documented the keys as string, then int|string, then array-key.

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, westonruter, 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

* @see wp_get_ability_categories()
*
* @return array<string, WP_Ability_Category> The array of registered ability categories.
* @return array<array-key, WP_Ability_Category> The array of registered ability categories.

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.

Since array-key is a PHPStan type, probably best to not use it yet in a generic PHPDoc tag.

Also, the underlying \WP_Ability_Categories_Registry::$registered_categories is typed as just WP_Ability_Category[].

/**
* Holds the registered ability categories.
*
* @since 6.9.0
* @var WP_Ability_Category[]
*/
private $registered_categories = array();

So I think we can simplify this to make it consistent:

Suggested change
* @return array<array-key, WP_Ability_Category> The array of registered ability categories.
* @return WP_Ability_Category[] The array of registered ability categories.

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

In looking at \WP_Ability_Categories_Registry::register() I can see that the $slug parameter is typed as lowercase-string&non-empty-string. And that method has a preg_match() check that enforces that the slug matches /^[a-z0-9]+(?:-[a-z0-9]+)*$/. This means that 123 is a valid slug. As an array key, this becomes an int. So what we could do is something like this:

Suggested change
* @return array<array-key, WP_Ability_Category> The array of registered ability categories.
* @return WP_Ability_Category[] The array of registered ability categories.
*
* @phpstan-return array<(lowercase-string&non-empty-string)|decimal-int-string, WP_Ability_Category>

This would be the very most narrowed type. The same should be applied via a @phpstan-var on WP_Ability_Categories_Registry::$registered_categories.

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.

Fixed in c819e39. @return is now WP_Ability_Category[], with the key type moved to @phpstan-return, so array-key is no longer used in a generic PHPDoc tag.

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.

Applied in c819e39: @return WP_Ability_Category[] plus @phpstan-return array<(lowercase-string&non-empty-string)|decimal-int-string, WP_Ability_Category>, and the same type via @phpstan-var on $registered_categories. Strict and default PHPStan report no errors, and the strict return.type baseline stays removed. I also updated the PR description.

hmbashar and others added 15 commits December 3, 2026 23:13
…d ability categories keys.

Keep the plain `WP_Ability_Category[]` PHPDoc types and describe the slug keys, including numeric slugs that PHP stores as integers, with the PHPStan-specific tags.
…e-int.

PHP casts a decimal integer string array key such as `"123"` or `"0"` to an int, so an all-digit slug, which the slug pattern allows, is stored under an int key. The `decimal-int-string` in the key type described a key that cannot exist, and PHPStan folded it away. Use `non-negative-int` instead, since the pattern disallows a leading dash and `"0"` is a valid slug.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
…tered.

Only a lowercase non-empty slug can be registered, so when `is_registered()` returns true the slug can be narrowed to `lowercase-string&non-empty-string` for callers.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
… ability category.

Calling `get_registered()` from `unregister()` emitted its own "not found" `_doing_it_wrong()` notice before the one from `unregister()`. Check the registered categories with `isset()` directly instead, as `get_registered()` does, so that PHPStan still knows the offset exists.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
…ssertion.

With a plain `@phpstan-assert-if-true`, PHPStan subtracts the asserted type from `$slug` when the method returns false. In `register()`, where `$slug` is already `lowercase-string&non-empty-string`, that narrowed it to `never` after the "already registered" guard, so the rest of the method went unanalyzed. The `=` form narrows only when the method returns true.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
…urn docs.

The published `@return` had dropped from `array<string, WP_Ability_Category>` to `WP_Ability_Category[]`, losing that the array is keyed by slug. Document the keys as `string|int`, since a numeric slug is stored under an int key.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
…ries property.

Match the `@var` of `$registered_categories` to the `@return` of `get_all_registered()`, which returns it.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
…s()` return.

The function returns the registry's categories, where a numeric slug such as `'123'` is stored under an integer key. Match the return types of `WP_Ability_Categories_Registry::get_all_registered()`, and note the integer keys, so that callers passing a key back to a function that takes a string slug know to expect them.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
…returns true.

Carry the `WP_Ability_Categories_Registry::is_registered()` assertion through to its wrapper, so that callers of the function also get `$slug` narrowed to `lowercase-string&non-empty-string`. This resolves an `argument.type` error in the REST categories controller.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
PHP casts a numeric string array key to an integer, so a category registered as `'123'` is stored under the integer key `123`. Verify that the registry still finds, returns, and unregisters it by its string slug without notices, and that `get_all_registered()` returns the integer key, as its return type now documents.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
…ability_categories()` docs.

A numeric slug comes back as an integer key, which throws a `TypeError` when passed to a `string` parameter under strict types. Direct callers to `WP_Ability_Category::get_slug()` when they need the slug as a string.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: f695cd97-2f83-4778-8c6d-ee3c1ab7ac5a
@westonruter

Copy link
Copy Markdown
Member

I further refined the types and fixed some offsetAccess.notFound errors which are reported when reportPossiblyNonexistentGeneralArrayOffset is enabled.

wporg-sync pushed a commit that referenced this pull request Dec 3, 2026
`WP_Ability_Categories_Registry::get_all_registered()` was documented as returning an array keyed by strings, but the slug check in `register()` accepts an all-digit slug such as `'123'`, which PHP stores under an integer key. The key type of the returned array, and of the property holding it, now includes integers, while the PHPStan-specific tags narrow it to the lowercase slugs and non-negative integers that can actually occur. This empties the strict return.type baseline, so it is removed. The `wp_get_ability_categories()` wrapper documents the same keys and points callers that need the slug as a string to `WP_Ability_Category::get_slug()`.

Additionally, `is_registered()` and `wp_has_ability_category()` now assert the narrowed slug type when they return true, and `unregister()` and `get_registered()` check the array with `isset()` directly, so that PHPStan knows the entry exists when `reportPossiblyNonexistentGeneralArrayOffset` is enabled. Behavior is unchanged.

Developed in #16448.
Follow-up to r61032, r65668.

Props hmbashar, westonruter, lancewillett.
See #66739.


git-svn-id: https://develop.svn.wordpress.org/trunk@65895 602fd350-edb4-49c9-b593-d223f7449a82
@westonruter westonruter closed this Dec 3, 2026
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Dec 3, 2026
`WP_Ability_Categories_Registry::get_all_registered()` was documented as returning an array keyed by strings, but the slug check in `register()` accepts an all-digit slug such as `'123'`, which PHP stores under an integer key. The key type of the returned array, and of the property holding it, now includes integers, while the PHPStan-specific tags narrow it to the lowercase slugs and non-negative integers that can actually occur. This empties the strict return.type baseline, so it is removed. The `wp_get_ability_categories()` wrapper documents the same keys and points callers that need the slug as a string to `WP_Ability_Category::get_slug()`.

Additionally, `is_registered()` and `wp_has_ability_category()` now assert the narrowed slug type when they return true, and `unregister()` and `get_registered()` check the array with `isset()` directly, so that PHPStan knows the entry exists when `reportPossiblyNonexistentGeneralArrayOffset` is enabled. Behavior is unchanged.

Developed in WordPress/wordpress-develop#16448.
Follow-up to r61032, r65668.

Props hmbashar, westonruter, lancewillett.
See #66739.

Built from https://develop.svn.wordpress.org/trunk@65895


git-svn-id: http://core.svn.wordpress.org/trunk@64924 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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.

4 participants