diff --git a/src/wp-includes/abilities-api.php b/src/wp-includes/abilities-api.php index 50f4b6ac05610..804dfe01663b8 100644 --- a/src/wp-includes/abilities-api.php +++ b/src/wp-includes/abilities-api.php @@ -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(); @@ -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 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(); diff --git a/src/wp-includes/abilities-api/class-wp-ability-categories-registry.php b/src/wp-includes/abilities-api/class-wp-ability-categories-registry.php index 92e6dbdd84f65..16b51881559ca 100644 --- a/src/wp-includes/abilities-api/class-wp-ability-categories-registry.php +++ b/src/wp-includes/abilities-api/class-wp-ability-categories-registry.php @@ -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 + * + * @phpstan-var array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category> */ private $registered_categories = array(); @@ -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. */ @@ -146,7 +148,9 @@ public function unregister( string $slug ): ?WP_Ability_Category { * * @see wp_get_ability_categories() * - * @return array The array of registered ability categories. + * @return array 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; @@ -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 ] ); @@ -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. */ diff --git a/tests/phpstan/baselines-strict/return.type.neon b/tests/phpstan/baselines-strict/return.type.neon deleted file mode 100644 index 891a38598dedd..0000000000000 --- a/tests/phpstan/baselines-strict/return.type.neon +++ /dev/null @@ -1,25 +0,0 @@ -# PHPStan baseline for the `return.type` errors in WordPress core. -# -# https://phpstan.org/error-identifiers/return.type -# -# Each entry is scoped to a single file and carries an exact occurrence count, -# so that a new instance is reported as a new error rather than being absorbed -# silently. Fixing an occurrence therefore means decrementing or removing its -# entry here as part of the same change. -# -# The goal is to empty this file and delete it. -# -# Generated by `composer phpstan:baselines`. Do not edit by hand. Regenerate with: -# -# composer phpstan:baselines -- --identifier=return.type --config=phpstan-strict.neon.dist -# -# This reruns the analysis with the baselines in this directory suppressed, so -# that their errors surface again, and rewrites only this file. - -parameters: - ignoreErrors: - - - message: '#^Method WP_Ability_Categories_Registry\:\:get_all_registered\(\) should return array\ but returns array\\.$#' - identifier: return.type - count: 1 - path: ../../../src/wp-includes/abilities-api/class-wp-ability-categories-registry.php diff --git a/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php b/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php index b2f879e603f1c..1053541c6fd9f 100644 --- a/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php +++ b/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php @@ -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. *