Make WordPress Core

Changeset 63441


Ignore:
Timestamp:
09/02/2026 07:08:14 PM (3 months ago)
Author:
westonruter
Message:

Code Quality: Restore void on the dual-mode template tag.

Many template tag functions either print their result or return it, depending on a display or echo param. Their @return carried void in a union until r61766, r61768 and r62178 replaced it with null, on the premise that void cannot belong to a union type. That premise holds for PHP's native return types but not for PHPDoc, where PHPStan reads void in a union as "may not return at all".

Restoring it is not enough on its own, though. PHPStan raises the "Result of function … (void) is used." error only when the resolved return type is exactly void, which a union never is, so the tags that kept void were getting no more out of it than the ones converted to null. What carries the distinction is a conditional @phpstan-return, resolving to plain void when the tag prints and to the type it returns otherwise. Thirty-one functions gain one, including one in the bundled Twenty Twenty theme, each with a single void branch and a nullable retrieval branch. The trailing return null; statements added in r63378 and r63379 are removed; they existed only to satisfy return.missing, which does not apply once void is in the union. Where the flag lives in an $args array the condition has to match every falsy spelling of it, since these tags variously default it to true or to 1. A call whose flag PHPStan cannot see, such as a query string or an array built at runtime, does not resolve to plain void, so it is never reported.

Four descriptions are corrected alongside: next_posts(), previous_posts() and wp_register() promised a link where an empty string is possible, and wp_dropdown_languages() prints and then returns the markup rather than choosing between the two. Tags returning a meaningful value on a path shared by both modes cannot resolve to plain void and are left alone, among them single_month_title(), wp_list_categories() and wp_nav_menu().

Developed as subset of ​https://github.com/WordPress/wordpress-develop/pull/13359.
Follow-up to r32568, r61766, r61768, r62178, r63378, r63379, r63440.

Props marian1, westonruter, apermo.
See #65817, #64704.

