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 | 783 | public function __construct(EntityManagerInterface $entityManager, BaseInfo $BaseInfo) |
|
| 56 | |||
| 57 | /* |
||
| 58 | * ItemHolderPreprocessor |
||
| 59 | */ |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 225 | public function process(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 82 | |||
| 83 | /* |
||
| 84 | * ItemHolderValidator |
||
| 85 | */ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | * |
||
| 90 | * TODO: handle関数に処理を分けた方がいいか |
||
| 91 | */ |
||
| 92 | 61 | protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
| 114 | |||
| 115 | /* |
||
| 116 | * PurchaseProcessor |
||
| 117 | */ |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | 9 | View Code Duplication | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc |
||
| 135 | */ |
||
| 136 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc |
||
| 143 | */ |
||
| 144 | 2 | View Code Duplication | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
| 154 | |||
| 155 | /* |
||
| 156 | * Helper methods |
||
| 157 | */ |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Processorが実行出来るかどうかを返す. |
||
| 161 | * |
||
| 162 | * 以下を満たす場合に実行できる. |
||
| 163 | * |
||
| 164 | * - ポイント設定が有効であること. |
||
| 165 | * - $itemHolderがOrderエンティティであること. |
||
| 166 | * - 会員のOrderであること. |
||
| 167 | * |
||
| 168 | * @param ItemHolderInterface $itemHolder |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | 232 | private function supports(ItemHolderInterface $itemHolder) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * 付与ポイントを計算. |
||
| 191 | * |
||
| 192 | * @param ItemHolderInterface $itemHolder |
||
| 193 | * |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | 208 | private function calculateAddPoint(ItemHolderInterface $itemHolder) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * 明細追加処理. |
||
| 223 | * |
||
| 224 | * @param ItemHolderInterface $itemHolder |
||
| 225 | * @param $discount |
||
| 226 | */ |
||
| 227 | 14 | private function addPointDiscountItem(ItemHolderInterface $itemHolder, $discount) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * 既存のポイント明細を削除する. |
||
| 248 | * |
||
| 249 | * @param ItemHolderInterface $itemHolder |
||
| 250 | */ |
||
| 251 | 208 | private function removePointDiscountItem(ItemHolderInterface $itemHolder) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * ポイントを金額に変換する. |
||
| 263 | * |
||
| 264 | * @param $point int ポイント |
||
| 265 | * |
||
| 266 | * @return int 金額 |
||
| 267 | */ |
||
| 268 | 14 | private function pointToPrice($point) |
|
| 272 | } |
||
| 273 |