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 |
||
| 21 | class PurchaseFlow |
||
|
|
|||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var ArrayCollection|ItemPreprocessor[] |
||
| 25 | */ |
||
| 26 | protected $itemPreprocessors; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var ArrayCollection|ItemHolderPreprocessor[] |
||
| 30 | */ |
||
| 31 | protected $itemHolderPreprocessors; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ArrayCollection|ItemValidator[] |
||
| 35 | */ |
||
| 36 | protected $itemValidators; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ArrayCollection|ItemHolderValidator[] |
||
| 40 | */ |
||
| 41 | protected $itemHolderValidators; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ArrayCollection|PurchaseProcessor[] |
||
| 45 | */ |
||
| 46 | protected $purchaseProcessors; |
||
| 47 | |||
| 48 | 691 | public function __construct() |
|
| 56 | |||
| 57 | 652 | public function setPurchaseProcessors(ArrayCollection $processors) |
|
| 61 | |||
| 62 | 685 | public function setItemValidators(ArrayCollection $itemValidators) |
|
| 66 | |||
| 67 | 685 | public function setItemHolderValidators(ArrayCollection $itemHolderValidators) |
|
| 71 | |||
| 72 | public function setItemPreprocessors(ArrayCollection $itemPreprocessors) |
||
| 76 | |||
| 77 | 685 | public function setItemHolderPreprocessors(ArrayCollection $itemHolderPreprocessors) |
|
| 81 | |||
| 82 | 228 | public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * 購入フロー仮確定処理. |
||
| 123 | * |
||
| 124 | * @param ItemHolderInterface $target |
||
| 125 | * @param PurchaseContext $context |
||
| 126 | * |
||
| 127 | * @throws PurchaseException |
||
| 128 | */ |
||
| 129 | 12 | public function prepare(ItemHolderInterface $target, PurchaseContext $context) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * 購入フロー確定処理. |
||
| 138 | * |
||
| 139 | * @param ItemHolderInterface $target |
||
| 140 | * @param PurchaseContext $context |
||
| 141 | * |
||
| 142 | * @throws PurchaseException |
||
| 143 | */ |
||
| 144 | 14 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * 購入フロー仮確定取り消し処理. |
||
| 153 | * |
||
| 154 | * @param ItemHolderInterface $target |
||
| 155 | * @param PurchaseContext $context |
||
| 156 | */ |
||
| 157 | public function rollback(ItemHolderInterface $target, PurchaseContext $context) |
||
| 163 | |||
| 164 | 1 | public function addPurchaseProcessor(PurchaseProcessor $processor) |
|
| 168 | |||
| 169 | 1 | public function addItemHolderPreprocessor(ItemHolderPreprocessor $holderPreprocessor) |
|
| 173 | |||
| 174 | 1 | public function addItemPreprocessor(ItemPreprocessor $itemPreprocessor) |
|
| 178 | |||
| 179 | 2 | public function addItemValidator(ItemValidator $itemValidator) |
|
| 183 | |||
| 184 | 11 | public function addItemHolderValidator(ItemHolderValidator $itemHolderValidator) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param ItemHolderInterface $itemHolder |
||
| 191 | */ |
||
| 192 | protected function calculateTotal(ItemHolderInterface $itemHolder) |
||
| 206 | |||
| 207 | 228 | View Code Duplication | protected function calculateSubTotal(ItemHolderInterface $itemHolder) |
| 222 | |||
| 223 | /** |
||
| 224 | * @param ItemHolderInterface $itemHolder |
||
| 225 | */ |
||
| 226 | 228 | View Code Duplication | protected function calculateDeliveryFeeTotal(ItemHolderInterface $itemHolder) |
| 237 | |||
| 238 | /** |
||
| 239 | * @param ItemHolderInterface $itemHolder |
||
| 240 | */ |
||
| 241 | 228 | View Code Duplication | protected function calculateDiscount(ItemHolderInterface $itemHolder) |
| 253 | |||
| 254 | /** |
||
| 255 | * @param ItemHolderInterface $itemHolder |
||
| 256 | */ |
||
| 257 | 228 | View Code Duplication | protected function calculateCharge(ItemHolderInterface $itemHolder) |
| 268 | |||
| 269 | /** |
||
| 270 | * @param ItemHolderInterface $itemHolder |
||
| 271 | */ |
||
| 272 | 228 | View Code Duplication | protected function calculateTax(ItemHolderInterface $itemHolder) |
| 282 | |||
| 283 | /** |
||
| 284 | * @param ItemHolderInterface $itemHolder |
||
| 285 | */ |
||
| 286 | 228 | protected function calculateAll(ItemHolderInterface $itemHolder) |
|
| 295 | } |
||
| 296 |