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_Item 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class WC_Order_Item implements ArrayAccess, WC_Data { |
||
18 | |||
19 | /** |
||
20 | * Data array, with defaults. |
||
21 | * @since 2.6.0 |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $_data = array( |
||
25 | 'order_id' => 0, |
||
26 | 'order_item_id' => 0, |
||
27 | 'name' => '', |
||
28 | 'type' => '', |
||
29 | ); |
||
30 | |||
31 | /** |
||
32 | * Stores additonal meta data. |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $_meta_data = array(); |
||
36 | |||
37 | /** |
||
38 | * Constructor. |
||
39 | * @param int|object|array $order_item ID to load from the DB (optional) or already queried data. |
||
40 | */ |
||
41 | public function __construct( $item = 0 ) { |
||
52 | |||
53 | /** |
||
54 | * Set all data based on input array. |
||
55 | * @param array $data |
||
56 | * @access private |
||
57 | */ |
||
58 | public function set_all( $data ) { |
||
71 | |||
72 | /** |
||
73 | * Change data to JSON format. |
||
74 | * @return string Data in JSON format. |
||
75 | */ |
||
76 | public function __toString() { |
||
79 | |||
80 | /** |
||
81 | * Type checking |
||
82 | * @param string|array $Type |
||
83 | * @return boolean |
||
84 | */ |
||
85 | public function is_type( $type ) { |
||
88 | |||
89 | /** |
||
90 | * Get qty. |
||
91 | * @return int |
||
92 | */ |
||
93 | public function get_qty() { |
||
96 | |||
97 | /* |
||
98 | |-------------------------------------------------------------------------- |
||
99 | | Getters |
||
100 | |-------------------------------------------------------------------------- |
||
101 | */ |
||
102 | |||
103 | /** |
||
104 | * Get all class data in array format. |
||
105 | * @since 2.6.0 |
||
106 | * @return array |
||
107 | */ |
||
108 | public function get_data() { |
||
111 | |||
112 | /** |
||
113 | * Get order item ID. |
||
114 | * @return int |
||
115 | */ |
||
116 | public function get_id() { |
||
119 | |||
120 | /** |
||
121 | * Get order ID this meta belongs to. |
||
122 | * @return int |
||
123 | */ |
||
124 | public function get_order_id() { |
||
127 | |||
128 | /** |
||
129 | * Get order item ID this meta belongs to. |
||
130 | * @return int |
||
131 | */ |
||
132 | public function get_order_item_id() { |
||
135 | |||
136 | /** |
||
137 | * Get order item name. |
||
138 | * @return string |
||
139 | */ |
||
140 | public function get_name() { |
||
143 | |||
144 | /** |
||
145 | * Get order item type. |
||
146 | * @return string |
||
147 | */ |
||
148 | public function get_type() { |
||
151 | |||
152 | /* |
||
153 | |-------------------------------------------------------------------------- |
||
154 | | Setters |
||
155 | |-------------------------------------------------------------------------- |
||
156 | */ |
||
157 | |||
158 | /** |
||
159 | * Set ID |
||
160 | * @param int $value |
||
161 | */ |
||
162 | public function set_id( $value ) { |
||
165 | |||
166 | /** |
||
167 | * Set order ID. |
||
168 | * @param int $value |
||
169 | */ |
||
170 | public function set_order_id( $value ) { |
||
173 | |||
174 | /** |
||
175 | * Set order item ID. |
||
176 | * @param int $value |
||
177 | */ |
||
178 | public function set_order_item_id( $value ) { |
||
181 | |||
182 | /** |
||
183 | * Set order item name. |
||
184 | * @param string $value |
||
185 | */ |
||
186 | public function set_name( $value ) { |
||
189 | |||
190 | /** |
||
191 | * Set order item type. |
||
192 | * @param string $value |
||
193 | */ |
||
194 | public function set_type( $value ) { |
||
198 | |||
199 | /* |
||
200 | |-------------------------------------------------------------------------- |
||
201 | | CRUD methods |
||
202 | |-------------------------------------------------------------------------- |
||
203 | | |
||
204 | | Methods which create, read, update and delete data from the database. |
||
205 | | |
||
206 | */ |
||
207 | |||
208 | /** |
||
209 | * Insert data into the database. |
||
210 | * @since 2.6.0 |
||
211 | */ |
||
212 | View Code Duplication | public function create() { |
|
224 | |||
225 | /** |
||
226 | * Update data in the database. |
||
227 | * @since 2.6.0 |
||
228 | */ |
||
229 | View Code Duplication | public function update() { |
|
240 | |||
241 | /** |
||
242 | * Read from the database. |
||
243 | * @since 2.6.0 |
||
244 | * @param int|object $item ID of object to read, or already queried object. |
||
245 | */ |
||
246 | public function read( $item ) { |
||
265 | |||
266 | /** |
||
267 | * Save data to the database. |
||
268 | * @since 2.6.0 |
||
269 | * @return int Item ID |
||
270 | */ |
||
271 | public function save() { |
||
281 | |||
282 | /** |
||
283 | * Delete data from the database. |
||
284 | * @since 2.6.0 |
||
285 | */ |
||
286 | public function delete() { |
||
295 | |||
296 | /* |
||
297 | |-------------------------------------------------------------------------- |
||
298 | | Meta Data Handling |
||
299 | |-------------------------------------------------------------------------- |
||
300 | */ |
||
301 | |||
302 | /** |
||
303 | * Get All Meta Data |
||
304 | * @return array |
||
305 | */ |
||
306 | public function get_meta_data() { |
||
309 | |||
310 | /** |
||
311 | * Expands things like term slugs before return. |
||
312 | * @param string $hideprefix (default: _) |
||
313 | * @return array |
||
314 | */ |
||
315 | public function get_formatted_meta_data( $hideprefix = '_' ) { |
||
345 | |||
346 | /** |
||
347 | * Internal meta keys we don't want exposed as part of meta_data. |
||
348 | * @return array() |
||
349 | */ |
||
350 | protected function get_internal_meta_keys() { |
||
353 | |||
354 | /** |
||
355 | * Get Meta Data by Key |
||
356 | * @param string $key |
||
357 | * @param bool $single return first found meta with key, or all with $key |
||
358 | * @return mixed |
||
359 | */ |
||
360 | View Code Duplication | public function get_meta( $key = '', $single = true ) { |
|
374 | |||
375 | /** |
||
376 | * Set all meta data from array. |
||
377 | * @param array $data Key/Value pairs |
||
378 | */ |
||
379 | View Code Duplication | public function set_meta_data( $data ) { |
|
392 | |||
393 | /** |
||
394 | * Add meta data. |
||
395 | * @param array $data Key/Value pairs |
||
396 | */ |
||
397 | View Code Duplication | public function add_meta_data( $key, $value, $unique = false ) { |
|
407 | |||
408 | /** |
||
409 | * Update meta data by key or ID, if provided. |
||
410 | * @param string $key |
||
411 | * @param string $value |
||
412 | * @param int $meta_id |
||
413 | */ |
||
414 | View Code Duplication | public function update_meta_data( $key, $value, $meta_id = '' ) { |
|
424 | |||
425 | /** |
||
426 | * Read Meta Data from the database. Ignore any internal properties. |
||
427 | */ |
||
428 | View Code Duplication | protected function read_meta_data() { |
|
455 | |||
456 | /** |
||
457 | * Update Meta Data in the database. |
||
458 | */ |
||
459 | View Code Duplication | protected function save_meta_data() { |
|
483 | |||
484 | /* |
||
485 | |-------------------------------------------------------------------------- |
||
486 | | Array Access Methods |
||
487 | |-------------------------------------------------------------------------- |
||
488 | | |
||
489 | | For backwards compat with legacy arrays. |
||
490 | | |
||
491 | */ |
||
492 | |||
493 | /** |
||
494 | * offsetSet for ArrayAccess |
||
495 | * @param string $offset |
||
496 | * @param mixed $value |
||
497 | */ |
||
498 | public function offsetSet( $offset, $value ) { |
||
505 | |||
506 | /** |
||
507 | * offsetUnset for ArrayAccess |
||
508 | * @param string $offset |
||
509 | */ |
||
510 | public function offsetUnset( $offset ) { |
||
517 | |||
518 | /** |
||
519 | * offsetExists for ArrayAccess |
||
520 | * @param string $offset |
||
521 | * @return bool |
||
522 | */ |
||
523 | public function offsetExists( $offset ) { |
||
529 | |||
530 | /** |
||
531 | * offsetGet for ArrayAccess |
||
532 | * @param string $offset |
||
533 | * @return mixed |
||
534 | */ |
||
535 | public function offsetGet( $offset ) { |
||
544 | } |
||
545 |
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.