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 | 787 | public function __construct(EntityManagerInterface $entityManager, BaseInfo $BaseInfo) |
|
56 | |||
57 | /* |
||
58 | * ItemHolderPreprocessor |
||
59 | */ |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 230 | public function process(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
82 | |||
83 | /* |
||
84 | * ItemHolderValidator |
||
85 | */ |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 61 | protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
112 | |||
113 | /* |
||
114 | * PurchaseProcessor |
||
115 | */ |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 12 | View Code Duplication | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
130 | |||
131 | /** |
||
132 | * {@inheritdoc |
||
133 | */ |
||
134 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc |
||
141 | */ |
||
142 | 2 | View Code Duplication | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
152 | |||
153 | /* |
||
154 | * Helper methods |
||
155 | */ |
||
156 | |||
157 | /** |
||
158 | * Processorが実行出来るかどうかを返す. |
||
159 | * |
||
160 | * 以下を満たす場合に実行できる. |
||
161 | * |
||
162 | * - ポイント設定が有効であること. |
||
163 | * - $itemHolderがOrderエンティティであること. |
||
164 | * - 会員のOrderであること. |
||
165 | * |
||
166 | * @param ItemHolderInterface $itemHolder |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | 237 | private function supports(ItemHolderInterface $itemHolder) |
|
186 | |||
187 | /** |
||
188 | * 付与ポイントを計算. |
||
189 | * |
||
190 | * @param ItemHolderInterface $itemHolder |
||
191 | * |
||
192 | * @return int |
||
193 | */ |
||
194 | 213 | private function calculateAddPoint(ItemHolderInterface $itemHolder) |
|
218 | |||
219 | /** |
||
220 | * 明細追加処理. |
||
221 | * |
||
222 | * @param ItemHolderInterface $itemHolder |
||
223 | * @param $discount |
||
224 | */ |
||
225 | 17 | private function addPointDiscountItem(ItemHolderInterface $itemHolder, $discount) |
|
246 | |||
247 | /** |
||
248 | * 既存のポイント明細を削除する. |
||
249 | * |
||
250 | * @param ItemHolderInterface $itemHolder |
||
251 | */ |
||
252 | 213 | private function removePointDiscountItem(ItemHolderInterface $itemHolder) |
|
261 | |||
262 | /** |
||
263 | * ポイントを金額に変換する. |
||
264 | * |
||
265 | * @param $point int ポイント |
||
266 | * |
||
267 | * @return int 金額 |
||
268 | */ |
||
269 | 17 | private function pointToPrice($point) |
|
273 | } |
||
274 |