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:
1 | <?php |
||
14 | class WC_Cart_Items { |
||
15 | |||
16 | /** |
||
17 | * An array of items in the cart. |
||
18 | * @var WC_Item_Product[] |
||
19 | */ |
||
20 | private $items = array(); |
||
21 | |||
22 | /** |
||
23 | * An array of items removed from the cart which can be restored. |
||
24 | * @var WC_Item_Product[] |
||
25 | */ |
||
26 | private $removed_items = array(); |
||
27 | |||
28 | /** |
||
29 | * Constructor. |
||
30 | */ |
||
31 | public function __construct() { |
||
34 | |||
35 | /** |
||
36 | * Generate a unique ID for the cart item being added. |
||
37 | * |
||
38 | * @param array $item Cart item. |
||
39 | * @return string cart item key |
||
40 | */ |
||
41 | public function generate_key( $item ) { |
||
45 | |||
46 | /** |
||
47 | * Get an item by it's key. |
||
48 | * |
||
49 | * @param string $item_key |
||
50 | * @return WC_Item_Product|bool |
||
51 | */ |
||
52 | public function get_item_by_key( $item_key ) { |
||
55 | |||
56 | /** |
||
57 | * Get items. |
||
58 | * @return array |
||
59 | */ |
||
60 | public function get_items() { |
||
63 | |||
64 | /** |
||
65 | * Get removed items. |
||
66 | * @return array |
||
67 | */ |
||
68 | public function get_removed_items() { |
||
71 | |||
72 | /** |
||
73 | * Set items. |
||
74 | * @param array $items |
||
75 | */ |
||
76 | View Code Duplication | public function set_items( $items ) { |
|
87 | |||
88 | /** |
||
89 | * Set items. |
||
90 | * @param array $items |
||
91 | */ |
||
92 | View Code Duplication | public function set_removed_items( $items ) { |
|
103 | |||
104 | /** |
||
105 | * Remove a cart item. |
||
106 | * @param string $cart_item_key |
||
107 | * @return bool |
||
108 | */ |
||
109 | View Code Duplication | public function remove_item( $cart_item_key ) { |
|
121 | |||
122 | /** |
||
123 | * Restore a cart item. |
||
124 | * @param string $cart_item_key |
||
125 | * @return bool |
||
126 | */ |
||
127 | View Code Duplication | public function restore_item( $cart_item_key ) { |
|
139 | |||
140 | /** |
||
141 | * Filter items needing shipping callback. |
||
142 | * @return bool |
||
143 | */ |
||
144 | protected function filter_items_needing_shipping( $item ) { |
||
148 | |||
149 | /** |
||
150 | * Get only items that need shipping. |
||
151 | * @return array |
||
152 | */ |
||
153 | public function get_items_needing_shipping() { |
||
156 | |||
157 | /** |
||
158 | * Check all cart items for errors. |
||
159 | */ |
||
160 | public function check_items() { |
||
177 | |||
178 | /** |
||
179 | * Get number of items in the cart. |
||
180 | * @return int |
||
181 | */ |
||
182 | public function get_quantity() { |
||
185 | |||
186 | /** |
||
187 | * Get all tax classes for items in the cart. |
||
188 | * @return array |
||
189 | */ |
||
190 | public function get_tax_classes() { |
||
193 | |||
194 | /** |
||
195 | * Get weight of items in the cart. |
||
196 | * @return int |
||
197 | */ |
||
198 | public function get_weight() { |
||
201 | } |
||
202 |
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.