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 |
||
16 | class WC_Order_Refund extends WC_Abstract_Order { |
||
17 | |||
18 | /** @public string Order type */ |
||
19 | public $order_type = 'refund'; |
||
20 | |||
21 | /** @var string Date */ |
||
22 | public $date; |
||
23 | |||
24 | /** @var string Refund reason */ |
||
25 | public $reason; |
||
26 | |||
27 | /** |
||
28 | * Init/load the refund object. Called from the constructor. |
||
29 | * |
||
30 | * @param string|int|object|WC_Order_Refund $refund Refund to init |
||
31 | * @uses WP_POST |
||
32 | */ |
||
33 | View Code Duplication | protected function init( $refund ) { |
|
48 | |||
49 | /** |
||
50 | * Gets an refund from the database. |
||
51 | * |
||
52 | * @since 2.2 |
||
53 | * @param int $id |
||
54 | * @return bool |
||
55 | */ |
||
56 | View Code Duplication | public function get_refund( $id = 0 ) { |
|
69 | |||
70 | /** |
||
71 | * Populates an refund from the loaded post data. |
||
72 | * |
||
73 | * @param mixed $result |
||
74 | */ |
||
75 | public function populate( $result ) { |
||
82 | |||
83 | /** |
||
84 | * Get refunded amount. |
||
85 | * |
||
86 | * @since 2.2 |
||
87 | * @return int|float |
||
88 | */ |
||
89 | public function get_refund_amount() { |
||
92 | |||
93 | /** |
||
94 | * Get formatted refunded amount. |
||
95 | * |
||
96 | * @since 2.4 |
||
97 | * @return string |
||
98 | */ |
||
99 | public function get_formatted_refund_amount() { |
||
102 | |||
103 | |||
104 | /** |
||
105 | * Get refunded amount. |
||
106 | * |
||
107 | * @since 2.2 |
||
108 | * @return int|float |
||
109 | */ |
||
110 | public function get_refund_reason() { |
||
113 | } |
||
114 |
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.