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 PurchaseFlow 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 PurchaseFlow, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class PurchaseFlow |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $flowType; |
||
28 | |||
29 | /** |
||
30 | * @var ArrayCollection|ItemPreprocessor[] |
||
31 | */ |
||
32 | protected $itemPreprocessors; |
||
33 | |||
34 | /** |
||
35 | * @var ArrayCollection|ItemHolderPreprocessor[] |
||
36 | */ |
||
37 | protected $itemHolderPreprocessors; |
||
38 | |||
39 | /** |
||
40 | * @var ArrayCollection|ItemValidator[] |
||
41 | */ |
||
42 | protected $itemValidators; |
||
43 | |||
44 | /** |
||
45 | * @var ArrayCollection|ItemHolderValidator[] |
||
46 | */ |
||
47 | protected $itemHolderValidators; |
||
48 | |||
49 | /** |
||
50 | * @var ArrayCollection|ItemHolderPostValidator[] |
||
51 | */ |
||
52 | protected $itemHolderPostValidators; |
||
53 | |||
54 | /** |
||
55 | * @var ArrayCollection|DiscountProcessor[] |
||
56 | */ |
||
57 | protected $discountProcessors; |
||
58 | |||
59 | /** |
||
60 | * @var ArrayCollection|PurchaseProcessor[] |
||
61 | */ |
||
62 | protected $purchaseProcessors; |
||
63 | |||
64 | public function __construct() |
||
74 | |||
75 | public function setFlowType($flowType) |
||
79 | |||
80 | public function setPurchaseProcessors(ArrayCollection $processors) |
||
84 | |||
85 | public function setItemValidators(ArrayCollection $itemValidators) |
||
89 | |||
90 | public function setItemHolderValidators(ArrayCollection $itemHolderValidators) |
||
94 | |||
95 | public function setItemPreprocessors(ArrayCollection $itemPreprocessors) |
||
99 | |||
100 | public function setItemHolderPreprocessors(ArrayCollection $itemHolderPreprocessors) |
||
104 | |||
105 | public function setItemHolderPostValidators(ArrayCollection $itemHolderPostValidators) |
||
109 | |||
110 | public function setDiscountProcessors(ArrayCollection $discountProcessors) |
||
114 | |||
115 | public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
179 | |||
180 | /** |
||
181 | * 購入フロー仮確定処理. |
||
182 | * |
||
183 | * @param ItemHolderInterface $target |
||
184 | * @param PurchaseContext $context |
||
185 | * |
||
186 | * @throws PurchaseException |
||
187 | */ |
||
188 | public function prepare(ItemHolderInterface $target, PurchaseContext $context) |
||
196 | |||
197 | /** |
||
198 | * 購入フロー確定処理. |
||
199 | * |
||
200 | * @param ItemHolderInterface $target |
||
201 | * @param PurchaseContext $context |
||
202 | * |
||
203 | * @throws PurchaseException |
||
204 | */ |
||
205 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
213 | |||
214 | /** |
||
215 | * 購入フロー仮確定取り消し処理. |
||
216 | * |
||
217 | * @param ItemHolderInterface $target |
||
218 | * @param PurchaseContext $context |
||
219 | */ |
||
220 | public function rollback(ItemHolderInterface $target, PurchaseContext $context) |
||
228 | |||
229 | public function addPurchaseProcessor(PurchaseProcessor $processor) |
||
233 | |||
234 | public function addItemHolderPreprocessor(ItemHolderPreprocessor $holderPreprocessor) |
||
238 | |||
239 | public function addItemPreprocessor(ItemPreprocessor $itemPreprocessor) |
||
243 | |||
244 | public function addItemValidator(ItemValidator $itemValidator) |
||
248 | |||
249 | public function addItemHolderValidator(ItemHolderValidator $itemHolderValidator) |
||
253 | |||
254 | public function addItemHolderPostValidator(ItemHolderPostValidator $itemHolderValidator) |
||
258 | |||
259 | public function addDiscountProcessor(DiscountProcessor $discountProcessor) |
||
263 | |||
264 | /** |
||
265 | * @param ItemHolderInterface $itemHolder |
||
266 | */ |
||
267 | View Code Duplication | protected function calculateTotal(ItemHolderInterface $itemHolder) |
|
281 | |||
282 | View Code Duplication | protected function calculateSubTotal(ItemHolderInterface $itemHolder) |
|
297 | |||
298 | /** |
||
299 | * @param ItemHolderInterface $itemHolder |
||
300 | */ |
||
301 | View Code Duplication | protected function calculateDeliveryFeeTotal(ItemHolderInterface $itemHolder) |
|
312 | |||
313 | /** |
||
314 | * @param ItemHolderInterface $itemHolder |
||
315 | */ |
||
316 | View Code Duplication | protected function calculateDiscount(ItemHolderInterface $itemHolder) |
|
328 | |||
329 | /** |
||
330 | * @param ItemHolderInterface $itemHolder |
||
331 | */ |
||
332 | View Code Duplication | protected function calculateCharge(ItemHolderInterface $itemHolder) |
|
343 | |||
344 | /** |
||
345 | * @param ItemHolderInterface $itemHolder |
||
346 | */ |
||
347 | protected function calculateTax(ItemHolderInterface $itemHolder) |
||
361 | |||
362 | /** |
||
363 | * @param ItemHolderInterface $itemHolder |
||
364 | */ |
||
365 | protected function calculateAll(ItemHolderInterface $itemHolder) |
||
374 | } |
||
375 |
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.