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_Report_Top_Sellers_Controller extends WC_REST_Report_Sales_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 = 'reports/top_sellers'; |
||
38 | |||
39 | /** |
||
40 | * Get sales reports. |
||
41 | * |
||
42 | * @param WP_REST_Request $request |
||
43 | * @return array|WP_Error |
||
44 | */ |
||
45 | public function get_items( $request ) { |
||
46 | // Set date filtering. |
||
47 | $filter = array( |
||
48 | 'period' => $request['period'], |
||
49 | 'date_min' => $request['date_min'], |
||
50 | 'date_max' => $request['date_max'], |
||
51 | ); |
||
52 | $this->setup_report( $filter ); |
||
53 | |||
54 | $report_data = $this->report->get_order_report_data( array( |
||
55 | 'data' => array( |
||
56 | '_product_id' => array( |
||
57 | 'type' => 'order_item_meta', |
||
58 | 'order_item_type' => 'line_item', |
||
59 | 'function' => '', |
||
60 | 'name' => 'product_id', |
||
61 | ), |
||
62 | '_qty' => array( |
||
63 | 'type' => 'order_item_meta', |
||
64 | 'order_item_type' => 'line_item', |
||
65 | 'function' => 'SUM', |
||
66 | 'name' => 'order_item_qty', |
||
67 | ) |
||
68 | ), |
||
69 | 'order_by' => 'order_item_qty DESC', |
||
70 | 'group_by' => 'product_id', |
||
71 | 'limit' => isset( $filter['limit'] ) ? absint( $filter['limit'] ) : 12, |
||
72 | 'query_type' => 'get_results', |
||
73 | 'filter_range' => true, |
||
74 | ) ); |
||
75 | |||
76 | $top_sellers = array(); |
||
77 | |||
78 | View Code Duplication | foreach ( $report_data as $item ) { |
|
|
|||
79 | $product = wc_get_product( $item->product_id ); |
||
80 | |||
81 | if ( $product ) { |
||
82 | $top_sellers[] = array( |
||
83 | 'name' => $product->get_title(), |
||
84 | 'product_id' => (int) $item->product_id, |
||
85 | 'quantity' => wc_stock_amount( $item->order_item_qty ), |
||
86 | ); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | $data = array(); |
||
91 | foreach ( $top_sellers as $top_seller ) { |
||
92 | $item = $this->prepare_item_for_response( (object) $top_seller, $request ); |
||
93 | $data[] = $this->prepare_response_for_collection( $item ); |
||
94 | } |
||
95 | |||
96 | return rest_ensure_response( $data ); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Prepare a report sales object for serialization. |
||
101 | * |
||
102 | * @param stdClass $top_seller |
||
103 | * @param WP_REST_Request $request Request object. |
||
104 | * @return WP_REST_Response $response Response data. |
||
105 | */ |
||
106 | View Code Duplication | public function prepare_item_for_response( $top_seller, $request ) { |
|
139 | |||
140 | /** |
||
141 | * Get the Report's schema, conforming to JSON Schema. |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function get_item_schema() { |
||
174 | } |
||
175 |
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.