WordPress.org

Make WordPress Core

Changeset 51788


Ignore:
Timestamp:
09/09/2021 07:41:53 PM (3 months ago)
Author:
hellofromTonya
Message:

Code Modernization: Fix parameter name mismatches for parent/child classes in WP_Sitemaps_Provider::get_max_num_pages().

In each child class, renames the parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.

Adds @since to clearly specify why the change happened.

Reassigns the generic parameter to the original parameter.
Why? Restoring the original name keeps the context intact within the method and makes the code more readable. An inline comment explains why this reassignment is made.

Note: Reassignment is done after the guard clause.
Why? To avoid unnecessary processing and memory should the method bail out.

Follow-up to [48072].

Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.

Location:
trunk/src/wp-includes/sitemaps/providers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php

    r51787 r51788  
    149149     *
    150150     * @since 5.5.0
    151      *
    152      * @param string $post_type Optional. Post type name. Default empty.
     151     * @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class
     152     *              for PHP 8 named parameter support.
     153     *
     154     * @param string $object_subtype Optional. Post type name. Default empty.
    153155     * @return int Total number of pages.
    154156     */
    155     public function get_max_num_pages( $post_type = '' ) {
    156         if ( empty( $post_type ) ) {
     157    public function get_max_num_pages( $object_subtype = '' ) {
     158        if ( empty( $object_subtype ) ) {
    157159            return 0;
    158160        }
     161
     162        // Restores the more descriptive, specific name for use within this method.
     163        $post_type = $object_subtype;
    159164
    160165        /**
  • trunk/src/wp-includes/sitemaps/providers/class-wp-sitemaps-taxonomies.php

    r51787 r51788  
    135135     *
    136136     * @since 5.5.0
    137      *
    138      * @param string $taxonomy Taxonomy name.
     137     * @since 5.9.0 Renamed `$taxonomy` to `$object_subtype` to match parent class
     138     *              for PHP 8 named parameter support.
     139     *
     140     * @param string $object_subtype Optional. Taxonomy name. Default empty.
    139141     * @return int Total number of pages.
    140142     */
    141     public function get_max_num_pages( $taxonomy = '' ) {
    142         if ( empty( $taxonomy ) ) {
     143    public function get_max_num_pages( $object_subtype = '' ) {
     144        if ( empty( $object_subtype ) ) {
    143145            return 0;
    144146        }
     147
     148        // Restores the more descriptive, specific name for use within this method.
     149        $taxonomy = $object_subtype;
    145150
    146151        /**
Note: See TracChangeset for help on using the changeset viewer.