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 | 116 | 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 | 9 | public function apply(Order $Order, OrderStatus $OrderStatus) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * 指定ステータスに遷移できるかどうかを判定. |
||
| 73 | * |
||
| 74 | * @param Order $Order 受注 |
||
| 75 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
| 76 | * |
||
| 77 | * @return boolean 指定ステータスに遷移できる場合はtrue |
||
| 78 | */ |
||
| 79 | 40 | public function can(Order $Order, OrderStatus $OrderStatus) |
|
| 83 | |||
| 84 | 46 | private function getTransition(Order $Order, OrderStatus $OrderStatus) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | 1 | public static function getSubscribedEvents() |
|
| 111 | |||
| 112 | /* |
||
| 113 | * Event handlers. |
||
| 114 | */ |
||
| 115 | |||
| 116 | /** |
||
| 117 | * 入金日を更新する. |
||
| 118 | * |
||
| 119 | * @param Event $event |
||
| 120 | */ |
||
| 121 | 1 | public function updatePaymentDate(Event $event) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * 発送日を更新する. |
||
| 130 | * |
||
| 131 | * @param Event $event |
||
| 132 | */ |
||
| 133 | 1 | public function updateShippingDate(Event $event) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * 会員の保有ポイントを減らす. |
||
| 144 | * |
||
| 145 | * @param Event $event |
||
| 146 | * |
||
| 147 | * @throws PurchaseFlow\PurchaseException |
||
| 148 | */ |
||
| 149 | 2 | public function commitUsePoint(Event $event) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * 利用ポイントを会員に戻す. |
||
| 158 | * |
||
| 159 | * @param Event $event |
||
| 160 | */ |
||
| 161 | 2 | public function rollbackUsePoint(Event $event) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * 在庫を減らす. |
||
| 170 | * |
||
| 171 | * @param Event $event |
||
| 172 | * |
||
| 173 | * @throws PurchaseFlow\PurchaseException |
||
| 174 | */ |
||
| 175 | 1 | public function commitStock(Event $event) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * 在庫を戻す. |
||
| 184 | * |
||
| 185 | * @param Event $event |
||
| 186 | */ |
||
| 187 | 1 | public function rollbackStock(Event $event) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * 会員に加算ポイントを付与する. |
||
| 196 | * |
||
| 197 | * @param Event $event |
||
| 198 | */ |
||
| 199 | 2 | View Code Duplication | public function commitAddPoint(Event $event) |
| 208 | |||
| 209 | /** |
||
| 210 | * 会員に付与した加算ポイントを取り消す. |
||
| 211 | * |
||
| 212 | * @param Event $event |
||
| 213 | */ |
||
| 214 | 1 | View Code Duplication | public function rollbackAddPoint(Event $event) |
| 223 | |||
| 224 | /** |
||
| 225 | * 受注ステータスを再設定. |
||
| 226 | * {@link StateMachine}によって遷移が終了したときには{@link Order#OrderStatus}のidが変更されるだけなのでOrderStatusを設定し直す. |
||
| 227 | * |
||
| 228 | * @param Event $event |
||
| 229 | */ |
||
| 230 | 9 | public function onCompleted(Event $event) |
|
| 238 | } |
||
| 239 |