Make WordPress Core

Changeset 65895


Ignore:
Timestamp:
12/03/2026 06:59:51 PM (8 hours ago)
Author:
westonruter
Message:

Abilities API: Resolve the strict return.type PHPStan error.

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 ​https://github.com/WordPress/wordpress-develop/pull/16448.
Follow-up to r61032, r65668.

Props hmbashar, westonruter, lancewillett.
See #66739.

Location:
trunk
Files:
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/abilities-api.php

    r65370 r65895  
    823823 * @param string $slug The slug of the ability category to check.
    824824 * @return bool `true` if the ability category is registered, `false` otherwise.
     825 *
     826 * @phpstan-assert-if-true =lowercase-string&non-empty-string $slug
    825827 */
    826828function wp_has_ability_category( string $slug ): bool {
    … …  
    884886 * @see wp_get_ability_category()
    885887 *
    886  * @return WP_Ability_Category[] An array of registered ability category instances,
    887  *                               keyed by category slug. Returns an empty array if no ability categories
    888  *                               are registered or if the registry is unavailable.
     888 * @return array<string|int, WP_Ability_Category> An array of registered ability category instances,
     889 *                                                keyed by category slug. A numeric slug, such as `'123'`,
     890 *                                                is keyed by an integer, since PHP casts numeric string
     891 *                                                array keys to integers; use
     892 *                                                {@see WP_Ability_Category::get_slug()} when a string slug is
     893 *                                                needed. Returns an empty array if no ability categories
     894 *                                                are registered or if the registry is unavailable.
     895 *
     896 * @phpstan-return array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category>
    889897 */
    890898function wp_get_ability_categories(): array {
  • trunk/src/wp-includes/abilities-api/class-wp-ability-categories-registry.php

    r64235 r65895  
    3131         *
    3232         * @since 6.9.0
    33          * @var WP_Ability_Category[]
     33         * @var array<string|int, WP_Ability_Category>
     34         *
     35         * @phpstan-var array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category>
    3436         */
    3537        private $registered_categories = array();
    … …  
    122124         */
    123125        public function unregister( string $slug ): ?WP_Ability_Category {
    124                 if ( ! $this->is_registered( $slug ) ) {
     126                if ( ! isset( $this->registered_categories[ $slug ] ) ) {
    125127                        _doing_it_wrong(
    126128                                __METHOD__,
    … …  
    147149         * @see wp_get_ability_categories()
    148150         *
    149          * @return array<string, WP_Ability_Category> The array of registered ability categories.
     151         * @return array<string|int, WP_Ability_Category> The array of registered ability categories.
     152         *
     153         * @phpstan-return array<(lowercase-string&non-empty-string)|non-negative-int, WP_Ability_Category>
    150154         */
    151155        public function get_all_registered(): array {
    … …  
    164168         * @param string $slug The slug of the ability category.
    165169         * @return bool True if the ability category is registered, false otherwise.
     170         *
     171         * @phpstan-assert-if-true =lowercase-string&non-empty-string $slug
    166172         */
    167173        public function is_registered( string $slug ): bool {
    … …  
    182188         */
    183189        public function get_registered( string $slug ): ?WP_Ability_Category {
    184                 if ( ! $this->is_registered( $slug ) ) {
     190                if ( ! isset( $this->registered_categories[ $slug ] ) ) {
    185191                        _doing_it_wrong(
    186192                                __METHOD__,
  • trunk/tests/phpunit/tests/abilities-api/wpAbilityCategoryRegistry.php

    r64088 r65895  
    350350                $this->assertArrayHasKey( 'test-math', $categories );
    351351                $this->assertArrayHasKey( 'test-system', $categories );
     352        }
     353
     354        /**
     355         * Tests that a category with a numeric slug is stored under an integer key.
     356         *
     357         * PHP casts a numeric string array key to an integer, so the registry
     358         * must still find the category by its string slug.
     359         *
     360         * @ticket 66739
     361         */
     362        public function test_numeric_category_slug(): void {
     363                $category = $this->registry->register(
     364                        '123',
     365                        array(
     366                                'label'       => 'Numeric',
     367                                'description' => 'A category with a numeric slug.',
     368                        )
     369                );
     370
     371                $this->assertInstanceOf( WP_Ability_Category::class, $category );
     372                $this->assertSame( '123', $category->get_slug() );
     373                $this->assertTrue( $this->registry->is_registered( '123' ) );
     374                $this->assertSame( $category, $this->registry->get_registered( '123' ) );
     375                $this->assertSame( array( 123 ), array_keys( $this->registry->get_all_registered() ), 'Expected the numeric slug to be an integer key.' );
     376
     377                $this->assertSame( $category, $this->registry->unregister( '123' ) );
     378                $this->assertFalse( $this->registry->is_registered( '123' ) );
     379                $this->assertSame( array(), $this->registry->get_all_registered() );
     380                $this->assertSame( array(), $this->doing_it_wrong_log, 'Expected no incorrect usage notices.' );
    352381        }
    353382
Note: See TracChangeset for help on using the changeset viewer.