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_Coupons_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_Coupons_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class WC_REST_Coupons_Controller extends WC_REST_Posts_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 = 'coupons'; |
||
38 | |||
39 | /** |
||
40 | * Post type. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $post_type = 'shop_coupon'; |
||
45 | |||
46 | /** |
||
47 | * Order refunds actions. |
||
48 | */ |
||
49 | public function __construct() { |
||
52 | |||
53 | /** |
||
54 | * Register the routes for coupons. |
||
55 | */ |
||
56 | public function register_routes() { |
||
57 | register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
||
58 | array( |
||
59 | 'methods' => WP_REST_Server::READABLE, |
||
60 | 'callback' => array( $this, 'get_items' ), |
||
61 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
62 | 'args' => $this->get_collection_params(), |
||
63 | ), |
||
64 | array( |
||
65 | 'methods' => WP_REST_Server::CREATABLE, |
||
66 | 'callback' => array( $this, 'create_item' ), |
||
67 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||
68 | 'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array( |
||
69 | 'code' => array( |
||
70 | 'required' => true, |
||
71 | ), |
||
72 | ) ), |
||
73 | ), |
||
74 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
75 | ) ); |
||
76 | |||
77 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( |
||
78 | array( |
||
79 | 'methods' => WP_REST_Server::READABLE, |
||
80 | 'callback' => array( $this, 'get_item' ), |
||
81 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
82 | 'args' => array( |
||
83 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
||
84 | ), |
||
85 | ), |
||
86 | array( |
||
87 | 'methods' => WP_REST_Server::EDITABLE, |
||
88 | 'callback' => array( $this, 'update_item' ), |
||
89 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||
90 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
91 | ), |
||
92 | array( |
||
93 | 'methods' => WP_REST_Server::DELETABLE, |
||
94 | 'callback' => array( $this, 'delete_item' ), |
||
95 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||
96 | 'args' => array( |
||
97 | 'force' => array( |
||
98 | 'default' => false, |
||
99 | 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ), |
||
100 | ), |
||
101 | ), |
||
102 | ), |
||
103 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
104 | ) ); |
||
105 | |||
106 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( |
||
107 | array( |
||
108 | 'methods' => WP_REST_Server::EDITABLE, |
||
109 | 'callback' => array( $this, 'batch_items' ), |
||
110 | 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
||
111 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
112 | ), |
||
113 | 'schema' => array( $this, 'get_public_batch_schema' ), |
||
114 | ) ); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Query args. |
||
119 | * |
||
120 | * @param array $args |
||
121 | * @param WP_REST_Request $request |
||
122 | * @return array |
||
123 | */ |
||
124 | public function query_args( $args, $request ) { |
||
135 | |||
136 | /** |
||
137 | * Prepare a single coupon output for response. |
||
138 | * |
||
139 | * @param WP_Post $post Post object. |
||
140 | * @param WP_REST_Request $request Request object. |
||
141 | * @return WP_REST_Response $data |
||
142 | */ |
||
143 | public function prepare_item_for_response( $post, $request ) { |
||
198 | |||
199 | /** |
||
200 | * Prepare a single coupon for create or update. |
||
201 | * |
||
202 | * @param WP_REST_Request $request Request object. |
||
203 | * @return WP_Error|stdClass $data Post object. |
||
204 | */ |
||
205 | protected function prepare_item_for_database( $request ) { |
||
278 | |||
279 | /** |
||
280 | * Expiry date format. |
||
281 | * |
||
282 | * @param string $expiry_date |
||
283 | * @return string |
||
284 | */ |
||
285 | protected function get_coupon_expiry_date( $expiry_date ) { |
||
292 | |||
293 | /** |
||
294 | * Add post meta fields. |
||
295 | * |
||
296 | * @param WP_Post $post |
||
297 | * @param WP_REST_Request $request |
||
298 | * @return bool|WP_Error |
||
299 | */ |
||
300 | protected function add_post_meta_fields( $post, $request ) { |
||
347 | |||
348 | /** |
||
349 | * Update post meta fields. |
||
350 | * |
||
351 | * @param WP_Post $post |
||
352 | * @param WP_REST_Request $request |
||
353 | * @return bool|WP_Error |
||
354 | */ |
||
355 | protected function update_post_meta_fields( $post, $request ) { |
||
356 | if ( isset( $request['amount'] ) ) { |
||
357 | update_post_meta( $post->ID, 'coupon_amount', wc_format_decimal( $request['amount'] ) ); |
||
358 | } |
||
359 | |||
360 | View Code Duplication | if ( isset( $request['individual_use'] ) ) { |
|
|
|||
361 | update_post_meta( $post->ID, 'individual_use', ( true === $request['individual_use'] ) ? 'yes' : 'no' ); |
||
362 | } |
||
363 | |||
364 | View Code Duplication | if ( isset( $request['product_ids'] ) ) { |
|
365 | update_post_meta( $post->ID, 'product_ids', implode( ',', array_filter( array_map( 'intval', $request['product_ids'] ) ) ) ); |
||
366 | } |
||
367 | |||
368 | View Code Duplication | if ( isset( $request['exclude_product_ids'] ) ) { |
|
369 | update_post_meta( $post->ID, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $request['exclude_product_ids'] ) ) ) ); |
||
370 | } |
||
371 | |||
372 | if ( isset( $request['usage_limit'] ) ) { |
||
373 | update_post_meta( $post->ID, 'usage_limit', absint( $request['usage_limit'] ) ); |
||
374 | } |
||
375 | |||
376 | if ( isset( $request['usage_limit_per_user'] ) ) { |
||
377 | update_post_meta( $post->ID, 'usage_limit_per_user', absint( $request['usage_limit_per_user'] ) ); |
||
378 | } |
||
379 | |||
380 | if ( isset( $request['limit_usage_to_x_items'] ) ) { |
||
381 | update_post_meta( $post->ID, 'limit_usage_to_x_items', absint( $request['limit_usage_to_x_items'] ) ); |
||
382 | } |
||
383 | |||
384 | if ( isset( $request['usage_count'] ) ) { |
||
385 | update_post_meta( $post->ID, 'usage_count', absint( $request['usage_count'] ) ); |
||
386 | } |
||
387 | |||
388 | if ( isset( $request['expiry_date'] ) ) { |
||
389 | update_post_meta( $post->ID, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $request['expiry_date'] ) ) ); |
||
390 | } |
||
391 | |||
392 | View Code Duplication | if ( isset( $request['free_shipping'] ) ) { |
|
393 | update_post_meta( $post->ID, 'free_shipping', ( true === $request['free_shipping'] ) ? 'yes' : 'no' ); |
||
394 | } |
||
395 | |||
396 | View Code Duplication | if ( isset( $request['product_categories'] ) ) { |
|
397 | update_post_meta( $post->ID, 'product_categories', array_filter( array_map( 'intval', $request['product_categories'] ) ) ); |
||
398 | } |
||
399 | |||
400 | View Code Duplication | if ( isset( $request['excluded_product_categories'] ) ) { |
|
401 | update_post_meta( $post->ID, 'exclude_product_categories', array_filter( array_map( 'intval', $request['excluded_product_categories'] ) ) ); |
||
402 | } |
||
403 | |||
404 | View Code Duplication | if ( isset( $request['exclude_sale_items'] ) ) { |
|
405 | update_post_meta( $post->ID, 'exclude_sale_items', ( true === $request['exclude_sale_items'] ) ? 'yes' : 'no' ); |
||
406 | } |
||
407 | |||
408 | if ( isset( $request['minimum_amount'] ) ) { |
||
409 | update_post_meta( $post->ID, 'minimum_amount', wc_format_decimal( $request['minimum_amount'] ) ); |
||
410 | } |
||
411 | |||
412 | if ( isset( $request['maximum_amount'] ) ) { |
||
413 | update_post_meta( $post->ID, 'maximum_amount', wc_format_decimal( $request['maximum_amount'] ) ); |
||
414 | } |
||
415 | |||
416 | View Code Duplication | if ( isset( $request['email_restrictions'] ) ) { |
|
417 | update_post_meta( $post->ID, 'customer_email', array_filter( array_map( 'sanitize_email', $request['email_restrictions'] ) ) ); |
||
418 | } |
||
419 | |||
420 | return true; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Get the Coupon's schema, conforming to JSON Schema. |
||
425 | * |
||
426 | * @return array |
||
427 | */ |
||
428 | public function get_item_schema() { |
||
565 | |||
566 | /** |
||
567 | * Get the query params for collections of attachments. |
||
568 | * |
||
569 | * @return array |
||
570 | */ |
||
571 | View Code Duplication | public function get_collection_params() { |
|
583 | } |
||
584 |
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.