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 |
||
14 | class WC_Order_Item_Coupon extends WC_Order_Item { |
||
15 | |||
16 | /** |
||
17 | * Data properties of this order item object. |
||
18 | * @since 2.7.0 |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $_data = array( |
||
22 | 'order_id' => 0, |
||
23 | 'order_item_id' => 0, |
||
24 | 'code' => '', |
||
25 | 'discount' => 0, |
||
26 | 'discount_tax' => 0, |
||
27 | ); |
||
28 | |||
29 | /** |
||
30 | * offsetGet for ArrayAccess/Backwards compatibility. |
||
31 | * @deprecated Add deprecation notices in future release. |
||
32 | * @param string $offset |
||
33 | * @return mixed |
||
34 | */ |
||
35 | View Code Duplication | public function offsetGet( $offset ) { |
|
43 | |||
44 | /** |
||
45 | * offsetSet for ArrayAccess/Backwards compatibility. |
||
46 | * @deprecated Add deprecation notices in future release. |
||
47 | * @param string $offset |
||
48 | * @param mixed $value |
||
49 | */ |
||
50 | View Code Duplication | public function offsetSet( $offset, $value ) { |
|
58 | |||
59 | /** |
||
60 | * offsetExists for ArrayAccess |
||
61 | * @param string $offset |
||
62 | * @return bool |
||
63 | */ |
||
64 | public function offsetExists( $offset ) { |
||
70 | |||
71 | /** |
||
72 | * Read/populate data properties specific to this order item. |
||
73 | */ |
||
74 | public function read( $id ) { |
||
81 | |||
82 | /** |
||
83 | * Save properties specific to this order item. |
||
84 | * @return int Item ID |
||
85 | */ |
||
86 | public function save() { |
||
95 | |||
96 | /** |
||
97 | * Internal meta keys we don't want exposed as part of meta_data. |
||
98 | * @return array() |
||
99 | */ |
||
100 | protected function get_internal_meta_keys() { |
||
103 | |||
104 | /* |
||
105 | |-------------------------------------------------------------------------- |
||
106 | | Setters |
||
107 | |-------------------------------------------------------------------------- |
||
108 | */ |
||
109 | |||
110 | /** |
||
111 | * Set order item name. |
||
112 | * @param string $value |
||
113 | */ |
||
114 | public function set_name( $value ) { |
||
117 | |||
118 | /** |
||
119 | * Set code. |
||
120 | * @param string $value |
||
121 | */ |
||
122 | public function set_code( $value ) { |
||
125 | |||
126 | /** |
||
127 | * Set discount amount. |
||
128 | * @param string $value |
||
129 | */ |
||
130 | public function set_discount( $value ) { |
||
133 | |||
134 | /** |
||
135 | * Set discounted tax amount. |
||
136 | * @param string $value |
||
137 | */ |
||
138 | public function set_discount_tax( $value ) { |
||
141 | |||
142 | /* |
||
143 | |-------------------------------------------------------------------------- |
||
144 | | Getters |
||
145 | |-------------------------------------------------------------------------- |
||
146 | */ |
||
147 | |||
148 | /** |
||
149 | * Get order item type. |
||
150 | * @return string |
||
151 | */ |
||
152 | public function get_type() { |
||
155 | |||
156 | /** |
||
157 | * Get order item name. |
||
158 | * @return string |
||
159 | */ |
||
160 | public function get_name() { |
||
163 | |||
164 | /** |
||
165 | * Get coupon code. |
||
166 | * @return string |
||
167 | */ |
||
168 | public function get_code() { |
||
171 | |||
172 | /** |
||
173 | * Get discount amount. |
||
174 | * @return string |
||
175 | */ |
||
176 | public function get_discount() { |
||
179 | |||
180 | /** |
||
181 | * Get discounted tax amount. |
||
182 | * @return string |
||
183 | */ |
||
184 | public function get_discount_tax() { |
||
187 | } |
||
188 |
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.