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_Order 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_Order, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class WC_Order extends WC_Abstract_Order { |
||
17 | |||
18 | /** @public string Order type */ |
||
19 | public $order_type = 'simple'; |
||
20 | |||
21 | /** |
||
22 | * Gets order total - formatted for display. |
||
23 | * |
||
24 | * @return string |
||
25 | */ |
||
26 | public function get_formatted_order_total( $tax_display = '', $display_refunded = true ) { |
||
58 | |||
59 | /** |
||
60 | * Get order refunds. |
||
61 | * @since 2.2 |
||
62 | * @return array of WC_Order_Refund objects |
||
63 | */ |
||
64 | public function get_refunds() { |
||
74 | |||
75 | /** |
||
76 | * Get amount already refunded. |
||
77 | * |
||
78 | * @since 2.2 |
||
79 | * @return string |
||
80 | */ |
||
81 | View Code Duplication | public function get_total_refunded() { |
|
94 | |||
95 | /** |
||
96 | * Get the total tax refunded. |
||
97 | * |
||
98 | * @since 2.3 |
||
99 | * @return float |
||
100 | */ |
||
101 | View Code Duplication | public function get_total_tax_refunded() { |
|
115 | |||
116 | /** |
||
117 | * Get the total shipping refunded. |
||
118 | * |
||
119 | * @since 2.4 |
||
120 | * @return float |
||
121 | */ |
||
122 | View Code Duplication | public function get_total_shipping_refunded() { |
|
136 | |||
137 | /** |
||
138 | * Gets the count of order items of a certain type that have been refunded. |
||
139 | * @since 2.4.0 |
||
140 | * @param string $item_type |
||
141 | * @return string |
||
142 | */ |
||
143 | View Code Duplication | public function get_item_count_refunded( $item_type = '' ) { |
|
160 | |||
161 | /** |
||
162 | * Get the total number of items refunded. |
||
163 | * |
||
164 | * @since 2.4.0 |
||
165 | * @param string $item_type type of the item we're checking, if not a line_item |
||
166 | * @return integer |
||
167 | */ |
||
168 | public function get_total_qty_refunded( $item_type = 'line_item' ) { |
||
177 | |||
178 | /** |
||
179 | * Get the refunded amount for a line item. |
||
180 | * |
||
181 | * @param int $item_id ID of the item we're checking |
||
182 | * @param string $item_type type of the item we're checking, if not a line_item |
||
183 | * @return integer |
||
184 | */ |
||
185 | public function get_qty_refunded_for_item( $item_id, $item_type = 'line_item' ) { |
||
196 | |||
197 | /** |
||
198 | * Get the refunded amount for a line item. |
||
199 | * |
||
200 | * @param int $item_id ID of the item we're checking |
||
201 | * @param string $item_type type of the item we're checking, if not a line_item |
||
202 | * @return integer |
||
203 | */ |
||
204 | public function get_total_refunded_for_item( $item_id, $item_type = 'line_item' ) { |
||
222 | |||
223 | /** |
||
224 | * Get the refunded amount for a line item. |
||
225 | * |
||
226 | * @param int $item_id ID of the item we're checking |
||
227 | * @param int $tax_id ID of the tax we're checking |
||
228 | * @param string $item_type type of the item we're checking, if not a line_item |
||
229 | * @return double |
||
230 | */ |
||
231 | public function get_tax_refunded_for_item( $item_id, $tax_id, $item_type = 'line_item' ) { |
||
255 | |||
256 | /** |
||
257 | * Get total tax refunded by rate ID. |
||
258 | * |
||
259 | * @param int $rate_id |
||
260 | * |
||
261 | * @return float |
||
262 | */ |
||
263 | public function get_total_tax_refunded_by_rate_id( $rate_id ) { |
||
275 | |||
276 | /** |
||
277 | * How much money is left to refund? |
||
278 | * @return string |
||
279 | */ |
||
280 | public function get_remaining_refund_amount() { |
||
283 | |||
284 | /** |
||
285 | * How many items are left to refund? |
||
286 | * @return int |
||
287 | */ |
||
288 | public function get_remaining_refund_items() { |
||
291 | } |
||
292 |
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.