Make WordPress Core

Changeset 51786


Ignore:
Timestamp:
09/09/2021 06:35:34 PM (3 years ago)
Author:
hellofromTonya
Message:

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

In each child and grandchild class, renames the first 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.

Changes for readability:

  • @since clearly specifies the original parameter name and its new name as well as why the change happened.
  • In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.

Follow-up to [38832], [39011], [39015], [39021], [39024], [39025], [39031], [39036], [43519], [43735], [43739], [43768], [46821], [48173], [48242], [49088], [50995], [51003], [51021].

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

Location:
trunk/src/wp-includes/rest-api/endpoints
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r51000 r51786  
    708708     *
    709709     * @since 4.7.0
    710      *
    711      * @param WP_Post         $post    Attachment object.
     710     * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
     711     *
     712     * @param WP_Post         $item    Attachment object.
    712713     * @param WP_REST_Request $request Request object.
    713714     * @return WP_REST_Response Response object.
    714715     */
    715     public function prepare_item_for_response( $post, $request ) {
     716    public function prepare_item_for_response( $item, $request ) {
     717        // Restores the more descriptive, specific name for use within this method.
     718        $post     = $item;
    716719        $response = parent::prepare_item_for_response( $post, $request );
    717720        $fields   = $this->get_fields_for_response( $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

    r51298 r51786  
    397397     *
    398398     * @since 5.0.0
    399      *
    400      * @param WP_Post         $post    Post revision object.
     399     * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
     400     *
     401     * @param WP_Post         $item    Post revision object.
    401402     * @param WP_REST_Request $request Request object.
    402403     * @return WP_REST_Response Response object.
    403404     */
    404     public function prepare_item_for_response( $post, $request ) {
    405 
     405    public function prepare_item_for_response( $item, $request ) {
     406        // Restores the more descriptive, specific name for use within this method.
     407        $post     = $item;
    406408        $response = $this->revisions_controller->prepare_item_for_response( $post, $request );
    407409
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php

    r51676 r51786  
    110110     *
    111111     * @since 5.5.0
    112      *
    113      * @param array           $plugin  The plugin metadata.
     112     * @since 5.9.0 Renamed `$plugin` to `$item` to match parent class for PHP 8 named parameter support.
     113     *
     114     * @param array           $item    The plugin metadata.
    114115     * @param WP_REST_Request $request Request object.
    115116     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
    116117     */
    117     public function prepare_item_for_response( $plugin, $request ) {
     118    public function prepare_item_for_response( $item, $request ) {
     119        // Restores the more descriptive, specific name for use within this method.
     120        $plugin = $item;
     121
    118122        // There might be multiple blocks in a plugin. Only the first block is mapped.
    119123        $block_data = reset( $plugin['blocks'] );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r51693 r51786  
    237237     *
    238238     * @since 5.5.0
    239      *
    240      * @param WP_Block_Type   $block_type Block type data.
    241      * @param WP_REST_Request $request    Full details about the request.
     239     * @since 5.9.0 Renamed `$block_type` to `$item` to match parent class for PHP 8 named parameter support.
     240     *
     241     * @param WP_Block_Type   $item    Block type data.
     242     * @param WP_REST_Request $request Full details about the request.
    242243     * @return WP_REST_Response Block type data.
    243244     */
    244     public function prepare_item_for_response( $block_type, $request ) {
    245 
    246         $fields = $this->get_fields_for_response( $request );
    247         $data   = array();
     245    public function prepare_item_for_response( $item, $request ) {
     246        // Restores the more descriptive, specific name for use within this method.
     247        $block_type = $item;
     248        $fields     = $this->get_fields_for_response( $request );
     249        $data       = array();
    248250
    249251        if ( rest_is_field_included( 'attributes', $fields ) ) {
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r51293 r51786  
    10291029     *
    10301030     * @since 4.7.0
    1031      *
    1032      * @param WP_Comment      $comment Comment object.
     1031     * @since 5.9.0 Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support.
     1032     *
     1033     * @param WP_Comment      $item    Comment object.
    10331034     * @param WP_REST_Request $request Request object.
    10341035     * @return WP_REST_Response Response object.
    10351036     */
    1036     public function prepare_item_for_response( $comment, $request ) {
    1037 
    1038         $fields = $this->get_fields_for_response( $request );
    1039         $data   = array();
     1037    public function prepare_item_for_response( $item, $request ) {
     1038        // Restores the more descriptive, specific name for use within this method.
     1039        $comment = $item;
     1040        $fields  = $this->get_fields_for_response( $request );
     1041        $data    = array();
    10401042
    10411043        if ( in_array( 'id', $fields, true ) ) {
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php

    r51289 r51786  
    200200     *
    201201     * @since 5.8.0
    202      *
    203      * @param object          $raw_pattern A pattern from api.wordpress.org, before any changes.
    204      * @param WP_REST_Request $request     Request object.
     202     * @since 5.9.0 Renamed `$raw_pattern` to `$item` to match parent class for PHP 8 named parameter support.
     203     *
     204     * @param object          $item    Raw pattern from api.wordpress.org, before any changes.
     205     * @param WP_REST_Request $request Request object.
    205206     * @return WP_REST_Response
    206207     */
    207     public function prepare_item_for_response( $raw_pattern, $request ) {
     208    public function prepare_item_for_response( $item, $request ) {
     209        // Restores the more descriptive, specific name for use within this method.
     210        $raw_pattern      = $item;
    208211        $prepared_pattern = array(
    209212            'id'             => absint( $raw_pattern->id ),
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php

    r51000 r51786  
    214214     *
    215215     * @since 4.7.0
    216      *
    217      * @param stdClass        $status  Post status data.
     216     * @since 5.9.0 Renamed `$status` to `$item` to match parent class for PHP 8 named parameter support.
     217     *
     218     * @param stdClass        $item    Post status data.
    218219     * @param WP_REST_Request $request Full details about the request.
    219220     * @return WP_REST_Response Post status data.
    220221     */
    221     public function prepare_item_for_response( $status, $request ) {
    222 
     222    public function prepare_item_for_response( $item, $request ) {
     223        // Restores the more descriptive, specific name for use within this method.
     224        $status = $item;
    223225        $fields = $this->get_fields_for_response( $request );
    224226        $data   = array();
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

    r51000 r51786  
    169169     *
    170170     * @since 4.7.0
    171      *
    172      * @param WP_Post_Type    $post_type Post type object.
    173      * @param WP_REST_Request $request   Full details about the request.
     171     * @since 5.9.0 Renamed `$post_type` to `$item` to match parent class for PHP 8 named parameter support.
     172     *
     173     * @param WP_Post_Type    $item    Post type object.
     174     * @param WP_REST_Request $request Full details about the request.
    174175     * @return WP_REST_Response Response object.
    175176     */
    176     public function prepare_item_for_response( $post_type, $request ) {
     177    public function prepare_item_for_response( $item, $request ) {
     178        // Restores the more descriptive, specific name for use within this method.
     179        $post_type  = $item;
    177180        $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
    178181        $taxonomies = wp_list_pluck( $taxonomies, 'name' );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r51327 r51786  
    16801680     *
    16811681     * @since 4.7.0
    1682      *
    1683      * @param WP_Post         $post    Post object.
     1682     * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
     1683     *
     1684     * @param WP_Post         $item    Post object.
    16841685     * @param WP_REST_Request $request Request object.
    16851686     * @return WP_REST_Response Response object.
    16861687     */
    1687     public function prepare_item_for_response( $post, $request ) {
     1688    public function prepare_item_for_response( $item, $request ) {
     1689        // Restores the more descriptive, specific name for use within this method.
     1690        $post            = $item;
    16881691        $GLOBALS['post'] = $post;
    16891692
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r51000 r51786  
    536536     *
    537537     * @since 4.7.0
    538      *
    539      * @param WP_Post         $post    Post revision object.
     538     * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
     539     *
     540     * @param WP_Post         $item    Post revision object.
    540541     * @param WP_REST_Request $request Request object.
    541542     * @return WP_REST_Response Response object.
    542543     */
    543     public function prepare_item_for_response( $post, $request ) {
     544    public function prepare_item_for_response( $item, $request ) {
     545        // Restores the more descriptive, specific name for use within this method.
     546        $post            = $item;
    544547        $GLOBALS['post'] = $post;
    545548
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

    r51302 r51786  
    187187     * @since 5.0.0
    188188     * @since 5.6.0 The `$id` parameter can accept a string.
    189      *
    190      * @param int|string      $id      ID of the item to prepare.
     189     * @since 5.9.0 Renamed `$id` to `$item` to match parent class for PHP 8 named parameter support.
     190     *
     191     * @param int|string      $item    ID of the item to prepare.
    191192     * @param WP_REST_Request $request Request object.
    192193     * @return WP_REST_Response Response object.
    193194     */
    194     public function prepare_item_for_response( $id, $request ) {
     195    public function prepare_item_for_response( $item, $request ) {
     196        // Restores the more descriptive, specific name for use within this method.
     197        $item_id = $item;
    195198        $handler = $this->get_search_handler( $request );
    196199        if ( is_wp_error( $handler ) ) {
     
    200203        $fields = $this->get_fields_for_response( $request );
    201204
    202         $data = $handler->prepare_item( $id, $fields );
     205        $data = $handler->prepare_item( $item_id, $fields );
    203206        $data = $this->add_additional_fields_to_object( $data, $request );
    204207
     
    208211        $response = rest_ensure_response( $data );
    209212
    210         $links               = $handler->prepare_item_links( $id );
     213        $links               = $handler->prepare_item_links( $item_id );
    211214        $links['collection'] = array(
    212215            'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php

    r51705 r51786  
    263263     *
    264264     * @since 5.8.0
     265     * @since 5.9.0 Renamed `$raw_sidebar` to `$item` to match parent class for PHP 8 named parameter support.
    265266     *
    266267     * @global array $wp_registered_sidebars The registered sidebars.
    267268     * @global array $wp_registered_widgets  The registered widgets.
    268269     *
    269      * @param array           $raw_sidebar Sidebar instance.
    270      * @param WP_REST_Request $request     Full details about the request.
     270     * @param array           $item    Sidebar instance.
     271     * @param WP_REST_Request $request Full details about the request.
    271272     * @return WP_REST_Response Prepared response object.
    272273     */
    273     public function prepare_item_for_response( $raw_sidebar, $request ) {
     274    public function prepare_item_for_response( $item, $request ) {
    274275        global $wp_registered_sidebars, $wp_registered_widgets;
    275276
    276         $id      = $raw_sidebar['id'];
    277         $sidebar = array( 'id' => $id );
     277        // Restores the more descriptive, specific name for use within this method.
     278        $raw_sidebar = $item;
     279        $id          = $raw_sidebar['id'];
     280        $sidebar     = array( 'id' => $id );
    278281
    279282        if ( isset( $wp_registered_sidebars[ $id ] ) ) {
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r51000 r51786  
    201201     *
    202202     * @since 4.7.0
    203      *
    204      * @param WP_Taxonomy     $taxonomy Taxonomy data.
    205      * @param WP_REST_Request $request  Full details about the request.
     203     * @since 5.9.0 Renamed `$taxonomy` to `$item` to match parent class for PHP 8 named parameter support.
     204     *
     205     * @param WP_Taxonomy     $item    Taxonomy data.
     206     * @param WP_REST_Request $request Full details about the request.
    206207     * @return WP_REST_Response Response object.
    207208     */
    208     public function prepare_item_for_response( $taxonomy, $request ) {
    209         $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
     209    public function prepare_item_for_response( $item, $request ) {
     210        // Restores the more descriptive, specific name for use within this method.
     211        $taxonomy = $item;
     212        $base     = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    210213
    211214        $fields = $this->get_fields_for_response( $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r51349 r51786  
    403403     *
    404404     * @since 5.8.0
    405      *
    406      * @param WP_Block_Template $template Template instance.
     405     * @since 5.9.0 Renamed `$template` to `$item` to match parent class for PHP 8 named parameter support.
     406     *
     407     * @param WP_Block_Template $item    Template instance.
    407408     * @param WP_REST_Request   $request Request object.
    408409     * @return WP_REST_Response $data
    409410     */
    410     public function prepare_item_for_response( $template, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
    411         $result = array(
     411    public function prepare_item_for_response( $item, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
     412        // Restores the more descriptive, specific name for use within this method.
     413        $template = $item;
     414        $result   = array(
    412415            'id'             => $template->id,
    413416            'theme'          => $template->theme,
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

    r51289 r51786  
    205205     *
    206206     * @since 5.0.0
    207      *
    208      * @param WP_Theme        $theme   Theme object.
     207     * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support.
     208     *
     209     * @param WP_Theme        $item    Theme object.
    209210     * @param WP_REST_Request $request Request object.
    210211     * @return WP_REST_Response Response object.
    211212     */
    212     public function prepare_item_for_response( $theme, $request ) {
     213    public function prepare_item_for_response( $item, $request ) {
     214        // Restores the more descriptive, specific name for use within this method.
     215        $theme  = $item;
    213216        $data   = array();
    214217        $fields = $this->get_fields_for_response( $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r51278 r51786  
    964964     *
    965965     * @since 4.7.0
    966      *
    967      * @param WP_User         $user    User object.
     966     * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support.
     967     *
     968     * @param WP_User         $item    User object.
    968969     * @param WP_REST_Request $request Request object.
    969970     * @return WP_REST_Response Response object.
    970971     */
    971     public function prepare_item_for_response( $user, $request ) {
    972 
     972    public function prepare_item_for_response( $item, $request ) {
     973        // Restores the more descriptive, specific name for use within this method.
     974        $user   = $item;
    973975        $data   = array();
    974976        $fields = $this->get_fields_for_response( $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php

    r51657 r51786  
    264264     *
    265265     * @since 5.8.0
    266      *
    267      * @param array           $widget_type Widget type data.
    268      * @param WP_REST_Request $request    Full details about the request.
     266     * @since 5.9.0 Renamed `$widget_type` to `$item` to match parent class for PHP 8 named parameter support.
     267     *
     268     * @param array           $item    Widget type data.
     269     * @param WP_REST_Request $request Full details about the request.
    269270     * @return WP_REST_Response Widget type data.
    270271     */
    271     public function prepare_item_for_response( $widget_type, $request ) {
    272         $fields = $this->get_fields_for_response( $request );
    273         $data   = array(
     272    public function prepare_item_for_response( $item, $request ) {
     273        // Restores the more descriptive, specific name for use within this method.
     274        $widget_type = $item;
     275        $fields      = $this->get_fields_for_response( $request );
     276        $data        = array(
    274277            'id' => $widget_type['id'],
    275278        );
Note: See TracChangeset for help on using the changeset viewer.