Complex classes like WC_Order_Item_Product 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_Item_Product, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class WC_Order_Item_Product extends WC_Order_Item { |
||
15 | |||
16 | /** |
||
17 | * Data properties of this order item object. |
||
18 | * @since 2.6.0 |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $_data = array( |
||
22 | 'order_id' => 0, |
||
23 | 'order_item_id' => 0, |
||
24 | 'name' => '', |
||
25 | 'product_id' => 0, |
||
26 | 'variation_id' => 0, |
||
27 | 'qty' => 0, |
||
28 | 'tax_class' => '', |
||
29 | 'subtotal' => 0, |
||
30 | 'subtotal_tax' => 0, |
||
31 | 'total' => 0, |
||
32 | 'total_tax' => 0, |
||
33 | 'taxes' => array( |
||
34 | 'subtotal' => array(), |
||
35 | 'total' => array() |
||
36 | ), |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * offsetGet for ArrayAccess/Backwards compatibility. |
||
41 | * @deprecated Add deprecation notices in future release. |
||
42 | * @param string $offset |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function offsetGet( $offset ) { |
||
59 | |||
60 | /** |
||
61 | * offsetExists for ArrayAccess |
||
62 | * @param string $offset |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function offsetExists( $offset ) { |
||
71 | |||
72 | /** |
||
73 | * Read/populate data properties specific to this order item. |
||
74 | */ |
||
75 | public function read( $id ) { |
||
89 | |||
90 | /** |
||
91 | * Save properties specific to this order item. |
||
92 | * @return int Item ID |
||
93 | */ |
||
94 | public function save() { |
||
110 | |||
111 | /** |
||
112 | * Internal meta keys we don't want exposed as part of meta_data. |
||
113 | * @return array() |
||
114 | */ |
||
115 | protected function get_internal_meta_keys() { |
||
118 | |||
119 | /** |
||
120 | * Get the associated product. |
||
121 | * @return WC_Product|bool |
||
122 | */ |
||
123 | public function get_product() { |
||
137 | |||
138 | /** |
||
139 | * Get any associated downloadable files. |
||
140 | * @return array |
||
141 | */ |
||
142 | public function get_item_downloads() { |
||
175 | |||
176 | /** |
||
177 | * Get tax status. |
||
178 | * @return string |
||
179 | */ |
||
180 | public function get_tax_status() { |
||
184 | |||
185 | /* |
||
186 | |-------------------------------------------------------------------------- |
||
187 | | Setters |
||
188 | |-------------------------------------------------------------------------- |
||
189 | */ |
||
190 | |||
191 | /** |
||
192 | * Set qty. |
||
193 | * @param int $value |
||
194 | */ |
||
195 | public function set_qty( $value ) { |
||
198 | |||
199 | /** |
||
200 | * Set tax class. |
||
201 | * @param string $value |
||
202 | */ |
||
203 | public function set_tax_class( $value ) { |
||
206 | |||
207 | /** |
||
208 | * Set Product ID |
||
209 | * @param int $value |
||
210 | */ |
||
211 | public function set_product_id( $value ) { |
||
214 | |||
215 | /** |
||
216 | * Set variation ID. |
||
217 | * @param int $value |
||
218 | */ |
||
219 | public function set_variation_id( $value ) { |
||
222 | |||
223 | /** |
||
224 | * Line subtotal (before discounts). |
||
225 | * @param string $value |
||
226 | */ |
||
227 | public function set_subtotal( $value ) { |
||
230 | |||
231 | /** |
||
232 | * Line total (after discounts). |
||
233 | * @param string $value |
||
234 | */ |
||
235 | public function set_total( $value ) { |
||
238 | |||
239 | /** |
||
240 | * Line subtotal tax (before discounts). |
||
241 | * @param string $value |
||
242 | */ |
||
243 | public function set_subtotal_tax( $value ) { |
||
246 | |||
247 | /** |
||
248 | * Line total tax (after discounts). |
||
249 | * @param string $value |
||
250 | */ |
||
251 | public function set_total_tax( $value ) { |
||
254 | |||
255 | /** |
||
256 | * Set line taxes. |
||
257 | * @param array $raw_tax_data |
||
258 | */ |
||
259 | public function set_taxes( $raw_tax_data ) { |
||
271 | |||
272 | /** |
||
273 | * Set variation data (stored as meta data - write only). |
||
274 | * @param array $data Key/Value pairs |
||
275 | */ |
||
276 | public function set_variation( $data ) { |
||
281 | |||
282 | /** |
||
283 | * Set properties based on passed in product object. |
||
284 | * @param WC_Product $product |
||
285 | */ |
||
286 | public function set_product( $product ) { |
||
295 | |||
296 | /* |
||
297 | |-------------------------------------------------------------------------- |
||
298 | | Getters |
||
299 | |-------------------------------------------------------------------------- |
||
300 | */ |
||
301 | |||
302 | /** |
||
303 | * Get order item type. |
||
304 | * @return string |
||
305 | */ |
||
306 | public function get_type() { |
||
309 | |||
310 | /** |
||
311 | * Get product ID. |
||
312 | * @return int |
||
313 | */ |
||
314 | public function get_product_id() { |
||
317 | |||
318 | /** |
||
319 | * Get variation ID. |
||
320 | * @return int |
||
321 | */ |
||
322 | public function get_variation_id() { |
||
325 | |||
326 | /** |
||
327 | * Get qty. |
||
328 | * @return int |
||
329 | */ |
||
330 | public function get_qty() { |
||
333 | |||
334 | /** |
||
335 | * Get tax class. |
||
336 | * @return string |
||
337 | */ |
||
338 | public function get_tax_class() { |
||
341 | |||
342 | /** |
||
343 | * Get subtotal. |
||
344 | * @return string |
||
345 | */ |
||
346 | public function get_subtotal() { |
||
349 | |||
350 | /** |
||
351 | * Get subtotal tax. |
||
352 | * @return string |
||
353 | */ |
||
354 | public function get_subtotal_tax() { |
||
357 | |||
358 | /** |
||
359 | * Get total. |
||
360 | * @return string |
||
361 | */ |
||
362 | public function get_total() { |
||
365 | |||
366 | /** |
||
367 | * Get total tax. |
||
368 | * @return string |
||
369 | */ |
||
370 | public function get_total_tax() { |
||
373 | |||
374 | /** |
||
375 | * Get fee taxes. |
||
376 | * @return array |
||
377 | */ |
||
378 | public function get_taxes() { |
||
381 | } |
||
382 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.