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 |
||
| 22 | class PurchaseFlow |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var ArrayCollection|ItemPreprocessor[] |
||
| 26 | */ |
||
| 27 | protected $itemPreprocessors; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ArrayCollection|ItemHolderPreprocessor[] |
||
| 31 | */ |
||
| 32 | protected $itemHolderPreprocessors; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ArrayCollection|ItemValidator[] |
||
| 36 | */ |
||
| 37 | protected $itemValidators; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ArrayCollection|ItemHolderValidator[] |
||
| 41 | */ |
||
| 42 | protected $itemHolderValidators; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ArrayCollection|PurchaseProcessor[] |
||
| 46 | */ |
||
| 47 | protected $purchaseProcessors; |
||
| 48 | |||
| 49 | 764 | public function __construct() |
|
| 57 | |||
| 58 | 725 | public function setPurchaseProcessors(ArrayCollection $processors) |
|
| 62 | |||
| 63 | 758 | public function setItemValidators(ArrayCollection $itemValidators) |
|
| 67 | |||
| 68 | 758 | public function setItemHolderValidators(ArrayCollection $itemHolderValidators) |
|
| 72 | |||
| 73 | public function setItemPreprocessors(ArrayCollection $itemPreprocessors) |
||
| 77 | |||
| 78 | 758 | public function setItemHolderPreprocessors(ArrayCollection $itemHolderPreprocessors) |
|
| 82 | |||
| 83 | 293 | public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * 購入フロー仮確定処理. |
||
| 124 | * |
||
| 125 | * @param ItemHolderInterface $target |
||
| 126 | * @param PurchaseContext $context |
||
| 127 | * |
||
| 128 | * @throws PurchaseException |
||
| 129 | */ |
||
| 130 | 34 | public function prepare(ItemHolderInterface $target, PurchaseContext $context) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * 購入フロー確定処理. |
||
| 139 | * |
||
| 140 | * @param ItemHolderInterface $target |
||
| 141 | * @param PurchaseContext $context |
||
| 142 | * |
||
| 143 | * @throws PurchaseException |
||
| 144 | */ |
||
| 145 | 26 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * 購入フロー仮確定取り消し処理. |
||
| 154 | * |
||
| 155 | * @param ItemHolderInterface $target |
||
| 156 | * @param PurchaseContext $context |
||
| 157 | */ |
||
| 158 | public function rollback(ItemHolderInterface $target, PurchaseContext $context) |
||
| 164 | |||
| 165 | 16 | public function addPurchaseProcessor(PurchaseProcessor $processor) |
|
| 169 | |||
| 170 | 26 | public function addItemHolderPreprocessor(ItemHolderPreprocessor $holderPreprocessor) |
|
| 174 | |||
| 175 | 2 | public function addItemPreprocessor(ItemPreprocessor $itemPreprocessor) |
|
| 179 | |||
| 180 | 3 | public function addItemValidator(ItemValidator $itemValidator) |
|
| 184 | |||
| 185 | 36 | public function addItemHolderValidator(ItemHolderValidator $itemHolderValidator) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param ItemHolderInterface $itemHolder |
||
| 192 | */ |
||
| 193 | protected function calculateTotal(ItemHolderInterface $itemHolder) |
||
| 207 | |||
| 208 | 293 | View Code Duplication | protected function calculateSubTotal(ItemHolderInterface $itemHolder) |
| 223 | |||
| 224 | /** |
||
| 225 | * @param ItemHolderInterface $itemHolder |
||
| 226 | */ |
||
| 227 | 293 | View Code Duplication | protected function calculateDeliveryFeeTotal(ItemHolderInterface $itemHolder) |
| 238 | |||
| 239 | /** |
||
| 240 | * @param ItemHolderInterface $itemHolder |
||
| 241 | */ |
||
| 242 | 293 | View Code Duplication | protected function calculateDiscount(ItemHolderInterface $itemHolder) |
| 254 | |||
| 255 | /** |
||
| 256 | * @param ItemHolderInterface $itemHolder |
||
| 257 | */ |
||
| 258 | 293 | View Code Duplication | protected function calculateCharge(ItemHolderInterface $itemHolder) |
| 269 | |||
| 270 | /** |
||
| 271 | * @param ItemHolderInterface $itemHolder |
||
| 272 | */ |
||
| 273 | 293 | protected function calculateTax(ItemHolderInterface $itemHolder) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param ItemHolderInterface $itemHolder |
||
| 290 | */ |
||
| 291 | 293 | protected function calculateAll(ItemHolderInterface $itemHolder) |
|
| 300 | } |
||
| 301 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.