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 | 3 | public function updateShippingDate(Event $event) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * 会員の保有ポイントを減らす. |
||
| 148 | * |
||
| 149 | * @param Event $event |
||
| 150 | * |
||
| 151 | * @throws PurchaseFlow\PurchaseException |
||
| 152 | */ |
||
| 153 | 2 | public function commitUsePoint(Event $event) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * 利用ポイントを会員に戻す. |
||
| 162 | * |
||
| 163 | * @param Event $event |
||
| 164 | */ |
||
| 165 | 2 | public function rollbackUsePoint(Event $event) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * 在庫を減らす. |
||
| 174 | * |
||
| 175 | * @param Event $event |
||
| 176 | * |
||
| 177 | * @throws PurchaseFlow\PurchaseException |
||
| 178 | */ |
||
| 179 | 1 | public function commitStock(Event $event) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * 在庫を戻す. |
||
| 188 | * |
||
| 189 | * @param Event $event |
||
| 190 | */ |
||
| 191 | 1 | public function rollbackStock(Event $event) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * 会員に加算ポイントを付与する. |
||
| 200 | * |
||
| 201 | * @param Event $event |
||
| 202 | */ |
||
| 203 | 4 | View Code Duplication | public function commitAddPoint(Event $event) |
| 212 | |||
| 213 | /** |
||
| 214 | * 会員に付与した加算ポイントを取り消す. |
||
| 215 | * |
||
| 216 | * @param Event $event |
||
| 217 | */ |
||
| 218 | 1 | View Code Duplication | public function rollbackAddPoint(Event $event) |
| 227 | |||
| 228 | /** |
||
| 229 | * 受注ステータスを再設定. |
||
| 230 | * {@link StateMachine}によって遷移が終了したときには{@link Order#OrderStatus}のidが変更されるだけなのでOrderStatusを設定し直す. |
||
| 231 | * |
||
| 232 | * @param Event $event |
||
| 233 | */ |
||
| 234 | 11 | public function onCompleted(Event $event) |
|
| 242 | |||
| 243 | 50 | private function newContext(Order $Order) |
|
| 247 | } |
||
| 248 | |||
| 293 |