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 |
||
15 | class WC_Order_Refund extends WC_Abstract_Order { |
||
16 | |||
17 | /** |
||
18 | * Extend the abstract _data properties and then read the order object. |
||
19 | * @param int|object|WC_Order $read Order to init. |
||
20 | */ |
||
21 | public function __construct( $read = 0 ) { |
||
30 | |||
31 | /** |
||
32 | * Data stored in meta keys, but not considered "meta" for an order. |
||
33 | * @since 2.7.0 |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $_internal_meta_keys = array( |
||
37 | '_order_currency', |
||
38 | '_cart_discount', |
||
39 | '_refund_amount', |
||
40 | '_refunded_by', |
||
41 | '_refund_reason', |
||
42 | '_cart_discount_tax', |
||
43 | '_order_shipping', |
||
44 | '_order_shipping_tax', |
||
45 | '_order_tax', |
||
46 | '_order_total', |
||
47 | '_order_version', |
||
48 | '_prices_include_tax', |
||
49 | '_payment_tokens', |
||
50 | ); |
||
51 | |||
52 | /** |
||
53 | * Insert data into the database. |
||
54 | * @since 2.7.0 |
||
55 | */ |
||
56 | View Code Duplication | public function create() { |
|
66 | |||
67 | /** |
||
68 | * Read from the database. |
||
69 | * @since 2.7.0 |
||
70 | * @param int $id ID of object to read. |
||
71 | */ |
||
72 | public function read( $id ) { |
||
87 | |||
88 | /** |
||
89 | * Update data in the database. |
||
90 | * @since 2.7.0 |
||
91 | */ |
||
92 | View Code Duplication | public function update() { |
|
100 | |||
101 | /** |
||
102 | * Delete data from the database. |
||
103 | * @since 2.7.0 |
||
104 | */ |
||
105 | public function delete() { |
||
108 | |||
109 | /** |
||
110 | * Get internal type (post type.) |
||
111 | * @return string |
||
112 | */ |
||
113 | public function get_type() { |
||
116 | |||
117 | /** |
||
118 | * Get status - always completed for refunds. |
||
119 | * @return string |
||
120 | */ |
||
121 | public function get_status() { |
||
124 | |||
125 | /** |
||
126 | * Get a title for the new post type. |
||
127 | */ |
||
128 | protected function get_post_title() { |
||
131 | |||
132 | /** |
||
133 | * Set refunded amount. |
||
134 | * @param string $value |
||
135 | * @throws WC_Data_Exception |
||
136 | */ |
||
137 | public function set_amount( $value ) { |
||
140 | |||
141 | /** |
||
142 | * Get refunded amount. |
||
143 | * @return int|float |
||
144 | */ |
||
145 | public function get_amount() { |
||
148 | |||
149 | /** |
||
150 | * Get formatted refunded amount. |
||
151 | * @since 2.4 |
||
152 | * @return string |
||
153 | */ |
||
154 | public function get_formatted_refund_amount() { |
||
157 | |||
158 | /** |
||
159 | * Set refund reason. |
||
160 | * @param string $value |
||
161 | * @throws WC_Data_Exception |
||
162 | */ |
||
163 | public function set_reason( $value ) { |
||
166 | |||
167 | /** |
||
168 | * Get refund reason. |
||
169 | * @since 2.2 |
||
170 | * @return int|float |
||
171 | */ |
||
172 | public function get_reason() { |
||
175 | |||
176 | /** |
||
177 | * Set refunded by. |
||
178 | * @param int $value |
||
179 | * @throws WC_Data_Exception |
||
180 | */ |
||
181 | public function set_refunded_by( $value ) { |
||
184 | |||
185 | /** |
||
186 | * Get ID of user who did the refund. |
||
187 | * @since 2.7 |
||
188 | * @return int |
||
189 | */ |
||
190 | public function get_refunded_by() { |
||
193 | |||
194 | /** |
||
195 | * Magic __get method for backwards compatibility. |
||
196 | * @param string $key |
||
197 | * @return mixed |
||
198 | */ |
||
199 | public function __get( $key ) { |
||
211 | |||
212 | /** |
||
213 | * Gets an refund from the database. |
||
214 | * @deprecated 2.7 |
||
215 | * @param int $id (default: 0). |
||
216 | * @return bool |
||
217 | */ |
||
218 | View Code Duplication | public function get_refund( $id = 0 ) { |
|
229 | |||
230 | /** |
||
231 | * Get refund amount. |
||
232 | * @deprecated 2.7 |
||
233 | * @return int|float |
||
234 | */ |
||
235 | public function get_refund_amount() { |
||
239 | |||
240 | /** |
||
241 | * Get refund reason. |
||
242 | * @deprecated 2.7 |
||
243 | * @return int|float |
||
244 | */ |
||
245 | public function get_refund_reason() { |
||
249 | } |
||
250 |
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.