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_Taxes_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_Taxes_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class WC_REST_Taxes_Controller extends WP_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 = 'taxes'; |
||
38 | |||
39 | /** |
||
40 | * Register the routes for taxes. |
||
41 | */ |
||
42 | public function register_routes() { |
||
43 | register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
||
44 | array( |
||
45 | 'methods' => WP_REST_Server::READABLE, |
||
46 | 'callback' => array( $this, 'get_items' ), |
||
47 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
48 | 'args' => $this->get_collection_params(), |
||
49 | ), |
||
50 | array( |
||
51 | 'methods' => WP_REST_Server::CREATABLE, |
||
52 | 'callback' => array( $this, 'create_item' ), |
||
53 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||
54 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
||
55 | ), |
||
56 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
57 | ) ); |
||
58 | |||
59 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( |
||
60 | array( |
||
61 | 'methods' => WP_REST_Server::READABLE, |
||
62 | 'callback' => array( $this, 'get_item' ), |
||
63 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
64 | 'args' => array( |
||
65 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
||
66 | ), |
||
67 | ), |
||
68 | array( |
||
69 | 'methods' => WP_REST_Server::EDITABLE, |
||
70 | 'callback' => array( $this, 'update_item' ), |
||
71 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||
72 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
73 | ), |
||
74 | array( |
||
75 | 'methods' => WP_REST_Server::DELETABLE, |
||
76 | 'callback' => array( $this, 'delete_item' ), |
||
77 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||
78 | 'args' => array( |
||
79 | 'force' => array( |
||
80 | 'default' => false, |
||
81 | 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ), |
||
82 | ), |
||
83 | ), |
||
84 | ), |
||
85 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
86 | ) ); |
||
87 | |||
88 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/update_items', array( |
||
89 | array( |
||
90 | 'methods' => WP_REST_Server::EDITABLE, |
||
91 | 'callback' => array( $this, 'update_items' ), |
||
92 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||
93 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
94 | ), |
||
95 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
96 | ) ); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Check whether a given request has permission to read taxes. |
||
101 | * |
||
102 | * @param WP_REST_Request $request Full details about the request. |
||
103 | * @return WP_Error|boolean |
||
104 | */ |
||
105 | View Code Duplication | public function get_items_permissions_check( $request ) { |
|
106 | if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { |
||
107 | return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list taxes.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
108 | } |
||
109 | |||
110 | return true; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Check if a given request has access create taxes. |
||
115 | * |
||
116 | * @param WP_REST_Request $request Full details about the request. |
||
117 | * @return boolean |
||
118 | */ |
||
119 | View Code Duplication | public function create_item_permissions_check( $request ) { |
|
120 | if ( ! wc_rest_check_manager_permissions( 'settings', 'create' ) ) { |
||
121 | return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
122 | } |
||
123 | |||
124 | return true; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Check if a given request has access to read a tax. |
||
129 | * |
||
130 | * @param WP_REST_Request $request Full details about the request. |
||
131 | * @return WP_Error|boolean |
||
132 | */ |
||
133 | View Code Duplication | public function get_item_permissions_check( $request ) { |
|
134 | if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { |
||
135 | return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
136 | } |
||
137 | |||
138 | return true; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Check if a given request has access update a tax. |
||
143 | * |
||
144 | * @param WP_REST_Request $request Full details about the request. |
||
145 | * @return boolean |
||
146 | */ |
||
147 | View Code Duplication | public function update_item_permissions_check( $request ) { |
|
148 | if ( ! wc_rest_check_manager_permissions( 'settings', 'edit' ) ) { |
||
149 | return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
150 | } |
||
151 | |||
152 | return true; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Check if a given request has access delete a tax. |
||
157 | * |
||
158 | * @param WP_REST_Request $request Full details about the request. |
||
159 | * @return boolean |
||
160 | */ |
||
161 | View Code Duplication | public function delete_item_permissions_check( $request ) { |
|
162 | if ( ! wc_rest_check_manager_permissions( 'settings', 'delete' ) ) { |
||
163 | return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you are not allowed to delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
164 | } |
||
165 | |||
166 | return true; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Get all taxes. |
||
171 | * |
||
172 | * @param WP_REST_Request $request Full details about the request. |
||
173 | * @return WP_Error|WP_REST_Response |
||
174 | */ |
||
175 | public function get_items( $request ) { |
||
176 | global $wpdb; |
||
177 | |||
178 | $prepared_args = array(); |
||
179 | $prepared_args['exclude'] = $request['exclude']; |
||
180 | $prepared_args['include'] = $request['include']; |
||
181 | $prepared_args['order'] = $request['order']; |
||
182 | $prepared_args['number'] = $request['per_page']; |
||
183 | View Code Duplication | if ( ! empty( $request['offset'] ) ) { |
|
184 | $prepared_args['offset'] = $request['offset']; |
||
185 | } else { |
||
186 | $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; |
||
187 | } |
||
188 | $orderby_possibles = array( |
||
189 | 'id' => 'tax_rate_id', |
||
190 | 'order' => 'tax_rate_order', |
||
191 | ); |
||
192 | $prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ]; |
||
193 | $prepared_args['class'] = $request['class']; |
||
194 | |||
195 | /** |
||
196 | * Filter arguments, before passing to $wpdb->get_results(), when querying taxes via the REST API. |
||
197 | * |
||
198 | * @param array $prepared_args Array of arguments for $wpdb->get_results(). |
||
199 | * @param WP_REST_Request $request The current request. |
||
200 | */ |
||
201 | $prepared_args = apply_filters( 'woocommerce_rest_tax_query', $prepared_args, $request ); |
||
202 | |||
203 | $query = " |
||
204 | SELECT * |
||
205 | FROM {$wpdb->prefix}woocommerce_tax_rates |
||
206 | WHERE 1 = 1 |
||
207 | "; |
||
208 | |||
209 | // Filter by tax class. |
||
210 | View Code Duplication | if ( ! empty( $prepared_args['class'] ) ) { |
|
211 | $class = 'standard' !== $prepared_args['class'] ? sanitize_title( $prepared_args['class'] ) : ''; |
||
212 | $query .= " AND tax_rate_class = '$class'"; |
||
213 | } |
||
214 | |||
215 | // Order tax rates. |
||
216 | $order_by = sprintf( ' ORDER BY %s', sanitize_key( $prepared_args['orderby'] ) ); |
||
217 | |||
218 | // Pagination. |
||
219 | $pagination = sprintf( ' LIMIT %d, %d', $prepared_args['offset'], $prepared_args['number'] ); |
||
220 | |||
221 | // Query taxes. |
||
222 | $results = $wpdb->get_results( $query . $order_by . $pagination ); |
||
223 | |||
224 | $taxes = array(); |
||
225 | foreach ( $results as $tax ) { |
||
226 | $data = $this->prepare_item_for_response( $tax, $request ); |
||
227 | $taxes[] = $this->prepare_response_for_collection( $data ); |
||
228 | } |
||
229 | |||
230 | $response = rest_ensure_response( $taxes ); |
||
231 | |||
232 | // Store pagation values for headers then unset for count query. |
||
233 | $per_page = (int) $prepared_args['number']; |
||
234 | $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 ); |
||
235 | |||
236 | // Query only for ids. |
||
237 | $wpdb->get_results( str_replace( 'SELECT *', 'SELECT tax_rate_id', $query ) ); |
||
238 | |||
239 | // Calcule totals. |
||
240 | $total_taxes = (int) $wpdb->num_rows; |
||
241 | $response->header( 'X-WP-Total', (int) $total_taxes ); |
||
242 | $max_pages = ceil( $total_taxes / $per_page ); |
||
243 | $response->header( 'X-WP-TotalPages', (int) $max_pages ); |
||
244 | |||
245 | $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) ); |
||
246 | View Code Duplication | if ( $page > 1 ) { |
|
247 | $prev_page = $page - 1; |
||
248 | if ( $prev_page > $max_pages ) { |
||
249 | $prev_page = $max_pages; |
||
250 | } |
||
251 | $prev_link = add_query_arg( 'page', $prev_page, $base ); |
||
252 | $response->link_header( 'prev', $prev_link ); |
||
253 | } |
||
254 | View Code Duplication | if ( $max_pages > $page ) { |
|
255 | $next_page = $page + 1; |
||
256 | $next_link = add_query_arg( 'page', $next_page, $base ); |
||
257 | $response->link_header( 'next', $next_link ); |
||
258 | } |
||
259 | |||
260 | return $response; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Take tax data from the request and return the updated or newly created rate. |
||
265 | * @todo Replace with CRUD in 2.7.0 |
||
266 | * @param array $request |
||
267 | * @return object |
||
268 | */ |
||
269 | protected function create_or_update_tax( $request ) { |
||
270 | $id = absint( isset( $request['id'] ) ? $request['id'] : 0 ); |
||
271 | $current_tax = $id ? WC_Tax::_get_tax_rate( $id, OBJECT ) : false; |
||
272 | $data = array(); |
||
273 | $fields = array( |
||
274 | 'tax_rate_country', |
||
275 | 'tax_rate_state', |
||
276 | 'tax_rate', |
||
277 | 'tax_rate_name', |
||
278 | 'tax_rate_priority', |
||
279 | 'tax_rate_compound', |
||
280 | 'tax_rate_shipping', |
||
281 | 'tax_rate_order', |
||
282 | 'tax_rate_class', |
||
283 | ); |
||
284 | |||
285 | foreach ( $fields as $key ) { |
||
286 | // Remove data that was not posted. |
||
287 | if ( ! isset( $request[ $key ] ) ) { |
||
288 | continue; |
||
289 | } |
||
290 | |||
291 | // Test new data against current data. |
||
292 | if ( $current_tax && $current_tax->$key === $request[ $key ] ) { |
||
293 | continue; |
||
294 | } |
||
295 | |||
296 | // Add to data array |
||
297 | switch ( $key ) { |
||
298 | case 'tax_rate_priority' : |
||
299 | case 'tax_rate_compound' : |
||
300 | case 'tax_rate_shipping' : |
||
301 | case 'tax_rate_order' : |
||
302 | $data[ $key ] = absint( $request[ $key ] ); |
||
303 | break; |
||
304 | case 'tax_rate_class' : |
||
305 | $data[ $key ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : ''; |
||
306 | break; |
||
307 | default : |
||
308 | $data[ $key ] = wc_clean( $request[ $key ] ); |
||
309 | break; |
||
310 | } |
||
311 | } |
||
312 | |||
313 | if ( $id ) { |
||
314 | WC_Tax::_update_tax_rate( $id, $data ); |
||
315 | } else { |
||
316 | $id = WC_Tax::_insert_tax_rate( $data ); |
||
317 | } |
||
318 | |||
319 | // Add locales. |
||
320 | if ( ! empty( $request['postcode'] ) ) { |
||
321 | WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $request['postcode'] ) ); |
||
1 ignored issue
–
show
|
|||
322 | } |
||
323 | if ( ! empty( $request['city'] ) ) { |
||
324 | WC_Tax::_update_tax_rate_cities( $id, wc_clean( $request['city'] ) ); |
||
1 ignored issue
–
show
|
|||
325 | } |
||
326 | |||
327 | return WC_Tax::_get_tax_rate( $id, OBJECT ); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Create a single tax. |
||
332 | * |
||
333 | * @param WP_REST_Request $request Full details about the request. |
||
334 | * @return WP_Error|WP_REST_Response |
||
335 | */ |
||
336 | public function create_item( $request ) { |
||
337 | if ( ! empty( $request['id'] ) ) { |
||
338 | return new WP_Error( 'woocommerce_rest_tax_exists', __( 'Cannot create existing resource.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
339 | } |
||
340 | |||
341 | $tax = $this->create_or_update_tax( $request ); |
||
342 | |||
343 | $this->update_additional_fields_for_object( $tax, $request ); |
||
344 | |||
345 | /** |
||
346 | * Fires after a tax is created or updated via the REST API. |
||
347 | * |
||
348 | * @param stdClass $tax Data used to create the tax. |
||
349 | * @param WP_REST_Request $request Request object. |
||
350 | * @param boolean $creating True when creating tax, false when updating tax. |
||
351 | */ |
||
352 | do_action( 'woocommerce_rest_insert_tax', $tax, $request, true ); |
||
353 | |||
354 | $request->set_param( 'context', 'edit' ); |
||
355 | $response = $this->prepare_item_for_response( $tax, $request ); |
||
356 | $response = rest_ensure_response( $response ); |
||
357 | $response->set_status( 201 ); |
||
358 | $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) ); |
||
359 | |||
360 | return $response; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Get a single tax. |
||
365 | * |
||
366 | * @param WP_REST_Request $request Full details about the request. |
||
367 | * @return WP_Error|WP_REST_Response |
||
368 | */ |
||
369 | View Code Duplication | public function get_item( $request ) { |
|
370 | $id = (int) $request['id']; |
||
371 | $tax_obj = WC_Tax::_get_tax_rate( $id, OBJECT ); |
||
372 | |||
373 | if ( empty( $id ) || empty( $tax_obj ) ) { |
||
374 | return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
375 | } |
||
376 | |||
377 | $tax = $this->prepare_item_for_response( $tax_obj, $request ); |
||
378 | $response = rest_ensure_response( $tax ); |
||
379 | |||
380 | return $response; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Update a single tax. |
||
385 | * |
||
386 | * @param WP_REST_Request $request Full details about the request. |
||
387 | * @return WP_Error|WP_REST_Response |
||
388 | */ |
||
389 | public function update_item( $request ) { |
||
390 | $tax = $this->create_or_update_tax( $request ); |
||
391 | |||
392 | $this->update_additional_fields_for_object( $tax, $request ); |
||
393 | |||
394 | /** |
||
395 | * Fires after a tax is created or updated via the REST API. |
||
396 | * |
||
397 | * @param stdClass $tax Data used to create the tax. |
||
398 | * @param WP_REST_Request $request Request object. |
||
399 | * @param boolean $creating True when creating tax, false when updating tax. |
||
400 | */ |
||
401 | do_action( 'woocommerce_rest_insert_tax', $tax, $request, false ); |
||
402 | |||
403 | $request->set_param( 'context', 'edit' ); |
||
404 | $response = $this->prepare_item_for_response( $tax, $request ); |
||
405 | $response = rest_ensure_response( $response ); |
||
406 | |||
407 | return $response; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Delete a single tax. |
||
412 | * |
||
413 | * @param WP_REST_Request $request Full details about the request. |
||
414 | * @return WP_Error|WP_REST_Response |
||
415 | */ |
||
416 | public function delete_item( $request ) { |
||
453 | |||
454 | /** |
||
455 | * Prepare a single tax output for response. |
||
456 | * |
||
457 | * @param stdClass $tax Tax object. |
||
458 | * @param WP_REST_Request $request Request object. |
||
459 | * @return WP_REST_Response $response Response data. |
||
460 | */ |
||
461 | public function prepare_item_for_response( $tax, $request ) { |
||
511 | |||
512 | /** |
||
513 | * Prepare links for the request. |
||
514 | * |
||
515 | * @param stdClass $tax Tax object. |
||
516 | * @return array Links for the given tax. |
||
517 | */ |
||
518 | View Code Duplication | protected function prepare_links( $tax ) { |
|
530 | |||
531 | /** |
||
532 | * Bulk update or create items. |
||
533 | * |
||
534 | * @param WP_REST_Request $request Full details about the request. |
||
535 | * @return array Of WP_Error or WP_REST_Response. |
||
536 | */ |
||
537 | public function update_items( $request ) { |
||
538 | /** @var WP_REST_Server $wp_rest_server */ |
||
539 | global $wp_rest_server; |
||
540 | |||
541 | // Get the request params. |
||
578 | |||
579 | /** |
||
580 | * Get the Taxes schema, conforming to JSON Schema. |
||
581 | * |
||
582 | * @return array |
||
583 | */ |
||
584 | public function get_item_schema() { |
||
661 | |||
662 | /** |
||
663 | * Get the query params for collections. |
||
664 | * |
||
665 | * @return array |
||
666 | */ |
||
667 | public function get_collection_params() { |
||
719 | } |
||
720 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.