Make WordPress Core

Changeset 62733


Ignore:
Timestamp:
07/14/2026 12:12:23 PM (3 months ago)
Author:
johnbillion
Message:

Code Quality: Tighten the documented types in WP_Hook.

This adds a Hook_Callback PHPStan type to represent the shape of a callback used internally in WP_Hook and used in return values of array access methods.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/12441

Props westonruter

See #64898

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-hook.php

    r62408 r62733  
    1515 * @see Iterator
    1616 * @see ArrayAccess
     17 *
     18 * @phpstan-type Hook_Callback array{
     19 *   function: callable,
     20 *   accepted_args: int,
     21 * }
     22 *
     23 * @phpstan-implements Iterator<int, array<string, Hook_Callback>>
     24 * @phpstan-implements ArrayAccess<int, array<string, Hook_Callback>>
    1725 */
    1826#[AllowDynamicProperties]
    … …  
    2028
    2129        /**
    22          * Hook callbacks.
     30         * Hook callbacks keyed by priority.
    2331         *
    2432         * @since 4.7.0
    2533         * @var array
     34         * @phpstan-var array<int, array<string, Hook_Callback>>
    2635         */
    2736        public $callbacks = array();
    … …  
    3140         *
    3241         * @since 6.4.0
    33          * @var array
     42         * @var list<int>
    3443         */
    3544        protected $priorities = array();
    … …  
    3948         *
    4049         * @since 4.7.0
    41          * @var array
     50         * @var array<int, list<int>>
    4251         */
    4352        private $iterations = array();
    … …  
    4756         *
    4857         * @since 4.7.0
    49          * @var array
     58         * @var array<int, int>
    5059         */
    5160        private $current_priority = array();
    … …  
    440449         *
    441450         * @param array $filters Filters to normalize. See documentation above for details.
    442          * @return WP_Hook[] Array of normalized filters.
     451         * @phpstan-param array<string, WP_Hook|array<int, array<Hook_Callback>>> $filters
     452         * @return array<string, WP_Hook> Array of normalized filters keyed by hook name.
    443453         */
    444454        public static function build_preinitialized_hooks( $filters ) {
    445                 /** @var WP_Hook[] $normalized */
     455                /** @var array<string, WP_Hook> $normalized */
    446456                $normalized = array();
    447457
    … …  
    476486         * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
    477487         *
    478          * @param mixed $offset An offset to check for.
     488         * @param int $offset An offset to check for.
    479489         * @return bool True if the offset exists, false otherwise.
    480490         */
    … …  
    491501         * @link https://www.php.net/manual/en/arrayaccess.offsetget.php
    492502         *
    493          * @param mixed $offset The offset to retrieve.
    494          * @return mixed If set, the value at the specified offset, null otherwise.
     503         * @param int $offset The offset to retrieve.
     504         * @return array|null If set, the value at the specified offset, null otherwise.
     505         * @phpstan-return array<string, Hook_Callback>|null
    495506         */
    496507        #[ReturnTypeWillChange]
    … …  
    506517         * @link https://www.php.net/manual/en/arrayaccess.offsetset.php
    507518         *
    508          * @param mixed $offset The offset to assign the value to.
    509          * @param mixed $value The value to set.
     519         * @param int|null $offset The offset to assign the value to.
     520         * @param array    $value The value to set.
     521         * @phpstan-param array<string, Hook_Callback> $value
    510522         */
    511523        #[ReturnTypeWillChange]
    … …  
    527539         * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
    528540         *
    529          * @param mixed $offset The offset to unset.
     541         * @param int $offset The offset to unset.
    530542         */
    531543        #[ReturnTypeWillChange]
    … …  
    542554         * @link https://www.php.net/manual/en/iterator.current.php
    543555         *
    544          * @return array Of callbacks at current priority.
     556         * @return array|false Array of callbacks at current priority, false if there are no more elements.
     557         * @phpstan-return array<string, Hook_Callback>|false
    545558         */
    546559        #[ReturnTypeWillChange]
    … …  
    556569         * @link https://www.php.net/manual/en/iterator.next.php
    557570         *
    558          * @return array Of callbacks at next priority.
     571         * @return array|false Array of callbacks at next priority, false if there are no more elements.
     572         * @phpstan-return array<string, Hook_Callback>|false
    559573         */
    560574        #[ReturnTypeWillChange]
    … …  
    570584         * @link https://www.php.net/manual/en/iterator.key.php
    571585         *
    572          * @return mixed Returns current priority on success, or NULL on failure
     586         * @return int|null Returns current priority on success, or NULL on failure
    573587         */
    574588        #[ReturnTypeWillChange]
Note: See TracChangeset for help on using the changeset viewer.