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 CartStrategy 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 CartStrategy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class CartStrategy extends AbstractImportStrategy |
||
17 | { |
||
18 | /** |
||
19 | * @var Cart |
||
20 | */ |
||
21 | protected $existingEntity; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $existingCartItems; |
||
27 | |||
28 | /** |
||
29 | * @param Cart $entity |
||
30 | * |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | protected function beforeProcessEntity($entity) |
||
44 | |||
45 | /** |
||
46 | * @param Cart $entity |
||
47 | * |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | View Code Duplication | public function process($entity) |
|
63 | |||
64 | /** |
||
65 | * @param Cart $cart |
||
66 | * @return bool |
||
67 | */ |
||
68 | protected function isProcessingAllowed(Cart $cart) |
||
69 | { |
||
70 | $isProcessingAllowed = true; |
||
71 | |||
72 | if ($cart->getCustomer()) { |
||
73 | $customer = $this->findExistingEntity($cart->getCustomer()); |
||
74 | |||
75 | $customerOriginId = $cart->getCustomer()->getOriginId(); |
||
76 | if (!$customer && $customerOriginId) { |
||
77 | $this->appendDataToContext(ContextCustomerReader::CONTEXT_POST_PROCESS_CUSTOMERS, $customerOriginId); |
||
78 | |||
79 | $isProcessingAllowed = false; |
||
80 | } |
||
81 | |||
82 | if (!$customer && $cart->getIsGuest()) { |
||
83 | /** @var MagentoSoapTransport $transport */ |
||
84 | $channel = $this->databaseHelper->findOneByIdentity($cart->getChannel()); |
||
85 | $transport = $channel->getTransport(); |
||
86 | if ($transport->getGuestCustomerSync()) { |
||
87 | $this->appendDataToContext( |
||
88 | 'postProcessGuestCustomers', |
||
89 | GuestCustomerDataConverter::extractCustomersValues((array)$this->context->getValue('itemData')) |
||
90 | ); |
||
91 | |||
92 | $isProcessingAllowed = false; |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return $isProcessingAllowed; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param Cart $entity |
||
102 | * |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | protected function afterProcessEntity($entity) |
||
132 | |||
133 | /** |
||
134 | * Update removed cart items - set `removed` field to true if cart item was removed from a cart |
||
135 | * |
||
136 | * @param Cart $entity |
||
137 | */ |
||
138 | protected function updateRemovedCartItems(Cart $entity) |
||
161 | |||
162 | /** |
||
163 | * Update Customer email |
||
164 | * |
||
165 | * @param Cart $cart |
||
166 | * |
||
167 | * @return CartStrategy |
||
168 | */ |
||
169 | protected function updateCustomer(Cart $cart) |
||
178 | |||
179 | /** |
||
180 | * @param Cart $cart |
||
181 | * |
||
182 | * @return CartStrategy |
||
183 | */ |
||
184 | protected function updateCartItems(Cart $cart) |
||
193 | |||
194 | /** |
||
195 | * @param Cart $entity |
||
196 | * |
||
197 | * @return CartStrategy |
||
198 | */ |
||
199 | protected function updateAddresses(Cart $entity) |
||
223 | |||
224 | /** |
||
225 | * @param Cart $entity |
||
226 | * @return null |
||
227 | */ |
||
228 | protected function hasContactInfo(Cart $entity) |
||
244 | |||
245 | /** |
||
246 | * Update cart status |
||
247 | * |
||
248 | * @param Cart $cart |
||
249 | * |
||
250 | * @return CartStrategy |
||
251 | */ |
||
252 | protected function updateCartStatus(Cart $cart) |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | protected function findExistingEntity($entity, array $searchContext = []) |
||
296 | } |
||
297 |
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.