Make WordPress Core


Ignore:
Timestamp:
08/27/2026 10:54:07 PM (3 months ago)
Author:
westonruter
Message:

Code Quality: Resolve the remaining return.missing PHPStan errors.

Nine template functions take a $display parameter and either echo their result or return it. Each is documented as returning string|null, but the echoing branch fell off the end without returning, which is what PHPStan reports as return.missing. Inverting the condition lets the value-returning path exit first and the echoing path fall through to an explicit return null;, the same shape single_month_title() received in r63378. Affected are wp_title(), single_post_title(), post_type_archive_title(), single_term_title(), the_date(), the_modified_date(), edit_term_link(), next_posts() and previous_posts(). No behavior changes.

WP_Widget::form() is documented as string|void again. r59336 added the void deliberately, noting that unlike its siblings it is the child classes which return nothing when the method is correctly implemented. r62178 replaced it with null on the premise that void cannot belong to a union, which holds for PHP's native return types but not for PHPDoc, where PHPStan reads void in a union as "may not return at all". Under string|null the annotation instead obliged every override to return, putting all 18 of them in breach, two of which live in bundled themes that r62178 did not touch.

With no occurrences left, tests/phpstan/baselines/return.missing.neon is deleted along with its entry in phpstan.neon.dist, completing the work begun in r63378.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13082.
Follow-up to r59336, r62178, r63020, r63378.

Props dpantazis, dmsnell.
See #65817, #64704.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r63378 r63379  
    16741674
    16751675        // Send it out.
    1676         if ( $display ) {
    1677                 echo $title;
    1678         } else {
     1676        if ( ! $display ) {
    16791677                return $title;
    16801678        }
     1679
     1680        echo $title;
     1681
     1682        return null;
    16811683}
    16821684
    … …  
    17131715         */
    17141716        $title = apply_filters( 'single_post_title', $_post->post_title, $_post );
    1715         if ( $display ) {
    1716                 echo $prefix . $title;
    1717         } else {
     1717
     1718        if ( ! $display ) {
    17181719                return $prefix . $title;
    17191720        }
     1721
     1722        echo $prefix . $title;
     1723
     1724        return null;
    17201725}
    17211726
    … …  
    17541759        $title = apply_filters( 'post_type_archive_title', $post_type_obj->labels->name, $post_type );
    17551760
    1756         if ( $display ) {
    1757                 echo $prefix . $title;
    1758         } else {
     1761        if ( ! $display ) {
    17591762                return $prefix . $title;
    17601763        }
     1764
     1765        echo $prefix . $title;
     1766
     1767        return null;
    17611768}
    17621769
    … …  
    18501857        }
    18511858
    1852         if ( $display ) {
    1853                 echo $prefix . $term_name;
    1854         } else {
     1859        if ( ! $display ) {
    18551860                return $prefix . $term_name;
    18561861        }
     1862
     1863        echo $prefix . $term_name;
     1864
     1865        return null;
    18571866}
    18581867
    … …  
    29142923        $the_date = apply_filters( 'the_date', $the_date, $format, $before, $after );
    29152924
    2916         if ( $display ) {
    2917                 echo $the_date;
    2918         } else {
     2925        if ( ! $display ) {
    29192926                return $the_date;
    29202927        }
     2928
     2929        echo $the_date;
     2930
     2931        return null;
    29212932}
    29222933
    … …  
    29822993        $the_modified_date = apply_filters( 'the_modified_date', $the_modified_date, $format, $before, $after );
    29832994
    2984         if ( $display ) {
    2985                 echo $the_modified_date;
    2986         } else {
     2995        if ( ! $display ) {
    29872996                return $the_modified_date;
    29882997        }
     2998
     2999        echo $the_modified_date;
     3000
     3001        return null;
    29893002}
    29903003
Note: See TracChangeset for help on using the changeset viewer.