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
This commit is contained in:
hellofromTonya 2021-09-09 18:36:57 +00:00
parent aaee916444
commit 41307cd4e6
18 changed files with 105 additions and 58 deletions

View File

@ -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();

View File

@ -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 );

View File

@ -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'] );

View File

@ -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();

View File

@ -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;

View File

@ -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 ),

View File

@ -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();

View File

@ -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;

View File

@ -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 );

View File

@ -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 );

View File

@ -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 ) ),
);

View File

@ -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 ];

View File

@ -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();

View File

@ -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 ),

View File

@ -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 );

View File

@ -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 );

View File

@ -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'],
);

View File

@ -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.