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 |
||
| 27 | class OrderStateMachine implements EventSubscriberInterface |
||
|
|
|||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var StateMachine |
||
| 31 | */ |
||
| 32 | private $machine; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var OrderStatusRepository |
||
| 36 | */ |
||
| 37 | private $orderStatusRepository; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var PointProcessor |
||
| 41 | */ |
||
| 42 | private $pointProcessor; |
||
| 43 | /** |
||
| 44 | * @var StockReduceProcessor |
||
| 45 | */ |
||
| 46 | private $stockReduceProcessor; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var EntityManagerInterface |
||
| 50 | */ |
||
| 51 | private $entityManager; |
||
| 52 | |||
| 53 | 124 | public function __construct(StateMachine $_orderStateMachine, OrderStatusRepository $orderStatusRepository, PointProcessor $pointProcessor, StockReduceProcessor $stockReduceProcessor, EntityManagerInterface $entityManager) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * 指定ステータスに遷移. |
||
| 64 | * |
||
| 65 | * @param Order $Order 受注 |
||
| 66 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
| 67 | */ |
||
| 68 | 11 | public function apply(Order $Order, OrderStatus $OrderStatus) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * 指定ステータスに遷移できるかどうかを判定. |
||
| 80 | * |
||
| 81 | * @param Order $Order 受注 |
||
| 82 | * @param OrderStatus $OrderStatus 遷移先ステータス |
||
| 83 | * |
||
| 84 | * @return boolean 指定ステータスに遷移できる場合はtrue |
||
| 85 | */ |
||
| 86 | 44 | public function can(Order $Order, OrderStatus $OrderStatus) |
|
| 90 | |||
| 91 | 50 | private function getTransition(Order $Order, OrderStatus $OrderStatus) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | 1 | public static function getSubscribedEvents() |
|
| 118 | |||
| 119 | /* |
||
| 120 | * Event handlers. |
||
| 121 | */ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * 入金日を更新する. |
||
| 125 | * |
||
| 126 | * @param Event $event |
||
| 127 | */ |
||
| 128 | 1 | public function updatePaymentDate(Event $event) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * 発送日を更新する. |
||
| 137 | * |
||
| 138 | * @param Event $event |
||
| 139 | */ |
||
| 140 | 3 | public function updateShippingDate(Event $event) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * 会員の保有ポイントを減らす. |
||
| 153 | * |
||
| 154 | * @param Event $event |
||
| 155 | * |
||
| 156 | * @throws PurchaseFlow\PurchaseException |
||
| 157 | */ |
||
| 158 | 2 | public function commitUsePoint(Event $event) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * 利用ポイントを会員に戻す. |
||
| 167 | * |
||
| 168 | * @param Event $event |
||
| 169 | */ |
||
| 170 | 2 | public function rollbackUsePoint(Event $event) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * 在庫を減らす. |
||
| 179 | * |
||
| 180 | * @param Event $event |
||
| 181 | * |
||
| 182 | * @throws PurchaseFlow\PurchaseException |
||
| 183 | */ |
||
| 184 | 1 | public function commitStock(Event $event) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * 在庫を戻す. |
||
| 193 | * |
||
| 194 | * @param Event $event |
||
| 195 | */ |
||
| 196 | 1 | public function rollbackStock(Event $event) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * 会員に加算ポイントを付与する. |
||
| 205 | * |
||
| 206 | * @param Event $event |
||
| 207 | */ |
||
| 208 | 4 | View Code Duplication | public function commitAddPoint(Event $event) |
| 217 | |||
| 218 | /** |
||
| 219 | * 会員に付与した加算ポイントを取り消す. |
||
| 220 | * |
||
| 221 | * @param Event $event |
||
| 222 | */ |
||
| 223 | 1 | View Code Duplication | public function rollbackAddPoint(Event $event) |
| 232 | |||
| 233 | /** |
||
| 234 | * 受注ステータスを再設定. |
||
| 235 | * {@link StateMachine}によって遷移が終了したときには{@link Order#OrderStatus}のidが変更されるだけなのでOrderStatusを設定し直す. |
||
| 236 | * |
||
| 237 | * @param Event $event |
||
| 238 | */ |
||
| 239 | 11 | public function onCompleted(Event $event) |
|
| 252 | } |
||
| 253 |