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 |
||
29 | class PointDiffProcessor extends ItemHolderValidator implements PurchaseProcessor |
||
30 | { |
||
31 | /** |
||
32 | * @var EntityManagerInterface |
||
33 | */ |
||
34 | protected $entityManager; |
||
35 | |||
36 | /** |
||
37 | * @var BaseInfo |
||
38 | */ |
||
39 | protected $BaseInfo; |
||
40 | |||
41 | /** |
||
42 | * AddPointProcessor constructor. |
||
43 | * |
||
44 | * @param EntityManagerInterface $entityManager |
||
45 | * @param BaseInfoRepository $baseInfoRepository |
||
46 | */ |
||
47 | public function __construct(EntityManagerInterface $entityManager, BaseInfoRepository $baseInfoRepository) |
||
52 | |||
53 | /* |
||
54 | * ItemHolderValidator |
||
55 | */ |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
79 | |||
80 | /* |
||
81 | * PurchaseProcessor |
||
82 | */ |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | View Code Duplication | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc |
||
102 | */ |
||
103 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc |
||
110 | */ |
||
111 | View Code Duplication | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
122 | |||
123 | /* |
||
124 | * Helper methods |
||
125 | */ |
||
126 | |||
127 | /** |
||
128 | * Processorが実行出来るかどうかを返す. |
||
129 | * |
||
130 | * 以下を満たす場合に実行できる. |
||
131 | * |
||
132 | * - ポイント設定が有効であること. |
||
133 | * - $itemHolderがOrderエンティティであること. |
||
134 | * - OrderStatusが新規受付、入金済み、対応中、発送済みのどれかであること |
||
135 | * - 会員のOrderであること. |
||
136 | * - PurchaseContextでOriginHolderが渡ってきている |
||
137 | * |
||
138 | * @param ItemHolderInterface $itemHolder |
||
139 | * @param PurchaseContext $context |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | private function supports(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
173 | |||
174 | /** |
||
175 | * 利用ポイントの差を計算する |
||
176 | * この差が新規利用ポイントとなる |
||
177 | * |
||
178 | * 使用ポイントが増えた場合プラスとなる |
||
179 | * 50 -> 100 : 50 |
||
180 | * 100 -> 50 : -50 |
||
181 | * |
||
182 | * @param ItemHolderInterface $itemHolder |
||
183 | * @param PurchaseContext $context |
||
184 | * |
||
185 | * @return int |
||
186 | */ |
||
187 | protected function getDiffOfUsePoint(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
198 | } |
||
199 |
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.