Make WordPress Core

Changeset 63379


Ignore:
Timestamp:
08/27/2026 10:54:07 PM (6 weeks 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.

Location:
trunk
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/phpstan.neon.dist

    r63367 r63379  
    7777        - tests/phpstan/baselines/property.private.neon
    7878        - tests/phpstan/baselines/property.protected.neon
    79         - tests/phpstan/baselines/return.missing.neon
    8079        - tests/phpstan/baselines/return.type.neon
    8180        - tests/phpstan/baselines/return.unusedType.neon
  • trunk/src/wp-includes/class-wp-widget.php

    r62178 r63379  
    139139         *
    140140         * @param array $instance The settings for the particular instance of the widget.
    141          * @return string|null Default return is 'noform'. A subclass may opt to return null.
     141         * @return string|void Default return is 'noform'. A subclass which echoes its own
     142         *                     form returns nothing.
    142143         */
    143144        public function form( $instance ) {
  • 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
  • trunk/src/wp-includes/link-template.php

    r63378 r63379  
    11651165        $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
    11661166
    1167         if ( $display ) {
    1168                 echo $link;
    1169         } else {
     1167        if ( ! $display ) {
    11701168                return $link;
    11711169        }
     1170
     1171        echo $link;
     1172
     1173        return null;
    11721174}
    11731175
    … …  
    25482550        $output = $link ? esc_url( $link ) : '';
    25492551
    2550         if ( $display ) {
    2551                 echo $output;
    2552         } else {
     2552        if ( ! $display ) {
    25532553                return $output;
    25542554        }
     2555
     2556        echo $output;
     2557
     2558        return null;
    25552559}
    25562560
    … …  
    26582662        $output = $link ? esc_url( $link ) : '';
    26592663
    2660         if ( $display ) {
    2661                 echo $output;
    2662         } else {
     2664        if ( ! $display ) {
    26632665                return $output;
    26642666        }
     2667
     2668        echo $output;
     2669
     2670        return null;
    26652671}
    26662672
Note: See TracChangeset for help on using the changeset viewer.