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 | /* |
||
53 | * DiscountProcessors |
||
54 | */ |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function removeDiscountItem(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function addDiscountItem(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
120 | |||
121 | /* |
||
122 | * PurchaseProcessor |
||
123 | */ |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc |
||
140 | */ |
||
141 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
145 | |||
146 | /** |
||
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 | * |
||
172 | * @param ItemHolderInterface $itemHolder |
||
173 | * |
||
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.