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 OrderStateMachine implements EventSubscriberInterface |
||
|
|||
30 | { |
||
31 | /** |
||
32 | * @var StateMachine |
||
33 | */ |
||
34 | private $machine; |
||
35 | |||
36 | /** |
||
37 | * @var OrderStatusRepository |
||
38 | */ |
||
39 | private $orderStatusRepository; |
||
40 | |||
41 | /** |
||
42 | * @var PointProcessor |
||
43 | */ |
||
44 | private $pointProcessor; |
||
45 | /** |
||
46 | * @var StockReduceProcessor |
||
47 | */ |
||
48 | private $stockReduceProcessor; |
||
49 | |||
50 | /** |
||
51 | * @var EntityManagerInterface |
||
52 | */ |
||
53 | private $entityManager; |
||
54 | |||
55 | 108 | public function __construct(StateMachine $_orderStateMachine, OrderStatusRepository $orderStatusRepository, PointProcessor $pointProcessor, StockReduceProcessor $stockReduceProcessor, EntityManagerInterface $entityManager) |
|
63 | |||
64 | /** |
||
65 | * 指定ステータスに遷移. |
||
66 | * |
||
67 | * @param Order $Order 受注 |
||
68 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
69 | */ |
||
70 | 10 | public function apply(Order $Order, OrderStatus $OrderStatus) |
|
79 | |||
80 | /** |
||
81 | * 指定ステータスに遷移できるかどうかを判定. |
||
82 | * |
||
83 | * @param Order $Order 受注 |
||
84 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
85 | * |
||
86 | * @return boolean 指定ステータスに遷移できる場合はtrue |
||
87 | */ |
||
88 | 43 | public function can(Order $Order, OrderStatus $OrderStatus) |
|
92 | |||
93 | 49 | private function getTransition(Order $Order, OrderStatus $OrderStatus) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 1 | public static function getSubscribedEvents() |
|
121 | |||
122 | /* |
||
123 | * Event handlers. |
||
124 | */ |
||
125 | |||
126 | /** |
||
127 | * 購入日を更新する. |
||
128 | * |
||
129 | * @param Event $event |
||
130 | */ |
||
131 | 2 | public function updatePaymentDate(Event $event) |
|
137 | |||
138 | /** |
||
139 | * 会員の保有ポイントを減らす. |
||
140 | * |
||
141 | * @param Event $event |
||
142 | * |
||
143 | * @throws PurchaseFlow\PurchaseException |
||
144 | */ |
||
145 | 2 | public function commitUsePoint(Event $event) |
|
151 | |||
152 | /** |
||
153 | * 利用ポイントを会員に戻す. |
||
154 | * |
||
155 | * @param Event $event |
||
156 | */ |
||
157 | 2 | public function rollbackUsePoint(Event $event) |
|
163 | |||
164 | /** |
||
165 | * 在庫を減らす. |
||
166 | * |
||
167 | * @param Event $event |
||
168 | * |
||
169 | * @throws PurchaseFlow\PurchaseException |
||
170 | */ |
||
171 | 1 | public function commitStock(Event $event) |
|
177 | |||
178 | /** |
||
179 | * 在庫を戻す. |
||
180 | * |
||
181 | * @param Event $event |
||
182 | */ |
||
183 | 1 | public function rollbackStock(Event $event) |
|
189 | |||
190 | /** |
||
191 | * 会員に加算ポイントを付与する. |
||
192 | * |
||
193 | * @param Event $event |
||
194 | */ |
||
195 | 5 | View Code Duplication | public function commitAddPoint(Event $event) |
204 | |||
205 | /** |
||
206 | * 会員に付与した加算ポイントを取り消す. |
||
207 | * |
||
208 | * @param Event $event |
||
209 | */ |
||
210 | 1 | View Code Duplication | public function rollbackAddPoint(Event $event) |
219 | |||
220 | /** |
||
221 | * 受注ステータスを再設定. |
||
222 | * {@link StateMachine}によって遷移が終了したときには{@link Order#OrderStatus}のidが変更されるだけなのでOrderStatusを設定し直す. |
||
223 | * |
||
224 | * @param Event $event |
||
225 | */ |
||
226 | 10 | public function onCompleted(Event $event) |
|
239 | |||
240 | /** |
||
241 | * すべての出荷が発送済みなら、受注も発送済みに遷移できる. |
||
242 | * |
||
243 | * @param GuardEvent $event |
||
244 | */ |
||
245 | 27 | public function guardShip(GuardEvent $event) |
|
256 | } |
||
257 |