From 41307cd4e6236bbf120d1b14f98e9781c66b0f45 Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Thu, 9 Sep 2021 18:36:57 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@51786 git-svn-id: http://core.svn.wordpress.org/trunk@51393 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-attachments-controller.php | 7 +++++-- .../class-wp-rest-autosaves-controller.php | 8 +++++--- .../class-wp-rest-block-directory-controller.php | 8 ++++++-- .../class-wp-rest-block-types-controller.php | 14 ++++++++------ .../class-wp-rest-comments-controller.php | 12 +++++++----- .../class-wp-rest-pattern-directory-controller.php | 9 ++++++--- .../class-wp-rest-post-statuses-controller.php | 8 +++++--- .../class-wp-rest-post-types-controller.php | 9 ++++++--- .../endpoints/class-wp-rest-posts-controller.php | 7 +++++-- .../class-wp-rest-revisions-controller.php | 7 +++++-- .../endpoints/class-wp-rest-search-controller.php | 11 +++++++---- .../class-wp-rest-sidebars-controller.php | 13 ++++++++----- .../class-wp-rest-taxonomies-controller.php | 11 +++++++---- .../class-wp-rest-templates-controller.php | 9 ++++++--- .../endpoints/class-wp-rest-themes-controller.php | 7 +++++-- .../endpoints/class-wp-rest-users-controller.php | 8 +++++--- .../class-wp-rest-widget-types-controller.php | 13 ++++++++----- wp-includes/version.php | 2 +- 18 files changed, 105 insertions(+), 58 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 3519b0ad2b..ad6a89b234 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -707,12 +707,15 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { * Prepares a single attachment output for response. * * @since 4.7.0 + * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Post $post Attachment object. + * @param WP_Post $item Attachment object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $post, $request ) { + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $post = $item; $response = parent::prepare_item_for_response( $post, $request ); $fields = $this->get_fields_for_response( $request ); $data = $response->get_data(); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php index 5f90b46401..3cb3d7efe5 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php @@ -396,13 +396,15 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { * Prepares the revision for the REST response. * * @since 5.0.0 + * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Post $post Post revision object. + * @param WP_Post $item Post revision object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $post, $request ) { - + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $post = $item; $response = $this->revisions_controller->prepare_item_for_response( $post, $request ); $fields = $this->get_fields_for_response( $request ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php index 3f54f24436..d054a814d4 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php @@ -109,12 +109,16 @@ class WP_REST_Block_Directory_Controller extends WP_REST_Controller { * Parse block metadata for a block, and prepare it for an API repsonse. * * @since 5.5.0 + * @since 5.9.0 Renamed `$plugin` to `$item` to match parent class for PHP 8 named parameter support. * - * @param array $plugin The plugin metadata. + * @param array $item The plugin metadata. * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ - public function prepare_item_for_response( $plugin, $request ) { + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $plugin = $item; + // There might be multiple blocks in a plugin. Only the first block is mapped. $block_data = reset( $plugin['blocks'] ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 44d91449d6..c546a3082a 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -236,15 +236,17 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller { * Prepares a block type object for serialization. * * @since 5.5.0 + * @since 5.9.0 Renamed `$block_type` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Block_Type $block_type Block type data. - * @param WP_REST_Request $request Full details about the request. + * @param WP_Block_Type $item Block type data. + * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response Block type data. */ - public function prepare_item_for_response( $block_type, $request ) { - - $fields = $this->get_fields_for_response( $request ); - $data = array(); + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $block_type = $item; + $fields = $this->get_fields_for_response( $request ); + $data = array(); if ( rest_is_field_included( 'attributes', $fields ) ) { $data['attributes'] = $block_type->get_attributes(); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 1845e7bd94..6e2daa829a 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -1028,15 +1028,17 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { * Prepares a single comment output for response. * * @since 4.7.0 + * @since 5.9.0 Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Comment $comment Comment object. + * @param WP_Comment $item Comment object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $comment, $request ) { - - $fields = $this->get_fields_for_response( $request ); - $data = array(); + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $comment = $item; + $fields = $this->get_fields_for_response( $request ); + $data = array(); if ( in_array( 'id', $fields, true ) ) { $data['id'] = (int) $comment->comment_ID; diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php index e79a1615ea..0c26c20ae0 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php @@ -199,12 +199,15 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller { * Prepare a raw pattern before it's output in an API response. * * @since 5.8.0 + * @since 5.9.0 Renamed `$raw_pattern` to `$item` to match parent class for PHP 8 named parameter support. * - * @param object $raw_pattern A pattern from api.wordpress.org, before any changes. - * @param WP_REST_Request $request Request object. + * @param object $item Raw pattern from api.wordpress.org, before any changes. + * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ - public function prepare_item_for_response( $raw_pattern, $request ) { + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $raw_pattern = $item; $prepared_pattern = array( 'id' => absint( $raw_pattern->id ), 'title' => sanitize_text_field( $raw_pattern->title->rendered ), diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php index 588bfcaf41..8ef94ea21a 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php @@ -213,13 +213,15 @@ class WP_REST_Post_Statuses_Controller extends WP_REST_Controller { * Prepares a post status object for serialization. * * @since 4.7.0 + * @since 5.9.0 Renamed `$status` to `$item` to match parent class for PHP 8 named parameter support. * - * @param stdClass $status Post status data. + * @param stdClass $item Post status data. * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response Post status data. */ - public function prepare_item_for_response( $status, $request ) { - + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $status = $item; $fields = $this->get_fields_for_response( $request ); $data = array(); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php index 5d1d327f4f..1301246dea 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php @@ -168,12 +168,15 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller { * Prepares a post type object for serialization. * * @since 4.7.0 + * @since 5.9.0 Renamed `$post_type` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Post_Type $post_type Post type object. - * @param WP_REST_Request $request Full details about the request. + * @param WP_Post_Type $item Post type object. + * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $post_type, $request ) { + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $post_type = $item; $taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) ); $taxonomies = wp_list_pluck( $taxonomies, 'name' ); $base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name; diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index b534cec5fb..068cc70bc2 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -1679,12 +1679,15 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { * Prepares a single post output for response. * * @since 4.7.0 + * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Post $post Post object. + * @param WP_Post $item Post object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $post, $request ) { + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $post = $item; $GLOBALS['post'] = $post; setup_postdata( $post ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index 09f9981d2b..5449812e19 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -535,12 +535,15 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller { * Prepares the revision for the REST response. * * @since 4.7.0 + * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Post $post Post revision object. + * @param WP_Post $item Post revision object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $post, $request ) { + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $post = $item; $GLOBALS['post'] = $post; setup_postdata( $post ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php index a0f2e17520..b87973c9d6 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php @@ -186,12 +186,15 @@ class WP_REST_Search_Controller extends WP_REST_Controller { * * @since 5.0.0 * @since 5.6.0 The `$id` parameter can accept a string. + * @since 5.9.0 Renamed `$id` to `$item` to match parent class for PHP 8 named parameter support. * - * @param int|string $id ID of the item to prepare. + * @param int|string $item ID of the item to prepare. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $id, $request ) { + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $item_id = $item; $handler = $this->get_search_handler( $request ); if ( is_wp_error( $handler ) ) { return new WP_REST_Response(); @@ -199,7 +202,7 @@ class WP_REST_Search_Controller extends WP_REST_Controller { $fields = $this->get_fields_for_response( $request ); - $data = $handler->prepare_item( $id, $fields ); + $data = $handler->prepare_item( $item_id, $fields ); $data = $this->add_additional_fields_to_object( $data, $request ); $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; @@ -207,7 +210,7 @@ class WP_REST_Search_Controller extends WP_REST_Controller { $response = rest_ensure_response( $data ); - $links = $handler->prepare_item_links( $id ); + $links = $handler->prepare_item_links( $item_id ); $links['collection'] = array( 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php index f5e132b5aa..24f13814cf 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php @@ -262,19 +262,22 @@ class WP_REST_Sidebars_Controller extends WP_REST_Controller { * Prepares a single sidebar output for response. * * @since 5.8.0 + * @since 5.9.0 Renamed `$raw_sidebar` to `$item` to match parent class for PHP 8 named parameter support. * * @global array $wp_registered_sidebars The registered sidebars. * @global array $wp_registered_widgets The registered widgets. * - * @param array $raw_sidebar Sidebar instance. - * @param WP_REST_Request $request Full details about the request. + * @param array $item Sidebar instance. + * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response Prepared response object. */ - public function prepare_item_for_response( $raw_sidebar, $request ) { + public function prepare_item_for_response( $item, $request ) { global $wp_registered_sidebars, $wp_registered_widgets; - $id = $raw_sidebar['id']; - $sidebar = array( 'id' => $id ); + // Restores the more descriptive, specific name for use within this method. + $raw_sidebar = $item; + $id = $raw_sidebar['id']; + $sidebar = array( 'id' => $id ); if ( isset( $wp_registered_sidebars[ $id ] ) ) { $registered_sidebar = $wp_registered_sidebars[ $id ]; diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php index 0089afbfb1..9d43ca2874 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php @@ -200,13 +200,16 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller { * Prepares a taxonomy object for serialization. * * @since 4.7.0 + * @since 5.9.0 Renamed `$taxonomy` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Taxonomy $taxonomy Taxonomy data. - * @param WP_REST_Request $request Full details about the request. + * @param WP_Taxonomy $item Taxonomy data. + * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $taxonomy, $request ) { - $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $taxonomy = $item; + $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; $fields = $this->get_fields_for_response( $request ); $data = array(); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index 48b6d71be0..1b4df794e8 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -402,13 +402,16 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { * Prepare a single template output for response * * @since 5.8.0 + * @since 5.9.0 Renamed `$template` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Block_Template $template Template instance. + * @param WP_Block_Template $item Template instance. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $data */ - public function prepare_item_for_response( $template, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - $result = array( + public function prepare_item_for_response( $item, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + // Restores the more descriptive, specific name for use within this method. + $template = $item; + $result = array( 'id' => $template->id, 'theme' => $template->theme, 'content' => array( 'raw' => $template->content ), diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php index 021f6adfaf..10b5c29a33 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php @@ -204,12 +204,15 @@ class WP_REST_Themes_Controller extends WP_REST_Controller { * Prepares a single theme output for response. * * @since 5.0.0 + * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_Theme $theme Theme object. + * @param WP_Theme $item Theme object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $theme, $request ) { + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $theme = $item; $data = array(); $fields = $this->get_fields_for_response( $request ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 63ad45b8ec..b3cdfc2e31 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -963,13 +963,15 @@ class WP_REST_Users_Controller extends WP_REST_Controller { * Prepares a single user output for response. * * @since 4.7.0 + * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support. * - * @param WP_User $user User object. + * @param WP_User $item User object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $user, $request ) { - + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $user = $item; $data = array(); $fields = $this->get_fields_for_response( $request ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php index c137302f31..3940566467 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php @@ -263,14 +263,17 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller { * Prepares a widget type object for serialization. * * @since 5.8.0 + * @since 5.9.0 Renamed `$widget_type` to `$item` to match parent class for PHP 8 named parameter support. * - * @param array $widget_type Widget type data. - * @param WP_REST_Request $request Full details about the request. + * @param array $item Widget type data. + * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response Widget type data. */ - public function prepare_item_for_response( $widget_type, $request ) { - $fields = $this->get_fields_for_response( $request ); - $data = array( + public function prepare_item_for_response( $item, $request ) { + // Restores the more descriptive, specific name for use within this method. + $widget_type = $item; + $fields = $this->get_fields_for_response( $request ); + $data = array( 'id' => $widget_type['id'], ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 8de4d037d2..11d347449b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-51785'; +$wp_version = '5.9-alpha-51786'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.