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 OrderStateMachine implements EventSubscriberInterface |
||
|
|
|||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var StateMachine |
||
| 32 | */ |
||
| 33 | private $machine; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var OrderStatusRepository |
||
| 37 | */ |
||
| 38 | private $orderStatusRepository; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var PointProcessor |
||
| 42 | */ |
||
| 43 | private $pointProcessor; |
||
| 44 | /** |
||
| 45 | * @var StockReduceProcessor |
||
| 46 | */ |
||
| 47 | private $stockReduceProcessor; |
||
| 48 | |||
| 49 | 86 | public function __construct(StateMachine $machine, OrderStatusRepository $orderStatusRepository, PointProcessor $pointProcessor, StockReduceProcessor $stockReduceProcessor) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * 指定ステータスに遷移. |
||
| 59 | * |
||
| 60 | * @param Order $Order 受注 |
||
| 61 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
| 62 | */ |
||
| 63 | 6 | public function apply(Order $Order, OrderStatus $OrderStatus) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * 指定ステータスに遷移できるかどうかを判定. |
||
| 75 | * |
||
| 76 | * @param Order $Order 受注 |
||
| 77 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
| 78 | * |
||
| 79 | * @return boolean 指定ステータスに遷移できる場合はtrue |
||
| 80 | */ |
||
| 81 | 37 | public function can(Order $Order, OrderStatus $OrderStatus) |
|
| 85 | |||
| 86 | 43 | private function getTransition(Order $Order, OrderStatus $OrderStatus) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 1 | public static function getSubscribedEvents() |
|
| 114 | |||
| 115 | /* |
||
| 116 | * Event handlers. |
||
| 117 | */ |
||
| 118 | |||
| 119 | /** |
||
| 120 | * 購入日を更新する. |
||
| 121 | * |
||
| 122 | * @param Event $event |
||
| 123 | */ |
||
| 124 | 1 | public function updatePaymentDate(Event $event) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * 会員の保有ポイントを減らす. |
||
| 133 | * |
||
| 134 | * @param Event $event |
||
| 135 | * |
||
| 136 | * @throws PurchaseFlow\PurchaseException |
||
| 137 | */ |
||
| 138 | 2 | public function commitUsePoint(Event $event) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * 利用ポイントを会員に戻す. |
||
| 147 | * |
||
| 148 | * @param Event $event |
||
| 149 | */ |
||
| 150 | 2 | public function rollbackUsePoint(Event $event) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * 在庫を減らす. |
||
| 159 | * |
||
| 160 | * @param Event $event |
||
| 161 | * |
||
| 162 | * @throws PurchaseFlow\PurchaseException |
||
| 163 | */ |
||
| 164 | 1 | public function commitStock(Event $event) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * 在庫を戻す. |
||
| 173 | * |
||
| 174 | * @param Event $event |
||
| 175 | */ |
||
| 176 | 1 | public function rollbackStock(Event $event) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * 会員に加算ポイントを付与する. |
||
| 185 | * |
||
| 186 | * @param Event $event |
||
| 187 | */ |
||
| 188 | 2 | 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) |
| 208 | |||
| 209 | /** |
||
| 210 | * 受注ステータスを再設定. |
||
| 211 | * {@link StateMachine}によって遷移が終了したときには{@link Order#OrderStatus}のidが変更されるだけなのでOrderStatusを設定し直す. |
||
| 212 | * |
||
| 213 | * @param Event $event |
||
| 214 | */ |
||
| 215 | 6 | public function onCompleted(Event $event) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * すべての出荷が発送済みなら、受注も発送済みに遷移できる. |
||
| 226 | * |
||
| 227 | * @param GuardEvent $event |
||
| 228 | */ |
||
| 229 | 23 | public function guardShip(GuardEvent $event) |
|
| 240 | } |
||
| 241 |