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 | * Data array, with defaults. |
||
21 | * @since 2.7.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 | * May store an order to prevent retriving it multiple times. |
||
33 | * @var object |
||
34 | */ |
||
35 | protected $_order; |
||
36 | |||
37 | /** |
||
38 | * Stores meta in cache for future reads. |
||
39 | * A group must be set to to enable caching. |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $_cache_group = 'order_itemmeta'; |
||
43 | |||
44 | /** |
||
45 | * Meta type. This should match up with |
||
46 | * the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata. |
||
47 | * WP defines 'post', 'user', 'comment', and 'term'. |
||
48 | */ |
||
49 | protected $_meta_type = 'order_item'; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * @param int|object|array $order_item ID to load from the DB (optional) or already queried data. |
||
|
|||
54 | */ |
||
55 | public function __construct( $item = 0 ) { |
||
66 | |||
67 | /** |
||
68 | * Set all data based on input array. |
||
69 | * @param array $data |
||
70 | * @access private |
||
71 | */ |
||
72 | public function set_all( $data ) { |
||
81 | |||
82 | /** |
||
83 | * Type checking |
||
84 | * @param string|array $Type |
||
85 | * @return boolean |
||
86 | */ |
||
87 | public function is_type( $type ) { |
||
90 | |||
91 | /** |
||
92 | * Get qty. |
||
93 | * @return int |
||
94 | */ |
||
95 | public function get_qty() { |
||
98 | |||
99 | /** |
||
100 | * Get parent order object. |
||
101 | * @return int |
||
102 | */ |
||
103 | public function get_order() { |
||
109 | |||
110 | /* |
||
111 | |-------------------------------------------------------------------------- |
||
112 | | Getters |
||
113 | |-------------------------------------------------------------------------- |
||
114 | */ |
||
115 | |||
116 | /** |
||
117 | * Get order item ID. |
||
118 | * @return int |
||
119 | */ |
||
120 | public function get_id() { |
||
123 | |||
124 | /** |
||
125 | * Get order ID this meta belongs to. |
||
126 | * @return int |
||
127 | */ |
||
128 | public function get_order_id() { |
||
131 | |||
132 | /** |
||
133 | * Get order item ID this meta belongs to. |
||
134 | * @return int |
||
135 | */ |
||
136 | protected function get_order_item_id() { |
||
139 | |||
140 | /** |
||
141 | * Get order item name. |
||
142 | * @return string |
||
143 | */ |
||
144 | public function get_name() { |
||
147 | |||
148 | /** |
||
149 | * Get order item type. |
||
150 | * @return string |
||
151 | */ |
||
152 | public function get_type() { |
||
155 | |||
156 | /* |
||
157 | |-------------------------------------------------------------------------- |
||
158 | | Setters |
||
159 | |-------------------------------------------------------------------------- |
||
160 | */ |
||
161 | |||
162 | /** |
||
163 | * Set ID |
||
164 | * @param int $value |
||
165 | */ |
||
166 | public function set_id( $value ) { |
||
169 | |||
170 | /** |
||
171 | * Set order ID. |
||
172 | * @param int $value |
||
173 | */ |
||
174 | public function set_order_id( $value ) { |
||
177 | |||
178 | /** |
||
179 | * Set order item ID. |
||
180 | * @param int $value |
||
181 | */ |
||
182 | protected function set_order_item_id( $value ) { |
||
185 | |||
186 | /** |
||
187 | * Set order item name. |
||
188 | * @param string $value |
||
189 | */ |
||
190 | public function set_name( $value ) { |
||
193 | |||
194 | /** |
||
195 | * Set order item type. |
||
196 | * @param string $value |
||
197 | */ |
||
198 | protected function set_type( $value ) { |
||
201 | |||
202 | /* |
||
203 | |-------------------------------------------------------------------------- |
||
204 | | CRUD methods |
||
205 | |-------------------------------------------------------------------------- |
||
206 | | |
||
207 | | Methods which create, read, update and delete data from the database. |
||
208 | | |
||
209 | */ |
||
210 | |||
211 | /** |
||
212 | * Insert data into the database. |
||
213 | * @since 2.7.0 |
||
214 | */ |
||
215 | View Code Duplication | public function create() { |
|
227 | |||
228 | /** |
||
229 | * Update data in the database. |
||
230 | * @since 2.7.0 |
||
231 | */ |
||
232 | View Code Duplication | public function update() { |
|
243 | |||
244 | /** |
||
245 | * Read from the database. |
||
246 | * @since 2.7.0 |
||
247 | * @param int|object $item ID of object to read, or already queried object. |
||
248 | */ |
||
249 | public function read( $item ) { |
||
268 | |||
269 | /** |
||
270 | * Save data to the database. |
||
271 | * @since 2.7.0 |
||
272 | * @return int Item ID |
||
273 | */ |
||
274 | public function save() { |
||
284 | |||
285 | /** |
||
286 | * Delete data from the database. |
||
287 | * @since 2.7.0 |
||
288 | */ |
||
289 | public function delete() { |
||
298 | |||
299 | /* |
||
300 | |-------------------------------------------------------------------------- |
||
301 | | Meta Data Handling |
||
302 | |-------------------------------------------------------------------------- |
||
303 | */ |
||
304 | |||
305 | /** |
||
306 | * Expands things like term slugs before return. |
||
307 | * @param string $hideprefix (default: _) |
||
308 | * @return array |
||
309 | */ |
||
310 | public function get_formatted_meta_data( $hideprefix = '_' ) { |
||
340 | |||
341 | /* |
||
342 | |-------------------------------------------------------------------------- |
||
343 | | Array Access Methods |
||
344 | |-------------------------------------------------------------------------- |
||
345 | | |
||
346 | | For backwards compat with legacy arrays. |
||
347 | | |
||
348 | */ |
||
349 | |||
350 | /** |
||
351 | * offsetSet for ArrayAccess |
||
352 | * @param string $offset |
||
353 | * @param mixed $value |
||
354 | */ |
||
355 | public function offsetSet( $offset, $value ) { |
||
369 | |||
370 | /** |
||
371 | * offsetUnset for ArrayAccess |
||
372 | * @param string $offset |
||
373 | */ |
||
374 | public function offsetUnset( $offset ) { |
||
386 | |||
387 | /** |
||
388 | * offsetExists for ArrayAccess |
||
389 | * @param string $offset |
||
390 | * @return bool |
||
391 | */ |
||
392 | public function offsetExists( $offset ) { |
||
398 | |||
399 | /** |
||
400 | * offsetGet for ArrayAccess |
||
401 | * @param string $offset |
||
402 | * @return mixed |
||
403 | */ |
||
404 | public function offsetGet( $offset ) { |
||
428 | } |
||
429 |
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.