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_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_Item_Product, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class WC_Item_Product extends WC_Item { |
||
15 | |||
16 | /** |
||
17 | * Data array. |
||
18 | * @since 2.7.0 |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $data = array( |
||
22 | 'name' => '', |
||
23 | 'product_id' => 0, |
||
24 | 'variation_id' => 0, |
||
25 | 'quantity' => 1, |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * Product this item represents. |
||
30 | * @var WC_Product |
||
31 | */ |
||
32 | protected $product = null; |
||
33 | |||
34 | /** |
||
35 | * offsetGet for ArrayAccess/Backwards compatibility. |
||
36 | * @deprecated Add deprecation notices in future release. |
||
37 | * @param string $offset |
||
38 | * @return mixed |
||
39 | */ |
||
40 | public function offsetGet( $offset ) { |
||
47 | |||
48 | /** |
||
49 | * offsetSet for ArrayAccess/Backwards compatibility. |
||
50 | * @deprecated Add deprecation notices in future release. |
||
51 | * @param string $offset |
||
52 | * @param mixed $value |
||
53 | */ |
||
54 | public function offsetSet( $offset, $value ) { |
||
64 | |||
65 | /** |
||
66 | * offsetExists for ArrayAccess |
||
67 | * @param string $offset |
||
68 | * @return bool |
||
69 | */ |
||
70 | public function offsetExists( $offset ) { |
||
76 | |||
77 | /** |
||
78 | * offsetUnset for ArrayAccess |
||
79 | * @param string $offset |
||
80 | */ |
||
81 | public function offsetUnset( $offset ) { |
||
84 | |||
85 | /** |
||
86 | * Get product object. |
||
87 | * @return WC_Product |
||
88 | */ |
||
89 | public function get_product() { |
||
92 | |||
93 | /** |
||
94 | * Gets price of the product. |
||
95 | * @return float |
||
96 | */ |
||
97 | public function get_price() { |
||
100 | |||
101 | /** |
||
102 | * Gets price of the product. |
||
103 | * @return float |
||
104 | */ |
||
105 | public function get_weight() { |
||
108 | |||
109 | /** |
||
110 | * Get tax status. |
||
111 | * @return string |
||
112 | */ |
||
113 | public function get_tax_status() { |
||
116 | |||
117 | /* |
||
118 | |-------------------------------------------------------------------------- |
||
119 | | Setters |
||
120 | |-------------------------------------------------------------------------- |
||
121 | */ |
||
122 | |||
123 | /** |
||
124 | * Set order item name. |
||
125 | * @param string $value |
||
126 | * @throws WC_Data_Exception |
||
127 | */ |
||
128 | public function set_name( $value ) { |
||
131 | |||
132 | /** |
||
133 | * Set quantity. |
||
134 | * @param int $value |
||
135 | * @throws WC_Data_Exception |
||
136 | */ |
||
137 | public function set_quantity( $value ) { |
||
143 | |||
144 | /** |
||
145 | * Set tax class. |
||
146 | * @param string $value |
||
147 | * @throws WC_Data_Exception |
||
148 | */ |
||
149 | public function set_tax_class( $value ) { |
||
155 | |||
156 | /** |
||
157 | * Set Product ID |
||
158 | * @param int $value |
||
159 | * @throws WC_Data_Exception |
||
160 | */ |
||
161 | public function set_product_id( $value ) { |
||
167 | |||
168 | /** |
||
169 | * Set variation ID. |
||
170 | * @param int $value |
||
171 | * @throws WC_Data_Exception |
||
172 | */ |
||
173 | public function set_variation_id( $value ) { |
||
179 | |||
180 | /** |
||
181 | * Set variation data (stored as meta data - write only). |
||
182 | * @param array $data Key/Value pairs |
||
183 | */ |
||
184 | public function set_variation( $data ) { |
||
189 | |||
190 | /** |
||
191 | * Set properties based on passed in product object. |
||
192 | * @param WC_Product $product |
||
193 | * @throws WC_Data_Exception |
||
194 | */ |
||
195 | public function set_product( $product ) { |
||
206 | |||
207 | /* |
||
208 | |-------------------------------------------------------------------------- |
||
209 | | Getters |
||
210 | |-------------------------------------------------------------------------- |
||
211 | */ |
||
212 | |||
213 | /** |
||
214 | * Get order item name. |
||
215 | * @return string |
||
216 | */ |
||
217 | public function get_name() { |
||
220 | |||
221 | /** |
||
222 | * Get item type. |
||
223 | * @return string |
||
224 | */ |
||
225 | public function get_type() { |
||
228 | |||
229 | /** |
||
230 | * Get product ID. |
||
231 | * @return int |
||
232 | */ |
||
233 | public function get_product_id() { |
||
236 | |||
237 | /** |
||
238 | * Get variation ID. |
||
239 | * @return int |
||
240 | */ |
||
241 | public function get_variation_id() { |
||
244 | |||
245 | /** |
||
246 | * Get quantity. |
||
247 | * @return int |
||
248 | */ |
||
249 | public function get_quantity() { |
||
252 | |||
253 | /** |
||
254 | * Get tax class. |
||
255 | * @return string |
||
256 | */ |
||
257 | public function get_tax_class() { |
||
260 | } |
||
261 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.