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 |
||
28 | class PointProcessor implements DiscountProcessor, PurchaseProcessor |
||
29 | { |
||
30 | /** |
||
31 | * @var EntityManagerInterface |
||
32 | */ |
||
33 | protected $entityManager; |
||
34 | |||
35 | /** |
||
36 | * @var PointHelper |
||
37 | */ |
||
38 | protected $pointHelper; |
||
39 | |||
40 | /** |
||
41 | * PointProcessor constructor. |
||
42 | * |
||
43 | * @param EntityManagerInterface $entityManager |
||
44 | * @param PointHelper $pointHelper |
||
45 | */ |
||
46 | public function __construct(EntityManagerInterface $entityManager, PointHelper $pointHelper) |
||
51 | |||
52 | 790 | /* |
|
53 | * DiscountProcessors |
||
54 | 790 | */ |
|
55 | 790 | ||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function removeDiscountItem(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
67 | 232 | ||
68 | 17 | /** |
|
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 215 | public function addDiscountItem(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
72 | { |
||
73 | if (!$this->supports($itemHolder)) { |
||
74 | 215 | return; |
|
75 | 17 | } |
|
76 | 17 | ||
77 | $usePoint = $itemHolder->getUsePoint(); |
||
78 | $discount = $this->pointHelper->pointToDiscount($usePoint); |
||
79 | |||
80 | 215 | // 利用ポイントがある場合は割引明細を追加 |
|
81 | 215 | if ($usePoint > 0) { |
|
82 | $result = null; |
||
83 | |||
84 | // 購入フロー実行時 |
||
85 | if ($context->isShoppingFlow()) { |
||
86 | // 支払い金額 < 利用ポイントによる値引き額. |
||
87 | if ($itemHolder->getTotal() + $discount < 0) { |
||
88 | $minus = $itemHolder->getTotal() + $discount; |
||
89 | // 利用ポイントが支払い金額を上回っていた場合は支払い金額が0円以上となるようにポイントを調整 |
||
90 | $overPoint = $this->pointHelper->priceToPoint($minus); |
||
91 | 61 | $usePoint = $itemHolder->getUsePoint() + $overPoint; |
|
92 | $discount = $this->pointHelper->pointToDiscount($usePoint); |
||
93 | 61 | $result = ProcessResult::warn(trans('purchase_flow.over_payment_total'), self::class); |
|
94 | 17 | } |
|
95 | |||
96 | // 所有ポイント < 利用ポイント |
||
97 | $Customer = $itemHolder->getCustomer(); |
||
98 | 44 | if ($Customer->getPoint() < $usePoint) { |
|
99 | 44 | // 利用ポイントが所有ポイントを上回っていた場合は所有ポイントで上書き |
|
100 | $usePoint = $Customer->getPoint(); |
||
101 | 2 | $discount = $this->pointHelper->pointToDiscount($usePoint); |
|
102 | 2 | $result = ProcessResult::warn(trans('purchase_flow.over_customer_point'), self::class); |
|
103 | } |
||
104 | // 受注登録・編集実行時 |
||
105 | } else { |
||
106 | 42 | // 支払い金額 < 利用ポイントによる値引き額. |
|
107 | if ($itemHolder->getTotal() >= 0 && $itemHolder->getTotal() + $discount < 0) { |
||
108 | 1 | $result = ProcessResult::error(trans('purchase_flow.over_payment_total'), self::class); |
|
109 | 1 | } |
|
110 | 1 | } |
|
111 | |||
112 | $itemHolder->setUsePoint($usePoint); |
||
113 | $this->pointHelper->addPointDiscountItem($itemHolder, $discount); |
||
114 | |||
115 | if ($result) { |
||
116 | return $result; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | 12 | /* |
|
122 | * PurchaseProcessor |
||
123 | 12 | */ |
|
124 | 3 | ||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 9 | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc |
||
140 | */ |
||
141 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
145 | |||
146 | 2 | /** |
|
147 | * {@inheritdoc |
||
148 | */ |
||
149 | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
158 | |||
159 | /* |
||
160 | * Helper methods |
||
161 | */ |
||
162 | |||
163 | /** |
||
164 | * Processorが実行出来るかどうかを返す. |
||
165 | * |
||
166 | * 以下を満たす場合に実行できる. |
||
167 | * |
||
168 | * - ポイント設定が有効であること. |
||
169 | * - $itemHolderがOrderエンティティであること. |
||
170 | * - 会員のOrderであること. |
||
171 | 239 | * |
|
172 | * @param ItemHolderInterface $itemHolder |
||
173 | 239 | * |
|
174 | * @return bool |
||
175 | */ |
||
176 | View Code Duplication | private function supports(ItemHolderInterface $itemHolder) |
|
192 | } |
||
193 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.