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 |
||
26 | class OrderStateMachine implements EventSubscriberInterface |
||
|
|||
27 | { |
||
28 | /** |
||
29 | * @var StateMachine |
||
30 | */ |
||
31 | private $machine; |
||
32 | |||
33 | /** |
||
34 | * @var OrderStatusRepository |
||
35 | */ |
||
36 | private $orderStatusRepository; |
||
37 | |||
38 | /** |
||
39 | * @var PointProcessor |
||
40 | */ |
||
41 | private $pointProcessor; |
||
42 | /** |
||
43 | * @var StockReduceProcessor |
||
44 | */ |
||
45 | private $stockReduceProcessor; |
||
46 | |||
47 | 140 | public function __construct(StateMachine $_orderStateMachine, OrderStatusRepository $orderStatusRepository, PointProcessor $pointProcessor, StockReduceProcessor $stockReduceProcessor) |
|
54 | |||
55 | /** |
||
56 | * 指定ステータスに遷移. |
||
57 | * |
||
58 | * @param Order $Order 受注 |
||
59 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
60 | */ |
||
61 | 11 | public function apply(Order $Order, OrderStatus $OrderStatus) |
|
71 | |||
72 | /** |
||
73 | * 指定ステータスに遷移できるかどうかを判定. |
||
74 | * |
||
75 | * @param Order $Order 受注 |
||
76 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
77 | * |
||
78 | * @return boolean 指定ステータスに遷移できる場合はtrue |
||
79 | */ |
||
80 | 44 | public function can(Order $Order, OrderStatus $OrderStatus) |
|
84 | |||
85 | 50 | private function getTransition(OrderStateMachineContext $context, OrderStatus $OrderStatus) |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 1 | public static function getSubscribedEvents() |
|
112 | |||
113 | /* |
||
114 | * Event handlers. |
||
115 | */ |
||
116 | |||
117 | /** |
||
118 | * 入金日を更新する. |
||
119 | * |
||
120 | * @param Event $event |
||
121 | */ |
||
122 | 1 | public function updatePaymentDate(Event $event) |
|
128 | |||
129 | /** |
||
130 | * 会員の保有ポイントを減らす. |
||
131 | * |
||
132 | * @param Event $event |
||
133 | * |
||
134 | * @throws PurchaseFlow\PurchaseException |
||
135 | */ |
||
136 | 2 | public function commitUsePoint(Event $event) |
|
142 | |||
143 | /** |
||
144 | * 利用ポイントを会員に戻す. |
||
145 | * |
||
146 | * @param Event $event |
||
147 | */ |
||
148 | 2 | public function rollbackUsePoint(Event $event) |
|
154 | |||
155 | /** |
||
156 | * 在庫を減らす. |
||
157 | * |
||
158 | * @param Event $event |
||
159 | * |
||
160 | * @throws PurchaseFlow\PurchaseException |
||
161 | */ |
||
162 | 1 | public function commitStock(Event $event) |
|
168 | |||
169 | /** |
||
170 | * 在庫を戻す. |
||
171 | * |
||
172 | * @param Event $event |
||
173 | */ |
||
174 | 1 | public function rollbackStock(Event $event) |
|
180 | |||
181 | /** |
||
182 | * 会員に加算ポイントを付与する. |
||
183 | * |
||
184 | * @param Event $event |
||
185 | */ |
||
186 | 4 | View Code Duplication | public function commitAddPoint(Event $event) |
195 | |||
196 | /** |
||
197 | * 会員に付与した加算ポイントを取り消す. |
||
198 | * |
||
199 | * @param Event $event |
||
200 | */ |
||
201 | 1 | View Code Duplication | public function rollbackAddPoint(Event $event) |
210 | |||
211 | /** |
||
212 | * 受注ステータスを再設定. |
||
213 | * {@link StateMachine}によって遷移が終了したときには{@link Order#OrderStatus}のidが変更されるだけなのでOrderStatusを設定し直す. |
||
214 | * |
||
215 | * @param Event $event |
||
216 | */ |
||
217 | 11 | public function onCompleted(Event $event) |
|
225 | |||
226 | 50 | private function newContext(Order $Order) |
|
230 | } |
||
231 | |||
276 |