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 |
||
| 33 | class PointProcessor extends ItemHolderValidator implements ItemHolderPreprocessor, PurchaseProcessor |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var EntityManagerInterface |
||
| 37 | */ |
||
| 38 | protected $entityManager; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var BaseInfo |
||
| 42 | */ |
||
| 43 | protected $BaseInfo; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * AddPointProcessor constructor. |
||
| 47 | * |
||
| 48 | * @param EntityManagerInterface $entityManager |
||
| 49 | * @param BaseInfo $BaseInfo |
||
|
|
|||
| 50 | */ |
||
| 51 | 653 | public function __construct(EntityManagerInterface $entityManager, BaseInfo $BaseInfo) |
|
| 56 | |||
| 57 | /* |
||
| 58 | * ItemHolderPreprocessor |
||
| 59 | */ |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 186 | public function process(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 77 | |||
| 78 | /* |
||
| 79 | * ItemHolderValidator |
||
| 80 | */ |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | 10 | protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 102 | |||
| 103 | /* |
||
| 104 | * PurchaseProcessor |
||
| 105 | */ |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 7 | View Code Duplication | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc |
||
| 124 | */ |
||
| 125 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 142 | |||
| 143 | /* |
||
| 144 | * Helper methods |
||
| 145 | */ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * 付与ポイントを計算. |
||
| 149 | * |
||
| 150 | * @param ItemHolderInterface $itemHolder |
||
| 151 | * |
||
| 152 | * @return int |
||
| 153 | */ |
||
| 154 | 186 | private function calculateAddPoint(ItemHolderInterface $itemHolder) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * 明細追加処理. |
||
| 180 | * |
||
| 181 | * @param ItemHolderInterface $itemHolder |
||
| 182 | * @param $discount |
||
| 183 | */ |
||
| 184 | 1 | private function addPointDiscountItem(ItemHolderInterface $itemHolder, $discount) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * 既存のポイント明細を削除する. |
||
| 205 | * |
||
| 206 | * @param ItemHolderInterface $itemHolder |
||
| 207 | */ |
||
| 208 | 1 | private function removePointDiscountItem(ItemHolderInterface $itemHolder) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * ポイントを金額に変換する. |
||
| 220 | * |
||
| 221 | * @param $point int ポイント |
||
| 222 | * |
||
| 223 | * @return int 金額 |
||
| 224 | */ |
||
| 225 | 11 | private function pointToPrice($point) |
|
| 229 | } |
||
| 230 |