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 | 'id' => 0, // order_item_id |
||
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 ) { |
||
79 | |||
80 | /** |
||
81 | * Type checking |
||
82 | * @param string|array $Type |
||
83 | * @return boolean |
||
84 | */ |
||
85 | public function is_type( $type ) { |
||
88 | |||
89 | /** |
||
90 | * Get quantity. |
||
91 | * @return int |
||
92 | */ |
||
93 | public function get_quantity() { |
||
96 | |||
97 | /** |
||
98 | * Get parent order object. |
||
99 | * @return int |
||
100 | */ |
||
101 | public function get_order() { |
||
102 | if ( ! $this->_order ) { |
||
103 | $this->_order = wc_get_order( $this->get_order_id() ); |
||
104 | } |
||
105 | return $this->_order; |
||
106 | } |
||
107 | |||
108 | /* |
||
109 | |-------------------------------------------------------------------------- |
||
110 | | Getters |
||
111 | |-------------------------------------------------------------------------- |
||
112 | */ |
||
113 | |||
114 | /** |
||
115 | * Get order item ID. |
||
116 | * @return int |
||
117 | */ |
||
118 | public function get_id() { |
||
121 | |||
122 | /** |
||
123 | * Get order ID this meta belongs to. |
||
124 | * @return int |
||
125 | */ |
||
126 | public function get_order_id() { |
||
129 | |||
130 | /** |
||
131 | * Get order item name. |
||
132 | * @return string |
||
133 | */ |
||
134 | public function get_name() { |
||
137 | |||
138 | /** |
||
139 | * Get order item type. |
||
140 | * @return string |
||
141 | */ |
||
142 | public function get_type() { |
||
145 | |||
146 | /* |
||
147 | |-------------------------------------------------------------------------- |
||
148 | | Setters |
||
149 | |-------------------------------------------------------------------------- |
||
150 | */ |
||
151 | |||
152 | /** |
||
153 | * Set ID |
||
154 | * @param int $value |
||
155 | */ |
||
156 | public function set_id( $value ) { |
||
159 | |||
160 | /** |
||
161 | * Set order ID. |
||
162 | * @param int $value |
||
163 | */ |
||
164 | public function set_order_id( $value ) { |
||
167 | |||
168 | /** |
||
169 | * Set order item name. |
||
170 | * @param string $value |
||
171 | */ |
||
172 | public function set_name( $value ) { |
||
175 | |||
176 | /** |
||
177 | * Set order item type. |
||
178 | * @param string $value |
||
179 | */ |
||
180 | protected function set_type( $value ) { |
||
181 | $this->_data['type'] = wc_clean( $value ); |
||
182 | } |
||
183 | |||
184 | /* |
||
185 | |-------------------------------------------------------------------------- |
||
186 | | CRUD methods |
||
187 | |-------------------------------------------------------------------------- |
||
188 | | |
||
189 | | Methods which create, read, update and delete data from the database. |
||
190 | | |
||
191 | */ |
||
192 | |||
193 | /** |
||
194 | * Insert data into the database. |
||
195 | * @since 2.7.0 |
||
196 | */ |
||
197 | View Code Duplication | public function create() { |
|
209 | |||
210 | /** |
||
211 | * Update data in the database. |
||
212 | * @since 2.7.0 |
||
213 | */ |
||
214 | View Code Duplication | public function update() { |
|
225 | |||
226 | /** |
||
227 | * Read from the database. |
||
228 | * @since 2.7.0 |
||
229 | * @param int|object $item ID of object to read, or already queried object. |
||
230 | */ |
||
231 | public function read( $item ) { |
||
250 | |||
251 | /** |
||
252 | * Save data to the database. |
||
253 | * @since 2.7.0 |
||
254 | * @return int Item ID |
||
255 | */ |
||
256 | public function save() { |
||
266 | |||
267 | /** |
||
268 | * Delete data from the database. |
||
269 | * @since 2.7.0 |
||
270 | */ |
||
271 | public function delete() { |
||
280 | |||
281 | /* |
||
282 | |-------------------------------------------------------------------------- |
||
283 | | Meta Data Handling |
||
284 | |-------------------------------------------------------------------------- |
||
285 | */ |
||
286 | |||
287 | /** |
||
288 | * Expands things like term slugs before return. |
||
289 | * @param string $hideprefix (default: _) |
||
290 | * @return array |
||
291 | */ |
||
292 | public function get_formatted_meta_data( $hideprefix = '_' ) { |
||
324 | |||
325 | /* |
||
326 | |-------------------------------------------------------------------------- |
||
327 | | Array Access Methods |
||
328 | |-------------------------------------------------------------------------- |
||
329 | | |
||
330 | | For backwards compat with legacy arrays. |
||
331 | | |
||
332 | */ |
||
333 | |||
334 | /** |
||
335 | * offsetSet for ArrayAccess |
||
336 | * @param string $offset |
||
337 | * @param mixed $value |
||
338 | */ |
||
339 | public function offsetSet( $offset, $value ) { |
||
353 | |||
354 | /** |
||
355 | * offsetUnset for ArrayAccess |
||
356 | * @param string $offset |
||
357 | */ |
||
358 | public function offsetUnset( $offset ) { |
||
370 | |||
371 | /** |
||
372 | * offsetExists for ArrayAccess |
||
373 | * @param string $offset |
||
374 | * @return bool |
||
375 | */ |
||
376 | public function offsetExists( $offset ) { |
||
382 | |||
383 | /** |
||
384 | * offsetGet for ArrayAccess |
||
385 | * @param string $offset |
||
386 | * @return mixed |
||
387 | */ |
||
388 | public function offsetGet( $offset ) { |
||
412 | } |
||
413 |
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.