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 extends WC_Data implements ArrayAccess { |
||
18 | |||
19 | /** |
||
20 | * Order Data array. This is the core order data exposed in APIs since 2.7.0. |
||
21 | * @since 2.7.0 |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $data = array( |
||
25 | 'order_id' => 0, |
||
26 | 'name' => '', |
||
27 | 'type' => '', |
||
28 | ); |
||
29 | |||
30 | /** |
||
31 | * May store an order to prevent retriving it multiple times. |
||
32 | * @var object |
||
33 | */ |
||
34 | protected $order; |
||
35 | |||
36 | /** |
||
37 | * Stores meta in cache for future reads. |
||
38 | * A group must be set to to enable caching. |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $cache_group = 'order_itemmeta'; |
||
42 | |||
43 | /** |
||
44 | * Meta type. This should match up with |
||
45 | * the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata. |
||
46 | * WP defines 'post', 'user', 'comment', and 'term'. |
||
47 | */ |
||
48 | protected $meta_type = 'order_item'; |
||
49 | |||
50 | /** |
||
51 | * Constructor. |
||
52 | * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
||
53 | */ |
||
54 | public function __construct( $read = 0 ) { |
||
67 | |||
68 | /** |
||
69 | * Type checking |
||
70 | * @param string|array $Type |
||
|
|||
71 | * @return boolean |
||
72 | */ |
||
73 | public function is_type( $type ) { |
||
76 | |||
77 | /** |
||
78 | * Get quantity. |
||
79 | * @return int |
||
80 | */ |
||
81 | public function get_quantity() { |
||
84 | |||
85 | /** |
||
86 | * Get parent order object. |
||
87 | * @return int |
||
88 | */ |
||
89 | public function get_order() { |
||
95 | |||
96 | /* |
||
97 | |-------------------------------------------------------------------------- |
||
98 | | Getters |
||
99 | |-------------------------------------------------------------------------- |
||
100 | */ |
||
101 | |||
102 | /** |
||
103 | * Get order ID this meta belongs to. |
||
104 | * @return int |
||
105 | */ |
||
106 | public function get_order_id() { |
||
109 | |||
110 | /** |
||
111 | * Get order item name. |
||
112 | * @return string |
||
113 | */ |
||
114 | public function get_name() { |
||
117 | |||
118 | /** |
||
119 | * Get order item type. |
||
120 | * @return string |
||
121 | */ |
||
122 | public function get_type() { |
||
125 | |||
126 | /* |
||
127 | |-------------------------------------------------------------------------- |
||
128 | | Setters |
||
129 | |-------------------------------------------------------------------------- |
||
130 | */ |
||
131 | |||
132 | /** |
||
133 | * Set order ID. |
||
134 | * @param int $value |
||
135 | * @throws WC_Data_Exception |
||
136 | */ |
||
137 | public function set_order_id( $value ) { |
||
140 | |||
141 | /** |
||
142 | * Set order item name. |
||
143 | * @param string $value |
||
144 | * @throws WC_Data_Exception |
||
145 | */ |
||
146 | public function set_name( $value ) { |
||
149 | |||
150 | /** |
||
151 | * Set order item type. |
||
152 | * @param string $value |
||
153 | * @throws WC_Data_Exception |
||
154 | */ |
||
155 | protected function set_type( $value ) { |
||
158 | |||
159 | /* |
||
160 | |-------------------------------------------------------------------------- |
||
161 | | CRUD methods |
||
162 | |-------------------------------------------------------------------------- |
||
163 | | |
||
164 | | Methods which create, read, update and delete data from the database. |
||
165 | | |
||
166 | */ |
||
167 | |||
168 | /** |
||
169 | * Insert data into the database. |
||
170 | * @since 2.7.0 |
||
171 | */ |
||
172 | View Code Duplication | public function create() { |
|
184 | |||
185 | /** |
||
186 | * Update data in the database. |
||
187 | * @since 2.7.0 |
||
188 | */ |
||
189 | View Code Duplication | public function update() { |
|
200 | |||
201 | /** |
||
202 | * Read from the database. |
||
203 | * @since 2.7.0 |
||
204 | * @param int|object $item ID of object to read, or already queried object. |
||
205 | */ |
||
206 | public function read( $item ) { |
||
231 | |||
232 | /** |
||
233 | * Save data to the database. |
||
234 | * @since 2.7.0 |
||
235 | * @return int Item ID |
||
236 | */ |
||
237 | public function save() { |
||
247 | |||
248 | /** |
||
249 | * Delete data from the database. |
||
250 | * @since 2.7.0 |
||
251 | */ |
||
252 | public function delete() { |
||
261 | |||
262 | /* |
||
263 | |-------------------------------------------------------------------------- |
||
264 | | Meta Data Handling |
||
265 | |-------------------------------------------------------------------------- |
||
266 | */ |
||
267 | |||
268 | /** |
||
269 | * Expands things like term slugs before return. |
||
270 | * @param string $hideprefix (default: _) |
||
271 | * @return array |
||
272 | */ |
||
273 | public function get_formatted_meta_data( $hideprefix = '_' ) { |
||
305 | |||
306 | /* |
||
307 | |-------------------------------------------------------------------------- |
||
308 | | Array Access Methods |
||
309 | |-------------------------------------------------------------------------- |
||
310 | | |
||
311 | | For backwards compat with legacy arrays. |
||
312 | | |
||
313 | */ |
||
314 | |||
315 | /** |
||
316 | * offsetSet for ArrayAccess |
||
317 | * @param string $offset |
||
318 | * @param mixed $value |
||
319 | */ |
||
320 | public function offsetSet( $offset, $value ) { |
||
334 | |||
335 | /** |
||
336 | * offsetUnset for ArrayAccess |
||
337 | * @param string $offset |
||
338 | */ |
||
339 | public function offsetUnset( $offset ) { |
||
351 | |||
352 | /** |
||
353 | * offsetExists for ArrayAccess |
||
354 | * @param string $offset |
||
355 | * @return bool |
||
356 | */ |
||
357 | public function offsetExists( $offset ) { |
||
363 | |||
364 | /** |
||
365 | * offsetGet for ArrayAccess |
||
366 | * @param string $offset |
||
367 | * @return mixed |
||
368 | */ |
||
369 | public function offsetGet( $offset ) { |
||
393 | } |
||
394 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.