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 |
||
9 | class PurchaseFlow |
||
|
|||
10 | { |
||
11 | /** |
||
12 | * @var ArrayCollection|ItemHolderProcessor[] |
||
13 | */ |
||
14 | protected $itemHolderProcessors; |
||
15 | |||
16 | /** |
||
17 | * @var ArrayCollection|ItemProcessor[] |
||
18 | */ |
||
19 | protected $itemProcessors; |
||
20 | |||
21 | /** |
||
22 | * @var ArrayCollection|PurchaseProcessor[] |
||
23 | */ |
||
24 | protected $purchaseProcessors; |
||
25 | |||
26 | public function __construct() |
||
32 | |||
33 | public function setItemProcessors(ArrayCollection $processors) |
||
37 | |||
38 | public function setItemHolderProcessors(ArrayCollection $processors) |
||
42 | |||
43 | public function setPurchaseProcessors(ArrayCollection $processors) |
||
47 | |||
48 | public function calculate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
73 | |||
74 | /** |
||
75 | * @param ItemHolderInterface $target |
||
76 | * @param PurchaseContext $context |
||
77 | * |
||
78 | * @throws PurchaseException |
||
79 | */ |
||
80 | public function purchase(ItemHolderInterface $target, PurchaseContext $context) |
||
86 | |||
87 | public function addPurchaseProcessor(PurchaseProcessor $processor) |
||
91 | |||
92 | public function addItemHolderProcessor(ItemHolderProcessor $prosessor) |
||
96 | |||
97 | public function addItemProcessor(ItemProcessor $prosessor) |
||
101 | |||
102 | /** |
||
103 | * @param ItemHolderInterface $itemHolder |
||
104 | */ |
||
105 | protected function calculateTotal(ItemHolderInterface $itemHolder) |
||
119 | |||
120 | View Code Duplication | protected function calculateSubTotal(ItemHolderInterface $itemHolder) |
|
135 | |||
136 | /** |
||
137 | * @param ItemHolderInterface $itemHolder |
||
138 | */ |
||
139 | View Code Duplication | protected function calculateDeliveryFeeTotal(ItemHolderInterface $itemHolder) |
|
150 | |||
151 | /** |
||
152 | * @param ItemHolderInterface $itemHolder |
||
153 | */ |
||
154 | View Code Duplication | protected function calculateDiscount(ItemHolderInterface $itemHolder) |
|
166 | |||
167 | /** |
||
168 | * @param ItemHolderInterface $itemHolder |
||
169 | */ |
||
170 | View Code Duplication | protected function calculateCharge(ItemHolderInterface $itemHolder) |
|
181 | |||
182 | /** |
||
183 | * @param ItemHolderInterface $itemHolder |
||
184 | */ |
||
185 | View Code Duplication | protected function calculateTax(ItemHolderInterface $itemHolder) |
|
195 | } |
||
196 |