Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
23 | class WC_REST_Shipping_Zone_Locations_Controller extends WC_REST_Shipping_Zones_Controller_Base { |
||
24 | |||
25 | /** |
||
26 | * Register the routes for Shipping Zone Locations. |
||
27 | */ |
||
28 | public function register_routes() { |
||
44 | |||
45 | /** |
||
46 | * Get all Shipping Zone Locations. |
||
47 | * |
||
48 | * @param WP_REST_Request $request |
||
49 | * @return WP_REST_Response|WP_Error |
||
50 | */ |
||
51 | View Code Duplication | public function get_items( $request ) { |
|
69 | |||
70 | /** |
||
71 | * Update all Shipping Zone Locations. |
||
72 | * |
||
73 | * @param WP_REST_Request $request |
||
74 | * @return WP_REST_Response|WP_Error |
||
75 | */ |
||
76 | public function update_items( $request ) { |
||
98 | |||
99 | /** |
||
100 | * Prepare the Shipping Zone Location for the REST response. |
||
101 | * |
||
102 | * @param array $item Shipping Zone Location. |
||
103 | * @param WP_REST_Request $request Request object. |
||
104 | * @return WP_REST_Response $response |
||
105 | */ |
||
106 | View Code Duplication | public function prepare_item_for_response( $item, $request ) { |
|
107 | $context = empty( $request['context'] ) ? 'view' : $request['context']; |
||
108 | $data = $this->add_additional_fields_to_object( $item, $request ); |
||
109 | $data = $this->filter_response_by_context( $data, $context ); |
||
110 | |||
111 | // Wrap the data in a response object. |
||
112 | $response = rest_ensure_response( $data ); |
||
113 | |||
114 | $response->add_links( $this->prepare_links( $request['id'] ) ); |
||
115 | |||
116 | return $response; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Prepare links for the request. |
||
121 | * |
||
122 | * @param int $zone_id Given Shipping Zone ID. |
||
123 | * @return array Links for the given Shipping Zone Location. |
||
124 | */ |
||
125 | View Code Duplication | protected function prepare_links( $zone_id ) { |
|
126 | $base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id; |
||
127 | $links = array( |
||
128 | 'collection' => array( |
||
129 | 'href' => rest_url( $base . '/locations' ), |
||
130 | ), |
||
131 | 'describes' => array( |
||
132 | 'href' => rest_url( $base ), |
||
133 | ), |
||
134 | ); |
||
135 | |||
136 | return $links; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Get the Shipping Zone Locations schema, conforming to JSON Schema |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | public function get_item_schema() { |
||
179 | } |
||
180 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.