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 | 744 | public function __construct(EntityManagerInterface $entityManager, BaseInfo $BaseInfo) |
|
56 | |||
57 | /* |
||
58 | * ItemHolderPreprocessor |
||
59 | */ |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 211 | public function process(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
81 | |||
82 | /* |
||
83 | * ItemHolderValidator |
||
84 | */ |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 10 | protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
107 | |||
108 | /* |
||
109 | * PurchaseProcessor |
||
110 | */ |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 9 | View Code Duplication | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
127 | |||
128 | /** |
||
129 | * {@inheritdoc |
||
130 | */ |
||
131 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc |
||
138 | */ |
||
139 | 2 | View Code Duplication | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
149 | |||
150 | /* |
||
151 | * Helper methods |
||
152 | */ |
||
153 | |||
154 | /** |
||
155 | * Processorが実行出来るかどうかを返す. |
||
156 | * |
||
157 | * 以下を満たす場合に実行できる. |
||
158 | * |
||
159 | * - ポイント設定が有効であること. |
||
160 | * - $itemHolderがOrderエンティティであること. |
||
161 | * - 会員のOrderであること. |
||
162 | * |
||
163 | * @param ItemHolderInterface $itemHolder |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | 222 | private function supports(ItemHolderInterface $itemHolder) |
|
183 | |||
184 | /** |
||
185 | * 付与ポイントを計算. |
||
186 | * |
||
187 | * @param ItemHolderInterface $itemHolder |
||
188 | * |
||
189 | * @return int |
||
190 | */ |
||
191 | 194 | private function calculateAddPoint(ItemHolderInterface $itemHolder) |
|
214 | |||
215 | /** |
||
216 | * 明細追加処理. |
||
217 | * |
||
218 | * @param ItemHolderInterface $itemHolder |
||
219 | * @param integer $discount |
||
220 | */ |
||
221 | 1 | private function addPointDiscountItem(ItemHolderInterface $itemHolder, $discount) |
|
239 | |||
240 | /** |
||
241 | * 既存のポイント明細を削除する. |
||
242 | * |
||
243 | * @param ItemHolderInterface $itemHolder |
||
244 | */ |
||
245 | 1 | View Code Duplication | private function removePointDiscountItem(ItemHolderInterface $itemHolder) |
254 | |||
255 | /** |
||
256 | * ポイントを金額に変換する. |
||
257 | * |
||
258 | * @param integer $point int ポイント |
||
259 | * |
||
260 | * @return int 金額 |
||
261 | */ |
||
262 | 11 | private function pointToPrice($point) |
|
266 | } |
||
267 |
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.