Make WordPress Core

Changeset 65777


Ignore:
Timestamp:
11/25/2026 11:11:52 AM (less than one hour ago)
Author:
swissspidy
Message:

Code Quality: Add PHPStan array shapes.

Document the known keys of arrays that functions accept or return as array shapes, such as the HTTP API response, which is defined once on WP_Http and reused by the remote request functions, and the widget, robots, and admin helper arrays. This lets static analysis check how the values are used.

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

Props swissspidy, westonruter, marian1, johnbillion.
See #66739.

Location:
trunk
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/media.php

    r65158 r65777  
    12381238 * @param bool|string $check
    12391239 * @return array<string, string> An array of data for the image size input fields.
     1240 *
     1241 * @phpstan-return array{label: string, input: 'html', html: string}
    12401242 */
    12411243function image_size_input_fields( $post, $check = '' ) {
    … …  
    19381940 * @param array $args
    19391941 * @return array<string, string> An array containing the media item and its metadata.
     1942 *
     1943 * @phpstan-return array{item: string, meta: string}
    19401944 */
    19411945function get_compat_media_markup( $attachment_id, $args = null ) {
  • trunk/src/wp-admin/includes/ms.php

    r64873 r65777  
    11881188 *
    11891189 * @return array Help tab arguments.
     1190 *
     1191 * @phpstan-return array{id: non-falsy-string, title: string, content: non-falsy-string}
    11901192 */
    11911193function get_site_screen_help_tab_args() {
  • trunk/src/wp-admin/includes/nav-menu.php

    r65317 r65777  
    13491349 *
    13501350 * @return string[] Array of column titles keyed by their column name.
     1351 *
     1352 * @phpstan-return array{
     1353 *     _title: string,
     1354 *     cb: non-falsy-string,
     1355 *     link-target: string,
     1356 *     title-attribute: string,
     1357 *     css-classes: string,
     1358 *     xfn: string,
     1359 *     description: string,
     1360 * }
    13511361 */
    13521362function wp_nav_menu_manage_columns() {
  • trunk/src/wp-admin/includes/post.php

    r65410 r65777  
    12211221 * @param string $type The post_type you want the statuses for. Default 'post'.
    12221222 * @return string[] An array of all the statuses for the supplied post type.
     1223 *
     1224 * @phpstan-return list<string>
    12231225 */
    12241226function get_available_post_statuses( $type = 'post' ) {
  • trunk/src/wp-includes/class-wp-dependencies.php

    r64585 r65777  
    7474         * @since 2.8.0
    7575         *
    76          * @var (int|false)[]
     76         * @var array<string, int|false>
     77         *
     78         * @phpstan-var array<non-empty-string, int|false>
    7779         */
    7880        public $groups = array();
  • trunk/src/wp-includes/class-wp-http.php

    r64350 r65777  
    3030 *
    3131 * @since 2.7.0
     32 *
     33 * @phpstan-type HTTP_Response array{
     34 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     35 *     body: string,
     36 *     response: array{code: int|false, message: string|false},
     37 *     cookies: WP_Http_Cookie[],
     38 *     filename?: string|null,
     39 *     http_response?: WP_HTTP_Requests_Response|null,
     40 * }
    3241 */
    3342#[AllowDynamicProperties]
    … …  
    167176         *     @type WP_HTTP_Requests_Response|null                         $http_response Response object.
    168177         * }
     178         *
     179         * @phpstan-return HTTP_Response|WP_Error
    169180         */
    170181        public function request( $url, $args = array() ) {
    … …  
    648659         * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
    649660         *                        A WP_Error instance upon error. See WP_Http::response() for details.
     661         *
     662         * @phpstan-return HTTP_Response|WP_Error
    650663         */
    651664        public function post( $url, $args = array() ) {
    … …  
    666679         * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
    667680         *                        A WP_Error instance upon error. See WP_Http::response() for details.
     681         *
     682         * @phpstan-return HTTP_Response|WP_Error
    668683         */
    669684        public function get( $url, $args = array() ) {
    … …  
    684699         * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'.
    685700         *                        A WP_Error instance upon error. See WP_Http::response() for details.
     701         *
     702         * @phpstan-return HTTP_Response|WP_Error
    686703         */
    687704        public function head( $url, $args = array() ) {
  • trunk/src/wp-includes/class-wp-widget.php

    r64794 r65777  
    110110         *                        'before_widget', and 'after_widget'.
    111111         * @param array $instance The settings for the particular instance of the widget.
     112         *
     113         * @phpstan-param array{
     114         *     name?: string,
     115         *     id?: string,
     116         *     description?: string,
     117         *     class?: string,
     118         *     before_widget: string,
     119         *     after_widget: string,
     120         *     before_title: string,
     121         *     after_title: string,
     122         *     before_sidebar?: string,
     123         *     after_sidebar?: string,
     124         *     show_in_rest?: bool,
     125         *     widget_id?: string,
     126         *     widget_name?: string,
     127         * } $args
    112128         */
    113129        public function widget( $args, $instance ) {
  • trunk/src/wp-includes/feed.php

    r64247 r65777  
    600600 * @param string $data Input string.
    601601 * @return array array(type, value)
     602 *
     603 * @phpstan-return array{'html'|'text'|'xhtml', string}
    602604 */
    603605function prep_atom_text_construct( $data ) {
  • trunk/src/wp-includes/formatting.php

    r65776 r65777  
    612612 * @param string $input The text which has to be formatted.
    613613 * @return string[] Array of the formatted text.
     614 *
     615 * @phpstan-return non-empty-list<string>
    614616 */
    615617function wp_html_split( $input ) {
  • trunk/src/wp-includes/functions.php

    r65776 r65777  
    83958395 * @return string The generated unique ID for the array.
    83968396 *
     8397 * @phpstan-param non-empty-array $data
    83978398 * @phpstan-return ( $prefix is lowercase-string ? lowercase-string&non-falsy-string : non-falsy-string )
    83988399 */
  • trunk/src/wp-includes/http.php

    r64350 r65777  
    4949 * @return array|WP_Error The response or WP_Error on failure.
    5050 *                        See WP_Http::request() for information on return value.
     51 *
     52 * @phpstan-return array{
     53 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     54 *     body: string,
     55 *     response: array{code: int|false, message: string|false},
     56 *     cookies: WP_Http_Cookie[],
     57 *     filename?: string|null,
     58 *     http_response?: WP_HTTP_Requests_Response|null,
     59 * }|WP_Error
    5160 */
    5261function wp_safe_remote_request( $url, $args = array() ) {
    … …  
    7887 * @return array|WP_Error The response or WP_Error on failure.
    7988 *                        See WP_Http::request() for information on return value.
     89 *
     90 * @phpstan-return array{
     91 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     92 *     body: string,
     93 *     response: array{code: int|false, message: string|false},
     94 *     cookies: WP_Http_Cookie[],
     95 *     filename?: string|null,
     96 *     http_response?: WP_HTTP_Requests_Response|null,
     97 * }|WP_Error
    8098 */
    8199function wp_safe_remote_get( $url, $args = array() ) {
    … …  
    107125 * @return array|WP_Error The response or WP_Error on failure.
    108126 *                        See WP_Http::request() for information on return value.
     127 *
     128 * @phpstan-return array{
     129 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     130 *     body: string,
     131 *     response: array{code: int|false, message: string|false},
     132 *     cookies: WP_Http_Cookie[],
     133 *     filename?: string|null,
     134 *     http_response?: WP_HTTP_Requests_Response|null,
     135 * }|WP_Error
    109136 */
    110137function wp_safe_remote_post( $url, $args = array() ) {
    … …  
    136163 * @return array|WP_Error The response or WP_Error on failure.
    137164 *                        See WP_Http::request() for information on return value.
     165 *
     166 * @phpstan-return array{
     167 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     168 *     body: string,
     169 *     response: array{code: int|false, message: string|false},
     170 *     cookies: WP_Http_Cookie[],
     171 *     filename?: string|null,
     172 *     http_response?: WP_HTTP_Requests_Response|null,
     173 * }|WP_Error
    138174 */
    139175function wp_safe_remote_head( $url, $args = array() ) {
    … …  
    163199 * @return array|WP_Error The response array or a WP_Error on failure.
    164200 *                        See WP_Http::request() for information on return value.
     201 *
     202 * @phpstan-return array{
     203 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     204 *     body: string,
     205 *     response: array{code: int|false, message: string|false},
     206 *     cookies: WP_Http_Cookie[],
     207 *     filename?: string|null,
     208 *     http_response?: WP_HTTP_Requests_Response|null,
     209 * }|WP_Error
    165210 */
    166211function wp_remote_request( $url, $args = array() ) {
    … …  
    184229 * @return array|WP_Error The response or WP_Error on failure.
    185230 *                        See WP_Http::request() for information on return value.
     231 *
     232 * @phpstan-return array{
     233 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     234 *     body: string,
     235 *     response: array{code: int|false, message: string|false},
     236 *     cookies: WP_Http_Cookie[],
     237 *     filename?: string|null,
     238 *     http_response?: WP_HTTP_Requests_Response|null,
     239 * }|WP_Error
    186240 */
    187241function wp_remote_get( $url, $args = array() ) {
    … …  
    205259 * @return array|WP_Error The response or WP_Error on failure.
    206260 *                        See WP_Http::request() for information on return value.
     261 *
     262 * @phpstan-return array{
     263 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     264 *     body: string,
     265 *     response: array{code: int|false, message: string|false},
     266 *     cookies: WP_Http_Cookie[],
     267 *     filename?: string|null,
     268 *     http_response?: WP_HTTP_Requests_Response|null,
     269 * }|WP_Error
    207270 */
    208271function wp_remote_post( $url, $args = array() ) {
    … …  
    226289 * @return array|WP_Error The response or WP_Error on failure.
    227290 *                        See WP_Http::request() for information on return value.
     291 *
     292 * @phpstan-return array{
     293 *     headers: \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array<string, string>,
     294 *     body: string,
     295 *     response: array{code: int|false, message: string|false},
     296 *     cookies: WP_Http_Cookie[],
     297 *     filename?: string|null,
     298 *     http_response?: WP_HTTP_Requests_Response|null,
     299 * }|WP_Error
    228300 */
    229301function wp_remote_head( $url, $args = array() ) {
  • trunk/src/wp-includes/nav-menu.php

    r65554 r65777  
    8686 * @global array $_wp_registered_nav_menus
    8787 *
    88  * @param string[] $locations Associative array of menu location identifiers (like a slug) and descriptive text.
     88 * @param array<string, string> $locations Associative array of menu location identifiers (like a slug) and descriptive text.
    8989 */
    9090function register_nav_menus( $locations = array() ) {
  • trunk/src/wp-includes/robots-template.php

    r53087 r65777  
    6565 * @see wp_robots_no_robots()
    6666 *
    67  * @param array $robots Associative array of robots directives.
    68  * @return array Filtered robots directives.
     67 * @param array<string, bool|string> $robots Associative array of robots directives.
     68 * @return array<string, bool|string> Filtered robots directives.
    6969 */
    7070function wp_robots_noindex( array $robots ) {
    … …  
    8787 * @see wp_robots_no_robots()
    8888 *
    89  * @param array $robots Associative array of robots directives.
    90  * @return array Filtered robots directives.
     89 * @param array<string, bool|string> $robots Associative array of robots directives.
     90 * @return array<string, bool|string> Filtered robots directives.
    9191 */
    9292function wp_robots_noindex_embeds( array $robots ) {
    … …  
    113113 * @see wp_robots_no_robots()
    114114 *
    115  * @param array $robots Associative array of robots directives.
    116  * @return array Filtered robots directives.
     115 * @param array<string, bool|string> $robots Associative array of robots directives.
     116 * @return array<string, bool|string> Filtered robots directives.
    117117 */
    118118function wp_robots_noindex_search( array $robots ) {
    … …  
    135135 * @since 5.7.0
    136136 *
    137  * @param array $robots Associative array of robots directives.
    138  * @return array Filtered robots directives.
     137 * @param array<string, bool|string> $robots Associative array of robots directives.
     138 * @return array<string, bool|string> Filtered robots directives.
     139 *
     140 * @phpstan-return array{ noindex: true, ...<string, bool|string> }
    139141 */
    140142function wp_robots_no_robots( array $robots ) {
    … …  
    162164 * @since 5.7.0
    163165 *
    164  * @param array $robots Associative array of robots directives.
    165  * @return array Filtered robots directives.
     166 * @param array<string, bool|string> $robots Associative array of robots directives.
     167 * @return array<string, bool|string> Filtered robots directives.
     168 *
     169 * @phpstan-return array{ noindex: true, noarchive: true, ...<string, bool|string> }
    166170 */
    167171function wp_robots_sensitive_page( array $robots ) {
    … …  
    183187 * @since 5.7.0
    184188 *
    185  * @param array $robots Associative array of robots directives.
    186  * @return array Filtered robots directives.
     189 * @param array<string, bool|string> $robots Associative array of robots directives.
     190 * @return array<string, bool|string> Filtered robots directives.
    187191 */
    188192function wp_robots_max_image_preview_large( array $robots ) {
  • trunk/src/wp-includes/shortcodes.php

    r65573 r65777  
    189189 * @param string $content The content to check.
    190190 * @return string[] An array of registered shortcode names found in the content.
     191 *
     192 * @phpstan-return list<string>
    191193 */
    192194function get_shortcode_tags_in_content( $content ) {
  • trunk/src/wp-includes/widgets.php

    r64681 r65777  
    17181718 * @param array|string $args   Values for input fields.
    17191719 * @param array        $inputs Override default display options.
     1720 *
     1721 * @phpstan-param array{
     1722 *     number: int|string,
     1723 *     error?: string|false,
     1724 *     title?: string,
     1725 *     url?: string,
     1726 *     items?: int,
     1727 *     show_summary?: int,
     1728 *     show_author?: int,
     1729 *     show_date?: int,
     1730 * } $args
     1731 * @phpstan-param array{
     1732 *     title?: bool|'hidden',
     1733 *     url?: bool|'hidden',
     1734 *     items?: bool|'hidden',
     1735 *     show_summary?: bool|'hidden',
     1736 *     show_author?: bool|'hidden',
     1737 *     show_date?: bool|'hidden',
     1738 * } $inputs
    17201739 */
    17211740function wp_widget_rss_form( $args, $inputs = null ) {
    … …  
    18031822 * @param bool  $check_feed Optional. Whether to check feed for errors. Default true.
    18041823 * @return array
     1824 *
     1825 * @phpstan-return array{
     1826 *     title: string,
     1827 *     url: string,
     1828 *     link: string,
     1829 *     items: int<1, 20>,
     1830 *     error: string|false,
     1831 *     show_summary: int,
     1832 *     show_author: int,
     1833 *     show_date: int,
     1834 * }
    18051835 */
    18061836function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
  • trunk/tests/phpstan/baselines-strict/argument.type.neon

    r65668 r65777  
    3535                        path: ../../../src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php
    3636                -
    37                         message: '#^Parameter \#1 \$wp_response of method WP_AI_Client_HTTP_Client\:\:create_psr_response\(\) expects array\<string, mixed\>, array given\.$#'
    38                         identifier: argument.type
    39                         count: 2
    40                         path: ../../../src/wp-includes/ai-client/adapters/class-wp-ai-client-http-client.php
    41                 -
    4237                        message: '#^Parameter \#2 \$value of method WordPress\\AiClientDependencies\\Psr\\Http\\Message\\MessageInterface\:\:withHeader\(\) expects array\<string\>\|string, mixed given\.$#'
    4338                        identifier: argument.type
Note: See TracChangeset for help on using the changeset viewer.