Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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'] ];
Note: See TracChangeset for help on using the changeset viewer.