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 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 = 'taxes'; |
||
38 | |||
39 | /** |
||
40 | * Register the routes for taxes. |
||
41 | */ |
||
42 | View Code Duplication | 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 . '/batch', array( |
||
89 | array( |
||
90 | 'methods' => WP_REST_Server::EDITABLE, |
||
91 | 'callback' => array( $this, 'batch_items' ), |
||
92 | 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
||
93 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
94 | ), |
||
95 | 'schema' => array( $this, 'get_public_batch_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 ) { |
|
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 ) { |
|
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 ) { |
|
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 ) { |
|
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 ) { |
|
168 | |||
169 | /** |
||
170 | * Check if a given request has access batch create, update and delete items. |
||
171 | * |
||
172 | * @param WP_REST_Request $request Full details about the request. |
||
173 | * @return boolean |
||
174 | */ |
||
175 | View Code Duplication | public function batch_items_permissions_check( $request ) { |
|
176 | if ( ! wc_rest_check_manager_permissions( 'settings', 'batch' ) ) { |
||
177 | return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to manipule this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
178 | } |
||
179 | |||
180 | return true; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get all taxes. |
||
185 | * |
||
186 | * @param WP_REST_Request $request Full details about the request. |
||
187 | * @return WP_Error|WP_REST_Response |
||
188 | */ |
||
189 | public function get_items( $request ) { |
||
276 | |||
277 | /** |
||
278 | * Take tax data from the request and return the updated or newly created rate. |
||
279 | * |
||
280 | * @todo Replace with CRUD in 2.7.0 |
||
281 | * @param WP_REST_Request $request Full details about the request. |
||
282 | * @param stdClass|null $current Existing tax object. |
||
283 | * @return stdClass |
||
284 | */ |
||
285 | protected function create_or_update_tax( $request, $current = null ) { |
||
347 | |||
348 | /** |
||
349 | * Create a single tax. |
||
350 | * |
||
351 | * @param WP_REST_Request $request Full details about the request. |
||
352 | * @return WP_Error|WP_REST_Response |
||
353 | */ |
||
354 | public function create_item( $request ) { |
||
380 | |||
381 | /** |
||
382 | * Get a single tax. |
||
383 | * |
||
384 | * @param WP_REST_Request $request Full details about the request. |
||
385 | * @return WP_Error|WP_REST_Response |
||
386 | */ |
||
387 | View Code Duplication | public function get_item( $request ) { |
|
400 | |||
401 | /** |
||
402 | * Update a single tax. |
||
403 | * |
||
404 | * @param WP_REST_Request $request Full details about the request. |
||
405 | * @return WP_Error|WP_REST_Response |
||
406 | */ |
||
407 | public function update_item( $request ) { |
||
434 | |||
435 | /** |
||
436 | * Delete a single tax. |
||
437 | * |
||
438 | * @param WP_REST_Request $request Full details about the request. |
||
439 | * @return WP_Error|WP_REST_Response |
||
440 | */ |
||
441 | public function delete_item( $request ) { |
||
478 | |||
479 | /** |
||
480 | * Prepare a single tax output for response. |
||
481 | * |
||
482 | * @param stdClass $tax Tax object. |
||
483 | * @param WP_REST_Request $request Request object. |
||
484 | * @return WP_REST_Response $response Response data. |
||
485 | */ |
||
486 | public function prepare_item_for_response( $tax, $request ) { |
||
536 | |||
537 | /** |
||
538 | * Prepare links for the request. |
||
539 | * |
||
540 | * @param stdClass $tax Tax object. |
||
541 | * @return array Links for the given tax. |
||
542 | */ |
||
543 | View Code Duplication | protected function prepare_links( $tax ) { |
|
544 | $links = array( |
||
545 | 'self' => array( |
||
546 | 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $tax->tax_rate_id ) ), |
||
547 | ), |
||
548 | 'collection' => array( |
||
549 | 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
||
550 | ), |
||
551 | ); |
||
552 | |||
553 | return $links; |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Get the Taxes schema, conforming to JSON Schema. |
||
558 | * |
||
559 | * @return array |
||
560 | */ |
||
561 | public function get_item_schema() { |
||
562 | $schema = array( |
||
563 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
||
564 | 'title' => 'tax', |
||
565 | 'type' => 'object', |
||
566 | 'properties' => array( |
||
567 | 'id' => array( |
||
568 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
||
569 | 'type' => 'integer', |
||
570 | 'context' => array( 'view', 'edit' ), |
||
571 | 'readonly' => true, |
||
572 | ), |
||
573 | 'country' => array( |
||
574 | 'description' => __( 'Country ISO 3166 code.', 'woocommerce' ), |
||
575 | 'type' => 'string', |
||
576 | 'context' => array( 'view', 'edit' ), |
||
577 | ), |
||
578 | 'state' => array( |
||
579 | 'description' => __( 'State code.', 'woocommerce' ), |
||
580 | 'type' => 'string', |
||
581 | 'context' => array( 'view', 'edit' ), |
||
582 | ), |
||
583 | 'postcode' => array( |
||
584 | 'description' => __( 'Postcode/ZIP.', 'woocommerce' ), |
||
585 | 'type' => 'string', |
||
586 | 'context' => array( 'view', 'edit' ), |
||
587 | ), |
||
588 | 'city' => array( |
||
589 | 'description' => __( 'City name.', 'woocommerce' ), |
||
590 | 'type' => 'string', |
||
591 | 'context' => array( 'view', 'edit' ), |
||
592 | ), |
||
593 | 'rate' => array( |
||
594 | 'description' => __( 'Tax rate.', 'woocommerce' ), |
||
595 | 'type' => 'float', |
||
596 | 'context' => array( 'view', 'edit' ), |
||
597 | ), |
||
598 | 'name' => array( |
||
599 | 'description' => __( 'Tax rate name.', 'woocommerce' ), |
||
600 | 'type' => 'string', |
||
601 | 'context' => array( 'view', 'edit' ), |
||
602 | ), |
||
603 | 'priority' => array( |
||
604 | 'description' => __( 'Tax priority.', 'woocommerce' ), |
||
605 | 'type' => 'integer', |
||
606 | 'default' => 1, |
||
607 | 'context' => array( 'view', 'edit' ), |
||
608 | ), |
||
609 | 'compound' => array( |
||
610 | 'description' => __( 'Whether or not this is a compound rate.', 'woocommerce' ), |
||
611 | 'type' => 'boolean', |
||
612 | 'default' => false, |
||
613 | 'context' => array( 'view', 'edit' ), |
||
614 | ), |
||
615 | 'shipping' => array( |
||
616 | 'description' => __( 'Whether or not this tax rate also gets applied to shipping.', 'woocommerce' ), |
||
617 | 'type' => 'boolean', |
||
618 | 'default' => true, |
||
619 | 'context' => array( 'view', 'edit' ), |
||
620 | ), |
||
621 | 'order' => array( |
||
622 | 'description' => __( 'Indicates the order that will appear in queries.', 'woocommerce' ), |
||
623 | 'type' => 'integer', |
||
624 | 'context' => array( 'view', 'edit' ), |
||
625 | ), |
||
626 | 'class' => array( |
||
627 | 'description' => __( 'Tax class.', 'woocommerce' ), |
||
628 | 'type' => 'string', |
||
629 | 'default' => 'standard', |
||
630 | 'enum' => array_merge( array( 'standard' ), array_map( 'sanitize_title', WC_Tax::get_tax_classes() ) ), |
||
631 | 'context' => array( 'view', 'edit' ), |
||
632 | ), |
||
633 | ), |
||
634 | ); |
||
635 | |||
636 | return $this->add_additional_fields_schema( $schema ); |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * Get the query params for collections. |
||
641 | * |
||
642 | * @return array |
||
643 | */ |
||
644 | public function get_collection_params() { |
||
696 | } |
||
697 |
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.