Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ff23eac
Code Quality: Resolve the strict `return.type` PHPStan error.
hmbashar Dec 3, 2026
405481f
Abilities API: Allow integer keys in the registered ability categorie…
hmbashar Dec 3, 2026
ddf52b7
Abilities API: Widen the get_all_registered() return annotation to ar…
hmbashar Dec 3, 2026
c819e39
Abilities API: Use @phpstan-return and @phpstan-var for the registere…
hmbashar Dec 3, 2026
a30d74b
Merge branch 'trunk' into 66739-phpstan-strict-return-type
westonruter Dec 3, 2026
848b8df
Fix offsetAccess.notFound in unregister() method
westonruter Dec 3, 2026
04ca2b8
Fix strict offsetAccess.notFound error in get_registered() method
westonruter Dec 3, 2026
648bf08
Abilities API: Type numeric ability category slug keys as non-negativ…
westonruter Dec 3, 2026
d0b81c8
Abilities API: Assert the slug type when an ability category is regis…
westonruter Dec 3, 2026
b8cbebd
Abilities API: Avoid a duplicate notice when unregistering an unknown…
westonruter Dec 3, 2026
58a0138
Abilities API: Use the equality form for the `is_registered()` slug a…
westonruter Dec 3, 2026
8f06efb
Abilities API: Restore the key type in the `get_all_registered()` ret…
westonruter Dec 3, 2026
5485724
Abilities API: Document the key type of the registered ability catego…
westonruter Dec 3, 2026
e730ab3
Abilities API: Document integer keys in the `wp_get_ability_categorie…
westonruter Dec 3, 2026
a7ebabd
Abilities API: Assert the slug type when `wp_has_ability_category()` …
westonruter Dec 3, 2026
a515dbc
Tests: Cover registering an ability category with a numeric slug.
westonruter Dec 3, 2026
d48654a
Abilities API: Point to `get_slug()` for string slugs in the `wp_get_…
westonruter Dec 3, 2026
e3236f7
Use see tag
westonruter Dec 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/wp-includes/abilities-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,8 @@ function wp_unregister_ability_category( string $slug ): ?WP_Ability_Category {
*
* @param string $slug The slug of the ability category to check.
* @return bool `true` if the ability category is registered, `false` otherwise.
*
* @phpstan-assert-if-true =lowercase-string&non-empty-string $slug
*/
function wp_has_ability_category( string $slug ): bool {
$registry = WP_Ability_Categories_Registry::get_instance();
Expand Down Expand Up @@ -883,9 +885,15 @@ function wp_get_ability_category( string $slug ): ?WP_Ability_Category {
* @see WP_Ability_Categories_Registry::get_all_registered()
* @see wp_get_ability_category()
*
* @return WP_Ability_Category[] An array of registered ability category instances,
* keyed by category slug. Returns an empty array if no ability categories
* are registered or if the registry is unavailable.
* @return array<string|int, WP_Ability_Category> An array of registered ability category instances,
* keyed by category slug. A numeric slug, such as `'123'`,
* is keyed by an integer, since PHP casts numeric string
* array keys to integers; use
* {@see WP_Ability_Category::get_slug()} when a string slug is
* needed. Returns an empty array if no ability categories
* are registered or if the registry is unavailable.
*
* @phpstan-return array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category>
*/
function wp_get_ability_categories(): array {
$registry = WP_Ability_Categories_Registry::get_instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ final class WP_Ability_Categories_Registry {
* Holds the registered ability categories.
*
* @since 6.9.0
* @var WP_Ability_Category[]
* @var array<string|int, WP_Ability_Category>
*
* @phpstan-var array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category>
*/
private $registered_categories = array();

Expand Down Expand Up @@ -121,7 +123,7 @@ public function register( string $slug, array $args ): ?WP_Ability_Category {
* @return WP_Ability_Category|null The unregistered ability category instance on success, null on failure.
*/
public function unregister( string $slug ): ?WP_Ability_Category {
if ( ! $this->is_registered( $slug ) ) {
if ( ! isset( $this->registered_categories[ $slug ] ) ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Ability category slug. */
Expand All @@ -146,7 +148,9 @@ public function unregister( string $slug ): ?WP_Ability_Category {
*
* @see wp_get_ability_categories()
*
* @return array<string, WP_Ability_Category> The array of registered ability categories.
* @return array<string|int, WP_Ability_Category> The array of registered ability categories.
*
* @phpstan-return array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category>
*/
public function get_all_registered(): array {
return $this->registered_categories;
Expand All @@ -163,6 +167,8 @@ public function get_all_registered(): array {
*
* @param string $slug The slug of the ability category.
* @return bool True if the ability category is registered, false otherwise.
*
* @phpstan-assert-if-true =lowercase-string&non-empty-string $slug
*/
public function is_registered( string $slug ): bool {
return isset( $this->registered_categories[ $slug ] );
Expand All @@ -181,7 +187,7 @@ public function is_registered( string $slug ): bool {
* @return WP_Ability_Category|null The registered ability category instance, or null if it is not registered.
*/
public function get_registered( string $slug ): ?WP_Ability_Category {
if ( ! $this->is_registered( $slug ) ) {
if ( ! isset( $this->registered_categories[ $slug ] ) ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Ability category slug. */
Expand Down
25 changes: 0 additions & 25 deletions tests/phpstan/baselines-strict/return.type.neon

This file was deleted.

29 changes: 29 additions & 0 deletions tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,35 @@ public function test_get_all_categories(): void {
$this->assertArrayHasKey( 'test-system', $categories );
}

/**
* Tests that a category with a numeric slug is stored under an integer key.
*
* PHP casts a numeric string array key to an integer, so the registry
* must still find the category by its string slug.
*
* @ticket 66739
*/
public function test_numeric_category_slug(): void {
$category = $this->registry->register(
'123',
array(
'label' => 'Numeric',
'description' => 'A category with a numeric slug.',
)
);

$this->assertInstanceOf( WP_Ability_Category::class, $category );
$this->assertSame( '123', $category->get_slug() );
$this->assertTrue( $this->registry->is_registered( '123' ) );
$this->assertSame( $category, $this->registry->get_registered( '123' ) );
$this->assertSame( array( 123 ), array_keys( $this->registry->get_all_registered() ), 'Expected the numeric slug to be an integer key.' );

$this->assertSame( $category, $this->registry->unregister( '123' ) );
$this->assertFalse( $this->registry->is_registered( '123' ) );
$this->assertSame( array(), $this->registry->get_all_registered() );
$this->assertSame( array(), $this->doing_it_wrong_log, 'Expected no incorrect usage notices.' );
}

/**
* Test category is_registered method.
*
Expand Down
Loading