Repository navigation
Conversation
|
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. |
There was a problem hiding this comment.
🟡 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.typebaseline.
| 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
left a comment
There was a problem hiding this comment.
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.
|
Thanks @lancewillett, fixed in ddf52b7. |
lancewillett
left a comment
There was a problem hiding this comment.
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
| * @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. |
There was a problem hiding this comment.
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[].
So I think we can simplify this to make it consistent:
| * @return array<array-key, WP_Ability_Category> The array of registered ability categories. | |
| * @return WP_Ability_Category[] The array of registered ability categories. |
There was a problem hiding this comment.
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:
| * @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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
…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
|
I further refined the types and fixed some |
`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
`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

✅ Committed in r65895 (28c99aa).
This resolves the last strict
return.typePHPStan 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 returningarray<string, WP_Ability_Category>, but it returns$this->registered_categories, which is documented asWP_Ability_Category[]. At level 10 PHPStan does not accept that array, whose keys are of any type, as one keyed by string, and reportsshould 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 thestringkey in the return annotation was too narrow.The plain PHPDoc type stays
WP_Ability_Category[]for both the property andget_all_registered(). The key type is described with the PHPStan-specific tags, so the PHPDoc itself does not use PHPStan-only types:The
@returnwas added in r61032.Thanks to the review comments for pointing out the numeric slug case and the
@phpstan-tags; earlier revisions documented the keys asstring, thenint|string, thenarray-key.Testing
composer phpstan:defaultandcomposer phpstan:strictreport no errors.composer phpstan:baselines -- --identifier=return.type --config=phpstan-strict.neon.distreports no errors, sotests/phpstan/baselines-strict/return.type.neonis deleted. The defaultreturn.typebaseline is unchanged.class-wp-ability-categories-registry.php.vendor/bin/phpunit --group abilities-apipasses (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.