Location:
trunk/src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentytwenty/inc/template-tags.php

    r61676 r63441  
    108108 *
    109109 * @param bool $display Display or return the HTML.
    110  * @return string|void The HTML to display.
     110 * @return string|void The HTML when `$display` is false, null when the site has no
     111 *                     description. Nothing otherwise.
     112 * @phpstan-return ( $display is true ? void : string|null )
    111113 */
    112114function twentytwenty_site_description( $display = true ) {
    … …  
    114116
    115117        if ( ! $description ) {
    116                 return;
     118                return null;
    117119        }
    118120
  • trunk/src/wp-includes/author-template.php

    r62091 r63441  
    450450 *     @type int[]|string $include       Array or comma/space-separated list of author IDs to include. Default empty.
    451451 * }
    452  * @return void|string Void if 'echo' argument is true, list of authors if 'echo' is false.
     452 * @return string|void List of authors if 'echo' is false, nothing otherwise.
     453 * @phpstan-return (
     454 *     $args is array{ echo: false|0|''|'0', ... }
     455 *         ? string
     456 *         : ( $args is ''|array ? void : string|null )
     457 * )
    453458 */
    454459function wp_list_authors( $args = '' ) {
  • trunk/src/wp-includes/bookmark-template.php

    r60058 r63441  
    207207 *                                          Default 'ASC'.
    208208 * }
    209  * @return void|string Void if 'echo' argument is true, HTML list of bookmarks if 'echo' is false.
     209 * @return string|void HTML list of bookmarks if 'echo' is false, nothing otherwise.
     210 * @phpstan-return (
     211 *     $args is array{ echo: false|0|''|'0', ... }
     212 *         ? string
     213 *         : ( $args is ''|array ? void : string|null )
     214 * )
    210215 */
    211216function wp_list_bookmarks( $args = '' ) {
  • trunk/src/wp-includes/category-template.php

    r63030 r63441  
    711711 *     @type bool   $echo      Whether or not to echo the return value. Default true.
    712712 * }
    713  * @return void|string|string[] Void if 'echo' argument is true, or on failure. Otherwise, tag cloud
    714  *                              as a string or an array, depending on 'format' argument.
     713 * @return string|string[]|void Tag cloud as a string, or as an array when the 'format' argument
     714 *                              is 'array'. Null on failure. Nothing when 'echo' is true and
     715 *                              'format' is not 'array'.
     716 * @phpstan-return (
     717 *     $args is array{ format: 'array', ... }
     718 *         ? string[]|null
     719 *         : ( $args is array{ echo: false|0|''|'0', ... }
     720 *             ? string|null
     721 *             : ( $args is ''|array ? void : string|string[]|null ) )
     722 * )
    715723 */
    716724function wp_tag_cloud( $args = '' ) {
    … …  
    746754
    747755        if ( empty( $tags ) || is_wp_error( $tags ) ) {
    748                 return;
     756                return null;
    749757        }
    750758
    … …  
    757765
    758766                if ( is_wp_error( $link ) ) {
    759                         return;
     767                        return null;
    760768                }
    761769
  • trunk/src/wp-includes/comment-template.php

    r62324 r63441  
    489489 * @param bool            $display   Optional. Whether to print or return the output.
    490490 *                                   Default true.
    491  * @return void|string Void if `$display` argument is true, comment classes if `$display` is false.
     491 * @return string|void Comment classes if `$display` is false, nothing otherwise.
     492 * @phpstan-return ( $display is true ? void : string )
    492493 */
    493494function comment_class( $css_class = '', $comment = null, $post = null, $display = true ) {
    … …  
    12401241 *
    12411242 * @since 0.71
    1242  *
    1243  * @param bool $deprecated_echo Not used.
    1244  * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
    1245  *                     for the result instead.
     1243 * @since 2.5.0 Deprecated the `$deprecated_echo` argument.
     1244 *
     1245 * @see get_trackback_url()
     1246 *
     1247 * @param bool $deprecated_echo Deprecated. Use {@see get_trackback_url()}. Echo the URL or
     1248 *                              return it. Default true.
     1249 * @return string|void The trackback URL when `$deprecated_echo` is false, nothing otherwise.
     1250 * @phpstan-return ( $deprecated_echo is true ? void : string )
    12461251 */
    12471252function trackback_url( $deprecated_echo = true ) {
    … …  
    22292234 * }
    22302235 * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Default null.
    2231  * @return void|string Void if 'echo' argument is true, or no comments to list.
    2232  *                     Otherwise, HTML list of comments.
     2236 * @return string|void HTML list of comments when 'echo' is false, null when there are no
     2237 *                     comments to list. Nothing otherwise.
     2238 * @phpstan-return (
     2239 *     $args is array{ echo: false|0|''|'0', ... }
     2240 *         ? string|null
     2241 *         : ( $args is ''|array ? void : string|null )
     2242 * )
    22332243 */
    22342244function wp_list_comments( $args = array(), $comments = null ) {
    … …  
    22752285                $comments = (array) $comments;
    22762286                if ( empty( $comments ) ) {
    2277                         return;
     2287                        return null;
    22782288                }
    22792289                if ( 'all' !== $parsed_args['type'] ) {
    22802290                        $comments_by_type = separate_comments( $comments );
    22812291                        if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) {
    2282                                 return;
     2292                                return null;
    22832293                        }
    22842294                        $_comments = $comments_by_type[ $parsed_args['type'] ];
    … …  
    23212331                                        $comments_by_type = separate_comments( $comments );
    23222332                                        if ( empty( $comments_by_type[ $parsed_args['type'] ] ) ) {
    2323                                                 return;
     2333                                                return null;
    23242334                                        }
    23252335
    … …  
    23332343                } else {
    23342344                        if ( empty( $wp_query->comments ) ) {
    2335                                 return;
     2345                                return null;
    23362346                        }
    23372347                        if ( 'all' !== $parsed_args['type'] ) {
    … …  
    23402350                                }
    23412351                                if ( empty( $wp_query->comments_by_type[ $parsed_args['type'] ] ) ) {
    2342                                         return;
     2352                                        return null;
    23432353                                }
    23442354                                $_comments = $wp_query->comments_by_type[ $parsed_args['type'] ];
  • trunk/src/wp-includes/functions.php

    r63420 r63441  
    87388738 * @param string $after   Markup to output after the annotation. Default `</p>`.
    87398739 * @param bool   $display Whether to echo or return the markup. Default `true` for echo.
    8740  * @return string|null Update PHP page annotation if available and $display is false, null otherwise.
     8740 * @return string|void Update PHP page annotation when `$display` is false, null when no
     8741 *                     annotation is available. Nothing otherwise.
     8742 * @phpstan-return ( $display is true ? void : string|null )
    87418743 */
    87428744function wp_update_php_annotation( $before = '<p class="description">', $after = '</p>', $display = true ) {
    87438745        $annotation = wp_get_update_php_annotation();
    87448746
    8745         if ( $annotation ) {
    8746                 if ( $display ) {
    8747                         echo $before . $annotation . $after;
    8748                 } else {
    8749                         return $before . $annotation . $after;
    8750                 }
    8751         }
    8752         return null;
     8747        if ( ! $annotation ) {
     8748                return null;
     8749        }
     8750
     8751        if ( ! $display ) {
     8752                return $before . $annotation . $after;
     8753        }
     8754
     8755        echo $before . $annotation . $after;
    87538756}
    87548757
  • trunk/src/wp-includes/general-template.php

    r63379 r63441  
    237237 *                              accessibility. Default empty.
    238238 * }
    239  * @return void|string Void if 'echo' argument is true, search form HTML if 'echo' is false.
     239 * @return string|void Search form HTML if 'echo' is false, nothing otherwise.
     240 * @phpstan-param array<string, mixed>|bool $args
     241 * @phpstan-return (
     242 *     $args is array{ echo: false|0|''|'0', ... }
     243 *         ? string
     244 *         : ( $args is false|0|''|'0' ? string : void )
     245 * )
    240246 */
    241247function get_search_form( $args = array() ) {
    … …  
    582588 * @param string $redirect Optional path to redirect to on login/logout.
    583589 * @param bool   $display  Default to echo and not return the link.
    584  * @return void|string Void if `$display` argument is true, log in/out link if `$display` is false.
     590 * @return string|void Log in/out link if `$display` is false, nothing otherwise.
     591 * @phpstan-return ( $display is true ? void : string )
    585592 */
    586593function wp_loginout( $redirect = '', $display = true ) {
    … …  
    722729 *
    723730 * }
    724  * @return void|string Void if 'echo' argument is true, login form HTML if 'echo' is false.
     731 * @return string|void Login form HTML if 'echo' is false, nothing otherwise.
     732 * @phpstan-return ( $args is array{ echo: false|0|''|'0', ... } ? string : void )
    725733 */
    726734function wp_login_form( $args = array() ) {
    … …  
    902910 * @param string $after   Text to output after the link. Default `</li>`.
    903911 * @param bool   $display Default to echo and not return the link.
    904  * @return void|string Void if `$display` argument is true, registration or admin link
    905  *                     if `$display` is false.
     912 * @return string|void The registration or admin link when `$display` is false, or an empty
     913 *                     string when registration is disabled or the logged-in user cannot
     914 *                     access the dashboard. Nothing otherwise.
     915 * @phpstan-return ( $display is true ? void : string )
    906916 */
    907917function wp_register( $before = '<li>', $after = '</li>', $display = true ) {
    … …  
    15441554 * @param bool   $display     Optional. Whether to display or retrieve title. Default true.
    15451555 * @param string $seplocation Optional. Location of the separator (either 'left' or 'right').
    1546  * @return string|null String when `$display` is false, null otherwise.
     1556 * @return string|void String when `$display` is false, nothing otherwise.
     1557 * @phpstan-return ( $display is true ? void : string )
    15471558 */
    15481559function wp_title( $sep = '&raquo;', $display = true, $seplocation = '' ) {
    … …  
    16791690
    16801691        echo $title;
    1681 
    1682         return null;
    16831692}
    16841693
    … …  
    16971706 * @param string $prefix  Optional. What to display before the title.
    16981707 * @param bool   $display Optional. Whether to display or retrieve title. Default true.
    1699  * @return string|null Title when retrieving.
     1708 * @return string|void Title when retrieving, null on failure.
     1709 *                     Nothing when displaying.
     1710 * @phpstan-return ( $display is true ? void : string|null )
    17001711 */
    17011712function single_post_title( $prefix = '', $display = true ) {
    … …  
    17211732
    17221733        echo $prefix . $title;
    1723 
    1724         return null;
    17251734}
    17261735
    … …  
    17351744 * @param string $prefix  Optional. What to display before the title.
    17361745 * @param bool   $display Optional. Whether to display or retrieve title. Default true.
    1737  * @return string|null Title when retrieving, null when displaying or on failure.
     1746 * @return string|void Title when retrieving, null on failure.
     1747 *                     Nothing when displaying.
     1748 * @phpstan-return ( $display is true ? void : string|null )
    17381749 */
    17391750function post_type_archive_title( $prefix = '', $display = true ) {
    … …  
    17641775
    17651776        echo $prefix . $title;
    1766 
    1767         return null;
    17681777}
    17691778
    … …  
    17791788 * @param string $prefix  Optional. What to display before the title.
    17801789 * @param bool   $display Optional. Whether to display or retrieve title. Default true.
    1781  * @return string|null Title when retrieving.
     1790 * @return string|void Title when retrieving, null on failure.
     1791 *                     Nothing when displaying.
     1792 * @phpstan-return ( $display is true ? void : string|null )
    17821793 */
    17831794function single_cat_title( $prefix = '', $display = true ) {
    1784         return single_term_title( $prefix, $display );
     1795        if ( ! $display ) {
     1796                return single_term_title( $prefix, false );
     1797        }
     1798
     1799        single_term_title( $prefix, true );
    17851800}
    17861801
    … …  
    17961811 * @param string $prefix  Optional. What to display before the title.
    17971812 * @param bool   $display Optional. Whether to display or retrieve title. Default true.
    1798  * @return string|null Title when retrieving.
     1813 * @return string|void Title when retrieving, null on failure.
     1814 *                     Nothing when displaying.
     1815 * @phpstan-return ( $display is true ? void : string|null )
    17991816 */
    18001817function single_tag_title( $prefix = '', $display = true ) {
    1801         return single_term_title( $prefix, $display );
     1818        if ( ! $display ) {
     1819                return single_term_title( $prefix, false );
     1820        }
     1821
     1822        single_term_title( $prefix, true );
    18021823}
    18031824
    … …  
    18131834 * @param string $prefix  Optional. What to display before the title.
    18141835 * @param bool   $display Optional. Whether to display or retrieve title. Default true.
    1815  * @return string|null Title when retrieving.
     1836 * @return string|void Title when retrieving, null on failure.
     1837 *                     Nothing when displaying.
     1838 * @phpstan-return ( $display is true ? void : string|null )
    18161839 */
    18171840function single_term_title( $prefix = '', $display = true ) {
    … …  
    18621885
    18631886        echo $prefix . $term_name;
    1864 
    1865         return null;
    18661887}
    18671888
    … …  
    22152236 *     @type string     $w               Week. Default current week.
    22162237 * }
    2217  * @return void|string Void if 'echo' argument is true, archive links if 'echo' is false.
     2238 * @return string|void Archive links when 'echo' is false, null when the post type is
     2239 *                     not viewable. Nothing otherwise.
     2240 * @phpstan-return (
     2241 *     $args is array{ echo: false|0|''|'0', ... }
     2242 *         ? string|null
     2243 *         : ( $args is ''|array ? void : string|null )
     2244 * )
    22182245 */
    22192246function wp_get_archives( $args = '' ) {
    … …  
    22512278        $post_type_object = get_post_type_object( $parsed_args['post_type'] );
    22522279        if ( ! is_post_type_viewable( $post_type_object ) ) {
    2253                 return;
     2280                return null;
    22542281        }
    22552282
    … …  
    24852512 *     @type string $post_type Optional. Post type. Default 'post'.
    24862513 * }
    2487  * @return void|string Void if `$display` argument is true, calendar HTML if `$display` is false.
     2514 * @return string|void Calendar HTML when `$display` is false, null when the site has
     2515 *                     no posts. Nothing otherwise.
     2516 * @phpstan-return ( $args is array{ display: false|0|''|'0', ... } ? string|null : void )
    24882517 */
    24892518function get_calendar( $args = array() ) {
    … …  
    25962625                        $cache[ $key ] = '';
    25972626                        wp_cache_set( 'get_calendar', $cache, 'calendar' );
    2598                         return;
     2627                        return null;
    25992628                }
    26002629        }
    … …  
    28992928 * @param string $after   Optional. Output after the date. Default empty.
    29002929 * @param bool   $display Optional. Whether to echo the date or return it. Default true.
    2901  * @return string|null String if retrieving.
     2930 * @return string|void String if retrieving.
     2931 * @phpstan-return ( $display is true ? void : string )
    29022932 */
    29032933function the_date( $format = '', $before = '', $after = '', $display = true ) {
    … …  
    29282958
    29292959        echo $the_date;
    2930 
    2931         return null;
    29322960}
    29332961
    … …  
    29763004 * @param string $after   Optional. Output after the date. Default empty.
    29773005 * @param bool   $display Optional. Whether to echo the date or return it. Default true.
    2978  * @return string|null String if retrieving.
     3006 * @return string|void String if retrieving.
     3007 * @phpstan-return ( $display is true ? void : string )
    29793008 */
    29803009function the_modified_date( $format = '', $before = '', $after = '', $display = true ) {
    … …  
    29983027
    29993028        echo $the_modified_date;
    3000 
    3001         return null;
    30023029}
    30033030
  • trunk/src/wp-includes/l10n.php

    r63320 r63441  
    17361736 *                                                  instead of an empty value. Default false.
    17371737 * }
    1738  * @return string|void HTML dropdown list of languages.
     1738 * @return string|void HTML dropdown list of languages. Always returned, whether or not
     1739 *                     'echo' is true; nothing is returned when the required `id` or `name`
     1740 *                     argument is missing.
    17391741 */
    17401742function wp_dropdown_languages( $args = array() ) {
  • trunk/src/wp-includes/link-template.php

    r63420 r63441  
    11321132 * @param int|WP_Term|null $term    Optional. Term ID or object. If null, the queried object will be inspected. Default null.
    11331133 * @param bool             $display Optional. Whether or not to echo the return. Default true.
    1134  * @return string|null HTML content.
     1134 * @return string|void HTML content when retrieving, null on failure or without the
     1135 *                     capability to edit the term. Nothing when displaying.
     1136 * @phpstan-return ( $display is true ? void : string|null )
    11351137 */
    11361138function edit_term_link( $link = '', $before = '', $after = '', $term = null, $display = true ) {
    … …  
    11701172
    11711173        echo $link;
    1172 
    1173         return null;
    11741174}
    11751175
    … …  
    25442544 * @param int  $max_page Optional. Max pages. Default 0.
    25452545 * @param bool $display  Optional. Whether to echo the link. Default true.
    2546  * @return string|null The link URL for next posts page if `$display = false`.
     2546 * @return string|void The next posts page link when `$display` is false, or an empty
     2547 *                     string when there is no next page. Nothing otherwise.
     2548 * @phpstan-return ( $display is true ? void : string )
    25472549 */
    25482550function next_posts( $max_page = 0, $display = true ) {
    … …  
    25552557
    25562558        echo $output;
    2557 
    2558         return null;
    25592559}
    25602560
    … …  
    26562656 *
    26572657 * @param bool $display Optional. Whether to echo the link. Default true.
    2658  * @return string|null The previous posts page link if `$display = false`.
     2658 * @return string|void The previous posts page link when `$display` is false, or an empty
     2659 *                     string when there is no previous page. Nothing otherwise.
     2660 * @phpstan-return ( $display is true ? void : string )
    26592661 */
    26602662function previous_posts( $display = true ) {
    … …  
    26672669
    26682670        echo $output;
    2669 
    2670         return null;
    26712671}
    26722672
    … …  
    32693269 *
    32703270 * @param string|array $args Optional args. See paginate_links(). Default empty array.
    3271  * @return void|string|array Void if 'echo' argument is true and 'type' is not an array,
    3272  *                           or if the query is not for an existing single post of any post type.
    3273  *                           Otherwise, markup for comment page links or array of comment page links,
    3274  *                           depending on 'type' argument.
     3271 * @return string|string[]|void Markup for comment page links, or an array of them when the 'type'
     3272 *                              argument is 'array'. Null if the query is not for an existing single
     3273 *                              post of any post type. Nothing when 'echo' is true and 'type' is not
     3274 *                              'array'.
     3275 * @phpstan-return (
     3276 *     $args is array{ type: 'array', ... }
     3277 *         ? string[]|null
     3278 *         : ( $args is array{ echo: false|0|''|'0', ... }
     3279 *             ? string|null
     3280 *             : ( $args is ''|array ? void : string|string[]|null ) )
     3281 * )
    32753282 */
    32763283function paginate_comments_links( $args = array() ) {
    … …  
    32783285
    32793286        if ( ! is_singular() ) {
    3280                 return;
     3287                return null;
    32813288        }
    32823289
  • trunk/src/wp-includes/post-template.php

    r63404 r63441  
    3838 * @param string $after   Optional. Markup to append to the title. Default empty.
    3939 * @param bool   $display Optional. Whether to echo or return the title. Default true for echo.
    40  * @return void|string Void if `$display` argument is true or the title is empty,
    41  *                     current post title if `$display` is false.
     40 * @return string|void Current post title when `$display` is false, null when the title
     41 *                     is empty. Nothing otherwise.
     42 * @phpstan-return ( $display is true ? void : string|null )
    4243 */
    4344function the_title( $before = '', $after = '', $display = true ) {
    … …  
    4546
    4647        if ( strlen( $title ) === 0 ) {
    47                 return;
     48                return null;
    4849        }
    4950
    … …  
    7778 *     @type WP_Post $post   Current post object to retrieve the title for.
    7879 * }
    79  * @return void|string Void if 'echo' argument is true, the title attribute if 'echo' is false.
     80 * @return string|void The title attribute when 'echo' is false, null when the title is
     81 *                     empty. Nothing otherwise.
     82 * @phpstan-return (
     83 *     $args is array{ echo: false|0|''|'0', ... }
     84 *         ? string|null
     85 *         : ( $args is ''|array ? void : string|null )
     86 * )
    8087 */
    8188function the_title_attribute( $args = '' ) {
    … …  
    9198
    9299        if ( strlen( $title ) === 0 ) {
    93                 return;
     100                return null;
    94101        }
    95102
    … …  
    12991306 *                                           Walker_Page instance being used.
    13001307 * }
    1301  * @return void|string Void if 'echo' argument is true, HTML list of pages if 'echo' is false.
     1308 * @return string|void HTML list of pages if 'echo' is false, nothing otherwise.
     1309 * @phpstan-return (
     1310 *     $args is array{ echo: false|0|''|'0', ... }
     1311 *         ? string
     1312 *         : ( $args is ''|array ? void : string|null )
     1313 * )
    13021314 */
    13031315function wp_list_pages( $args = '' ) {
    … …  
    14221434 *                                         Walker_Page instance being used.
    14231435 * }
    1424  * @return void|string Void if 'echo' argument is true, HTML menu if 'echo' is false.
     1436 * @return string|void HTML menu if 'echo' is false, nothing otherwise.
     1437 * @phpstan-return (
     1438 *     $args is array{ echo: false|0|''|'0', ... }
     1439 *         ? string
     1440 *         : ( $args is ''|array ? void : string|null )
     1441 * )
    14251442 */
    14261443function wp_page_menu( $args = array() ) {
  • trunk/src/wp-includes/user.php

    r63378 r63441  
    908908 *     @type string $include       An array, comma-, or space-separated list of user IDs to include. Default empty.
    909909 * }
    910  * @return string|null The output if echo is false. Otherwise null.
     910 * @return string|void The output if 'echo' is false, nothing otherwise.
     911 * @phpstan-return (
     912 *     $args is array{ echo: false|0|''|'0', ... }
     913 *         ? string
     914 *         : ( $args is ''|array ? void : string|null )
     915 * )
    911916 */
    912917function wp_list_users( $args = array() ) {
    … …  
    10161021
    10171022        echo $return;
    1018 
    1019         return null;
    10201023}
    10211024
Note: See TracChangeset for help on using the changeset viewer.