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:
Complex classes like WC_REST_Order_Notes_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_REST_Order_Notes_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class WC_REST_Order_Notes_Controller extends WC_REST_Controller { |
||
24 | |||
25 | /** |
||
26 | * Endpoint namespace. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $namespace = 'wc/v1'; |
||
31 | |||
32 | /** |
||
33 | * Route base. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $rest_base = 'orders/(?P<order_id>[\d]+)/notes'; |
||
38 | |||
39 | /** |
||
40 | * Post type. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $post_type = 'shop_order'; |
||
45 | |||
46 | /** |
||
47 | * Register the routes for order notes. |
||
48 | */ |
||
49 | View Code Duplication | public function register_routes() { |
|
93 | |||
94 | /** |
||
95 | * Check whether a given request has permission to read order notes. |
||
96 | * |
||
97 | * @param WP_REST_Request $request Full details about the request. |
||
98 | * @return WP_Error|boolean |
||
99 | */ |
||
100 | public function get_items_permissions_check( $request ) { |
||
101 | if ( ! wc_rest_check_post_permissions( $this->post_type, 'read' ) ) { |
||
102 | return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
103 | } |
||
104 | |||
105 | return true; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Check if a given request has access create order notes. |
||
110 | * |
||
111 | * @param WP_REST_Request $request Full details about the request. |
||
112 | * @return boolean |
||
113 | */ |
||
114 | public function create_item_permissions_check( $request ) { |
||
115 | if ( ! wc_rest_check_post_permissions( $this->post_type, 'create' ) ) { |
||
116 | return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
117 | } |
||
118 | |||
119 | return true; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Check if a given request has access to read a order note. |
||
124 | * |
||
125 | * @param WP_REST_Request $request Full details about the request. |
||
126 | * @return WP_Error|boolean |
||
127 | */ |
||
128 | View Code Duplication | public function get_item_permissions_check( $request ) { |
|
129 | $post = get_post( (int) $request['order_id'] ); |
||
130 | |||
131 | if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'read', $post->ID ) ) { |
||
132 | return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
133 | } |
||
134 | |||
135 | return true; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Check if a given request has access delete a order note. |
||
140 | * |
||
141 | * @param WP_REST_Request $request Full details about the request. |
||
142 | * @return boolean |
||
143 | */ |
||
144 | View Code Duplication | public function delete_item_permissions_check( $request ) { |
|
145 | $post = get_post( (int) $request['order_id'] ); |
||
146 | |||
147 | if ( $post && ! wc_rest_check_post_permissions( $this->post_type, 'delete', $post->ID ) ) { |
||
148 | return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
149 | } |
||
150 | |||
151 | return true; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Get order notes from an order. |
||
156 | * |
||
157 | * @param WP_REST_Request $request |
||
158 | * @return array |
||
159 | */ |
||
160 | public function get_items( $request ) { |
||
161 | $order = get_post( (int) $request['order_id'] ); |
||
162 | |||
163 | if ( empty( $order->post_type ) || $this->post_type !== $order->post_type ) { |
||
164 | return new WP_Error( 'woocommerce_rest_{$this->post_type}_invalid_id', __( 'Invalid order id.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
165 | } |
||
166 | |||
167 | $args = array( |
||
168 | 'post_id' => $order->ID, |
||
169 | 'approve' => 'approve', |
||
170 | 'type' => 'order_note', |
||
171 | ); |
||
172 | |||
173 | remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); |
||
174 | |||
175 | $notes = get_comments( $args ); |
||
176 | |||
177 | add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); |
||
178 | |||
179 | $data = array(); |
||
180 | foreach ( $notes as $note ) { |
||
181 | $order_note = $this->prepare_item_for_response( $note, $request ); |
||
182 | $order_note = $this->prepare_response_for_collection( $order_note ); |
||
183 | $data[] = $order_note; |
||
184 | } |
||
185 | |||
186 | return rest_ensure_response( $data ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Create a single order note. |
||
191 | * |
||
192 | * @param WP_REST_Request $request Full details about the request. |
||
193 | * @return WP_Error|WP_REST_Response |
||
194 | */ |
||
195 | public function create_item( $request ) { |
||
235 | |||
236 | /** |
||
237 | * Get a single order note. |
||
238 | * |
||
239 | * @param WP_REST_Request $request Full details about the request. |
||
240 | * @return WP_Error|WP_REST_Response |
||
241 | */ |
||
242 | View Code Duplication | public function get_item( $request ) { |
|
243 | $id = (int) $request['id']; |
||
244 | $order = get_post( (int) $request['order_id'] ); |
||
245 | |||
246 | if ( empty( $order->post_type ) || $this->post_type !== $order->post_type ) { |
||
247 | return new WP_Error( 'woocommerce_rest_order_invalid_id', __( 'Invalid order id.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
248 | } |
||
249 | |||
250 | $note = get_comment( $id ); |
||
251 | |||
252 | if ( empty( $id ) || empty( $note ) || intval( $note->comment_post_ID ) !== intval( $order->ID ) ) { |
||
253 | return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
254 | } |
||
255 | |||
256 | $order_note = $this->prepare_item_for_response( $note, $request ); |
||
257 | $response = rest_ensure_response( $order_note ); |
||
258 | |||
259 | return $response; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Delete a single order note. |
||
264 | * |
||
265 | * @param WP_REST_Request $request Full details about the request. |
||
266 | * @return WP_REST_Response|WP_Error |
||
267 | */ |
||
268 | public function delete_item( $request ) { |
||
309 | |||
310 | /** |
||
311 | * Prepare a single order note output for response. |
||
312 | * |
||
313 | * @param WP_Comment $note Order note object. |
||
314 | * @param WP_REST_Request $request Request object. |
||
315 | * @return WP_REST_Response $response Response data. |
||
316 | */ |
||
317 | public function prepare_item_for_response( $note, $request ) { |
||
343 | |||
344 | /** |
||
345 | * Prepare links for the request. |
||
346 | * |
||
347 | * @param WP_Comment $note Delivery order_note object. |
||
348 | * @return array Links for the given order note. |
||
349 | */ |
||
350 | View Code Duplication | protected function prepare_links( $note ) { |
|
367 | |||
368 | /** |
||
369 | * Get the Order Notes schema, conforming to JSON Schema. |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | public function get_item_schema() { |
||
407 | |||
408 | /** |
||
409 | * Get the query params for collections. |
||
410 | * |
||
411 | * @return array |
||
412 | */ |
||
413 | public function get_collection_params() { |
||
418 | } |
||
419 |
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.