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:
Complex classes like ShoppingController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ShoppingController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class ShoppingController extends AbstractShoppingController |
||
|
|
|||
| 49 | { |
||
| 50 | /** |
||
| 51 | * @var BaseInfo |
||
| 52 | */ |
||
| 53 | protected $BaseInfo; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var OrderHelper |
||
| 57 | */ |
||
| 58 | protected $orderHelper; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var CartService |
||
| 62 | */ |
||
| 63 | protected $cartService; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var ShoppingService |
||
| 67 | */ |
||
| 68 | protected $shoppingService; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var CustomerAddressRepository |
||
| 72 | */ |
||
| 73 | protected $customerAddressRepository; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var ParameterBag |
||
| 77 | */ |
||
| 78 | protected $parameterBag; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * ShoppingController constructor. |
||
| 82 | * |
||
| 83 | * @param BaseInfo $BaseInfo |
||
| 84 | * @param OrderHelper $orderHelper |
||
| 85 | * @param CartService $cartService |
||
| 86 | * @param ShoppingService $shoppingService |
||
| 87 | * @param CustomerAddressRepository $customerAddressRepository |
||
| 88 | * @param ParameterBag $parameterBag |
||
| 89 | */ |
||
| 90 | 59 | public function __construct( |
|
| 91 | BaseInfo $BaseInfo, |
||
| 92 | OrderHelper $orderHelper, |
||
| 93 | CartService $cartService, |
||
| 94 | ShoppingService $shoppingService, |
||
| 95 | CustomerAddressRepository $customerAddressRepository, |
||
| 96 | ParameterBag $parameterBag |
||
| 97 | ) { |
||
| 98 | 59 | $this->BaseInfo = $BaseInfo; |
|
| 99 | 59 | $this->orderHelper = $orderHelper; |
|
| 100 | 59 | $this->cartService = $cartService; |
|
| 101 | 59 | $this->shoppingService = $shoppingService; |
|
| 102 | 59 | $this->customerAddressRepository = $customerAddressRepository; |
|
| 103 | 59 | $this->parameterBag = $parameterBag; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * 購入画面表示 |
||
| 108 | * |
||
| 109 | * @Route("/shopping", name="shopping") |
||
| 110 | * @Template("Shopping/index.twig") |
||
| 111 | */ |
||
| 112 | 51 | public function index(Request $request) |
|
| 113 | { |
||
| 114 | // カートチェック |
||
| 115 | 51 | $response = $this->forwardToRoute('shopping_check_to_cart'); |
|
| 116 | 51 | if ($response->isRedirection() || $response->getContent()) { |
|
| 117 | 1 | return $response; |
|
| 118 | } |
||
| 119 | |||
| 120 | // 受注情報を初期化 |
||
| 121 | 50 | $response = $this->forwardToRoute('shopping_initialize_order'); |
|
| 122 | 50 | if ($response->isRedirection() || $response->getContent()) { |
|
| 123 | return $response; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** @var Order $Order */ |
||
| 127 | 50 | $Order = $this->parameterBag->get('Order'); |
|
| 128 | |||
| 129 | // 単価集計 |
||
| 130 | 50 | $flowResult = $this->executePurchaseFlow($Order); |
|
| 131 | |||
| 132 | // 明細が丸められる場合に, カートから注文画面へ遷移できなくなるため, 集計の結果を保存する |
||
| 133 | 50 | $this->entityManager->flush(); |
|
| 134 | |||
| 135 | // フォームを生成する |
||
| 136 | 50 | $this->forwardToRoute('shopping_create_form'); |
|
| 137 | |||
| 138 | 50 | if ($flowResult->hasWarning() || $flowResult->hasError()) { |
|
| 139 | 10 | return $this->redirectToRoute('cart'); |
|
| 140 | } |
||
| 141 | |||
| 142 | 45 | $form = $this->parameterBag->get(OrderType::class); |
|
| 143 | |||
| 144 | return [ |
||
| 145 | 45 | 'form' => $form->createView(), |
|
| 146 | 45 | 'Order' => $Order, |
|
| 147 | ]; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * 購入確認画面から, 他の画面へのリダイレクト. |
||
| 152 | * 配送業者や支払方法、お問い合わせ情報をDBに保持してから遷移する. |
||
| 153 | * |
||
| 154 | * @Route("/shopping/redirect", name="shopping_redirect_to") |
||
| 155 | * @Template("Shopping/index.twig") |
||
| 156 | */ |
||
| 157 | 20 | public function redirectTo(Request $request) |
|
| 158 | { |
||
| 159 | // カートチェック |
||
| 160 | 20 | $response = $this->forwardToRoute('shopping_check_to_cart'); |
|
| 161 | 20 | if ($response->isRedirection() || $response->getContent()) { |
|
| 162 | return $response; |
||
| 163 | } |
||
| 164 | |||
| 165 | // 受注の存在チェック |
||
| 166 | 20 | $response = $this->forwardToRoute('shopping_exists_order'); |
|
| 167 | 20 | if ($response->isRedirection() || $response->getContent()) { |
|
| 168 | return $response; |
||
| 169 | } |
||
| 170 | |||
| 171 | // フォームの生成 |
||
| 172 | 20 | $this->forwardToRoute('shopping_create_form'); |
|
| 173 | 20 | $form = $this->parameterBag->get(OrderType::class); |
|
| 174 | 20 | $form->handleRequest($request); |
|
| 175 | |||
| 176 | // 各種変更ページへリダイレクトする |
||
| 177 | 20 | $response = $this->forwardToRoute('shopping_redirect_to_change'); |
|
| 178 | 20 | if ($response->isRedirection() || $response->getContent()) { |
|
| 179 | 8 | return $response; |
|
| 180 | } |
||
| 181 | 12 | $form = $this->parameterBag->get(OrderType::class); |
|
| 182 | 12 | $Order = $this->parameterBag->get('Order'); |
|
| 183 | |||
| 184 | return [ |
||
| 185 | 12 | 'form' => $form->createView(), |
|
| 186 | 12 | 'Order' => $Order, |
|
| 187 | ]; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * 購入処理 |
||
| 192 | * |
||
| 193 | * @Route("/shopping/confirm", name="shopping_confirm") |
||
| 194 | * @Method("POST") |
||
| 195 | * @Template("Shopping/confirm.twig") |
||
| 196 | */ |
||
| 197 | 5 | public function confirm(Request $request) |
|
| 198 | { |
||
| 199 | // カートチェック |
||
| 200 | 5 | $response = $this->forwardToRoute('shopping_check_to_cart'); |
|
| 201 | 5 | if ($response->isRedirection() || $response->getContent()) { |
|
| 202 | return $response; |
||
| 203 | } |
||
| 204 | |||
| 205 | // 受注の存在チェック |
||
| 206 | 5 | $response = $this->forwardToRoute('shopping_exists_order'); |
|
| 207 | 5 | if ($response->isRedirection() || $response->getContent()) { |
|
| 208 | return $response; |
||
| 209 | } |
||
| 210 | |||
| 211 | // フォームの生成 |
||
| 212 | 5 | $this->forwardToRoute('shopping_create_form'); |
|
| 213 | 5 | $form = $this->parameterBag->get(OrderType::class); |
|
| 214 | 5 | $form->handleRequest($request); |
|
| 215 | |||
| 216 | 5 | $form = $this->parameterBag->get(OrderType::class); |
|
| 217 | 5 | $Order = $this->parameterBag->get('Order'); |
|
| 218 | |||
| 219 | 5 | $flowResult = $this->executePurchaseFlow($Order); |
|
| 220 | 5 | if ($flowResult->hasWarning() || $flowResult->hasError()) { |
|
| 221 | return $this->redirectToRoute('shopping_error'); |
||
| 222 | } |
||
| 223 | |||
| 224 | 5 | $paymentMethod = $this->createPaymentMethod($Order, $form); |
|
| 225 | |||
| 226 | 5 | $PaymentResult = $paymentMethod->verify(); |
|
| 227 | // エラーの場合は注文入力画面に戻す? |
||
| 228 | 5 | if ($PaymentResult instanceof PaymentResult) { |
|
| 229 | if (!$PaymentResult->isSuccess()) { |
||
| 230 | $this->entityManager->getConnection()->rollback(); |
||
| 231 | |||
| 232 | $this->addError($PaymentResult->getErrors()); |
||
| 233 | } |
||
| 234 | |||
| 235 | $response = $PaymentResult->getResponse(); |
||
| 236 | if ($response->isRedirection() || $response->getContent()) { |
||
| 237 | $this->entityManager->flush(); |
||
| 238 | |||
| 239 | return $response; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | 5 | $this->entityManager->flush(); |
|
| 243 | |||
| 244 | return [ |
||
| 245 | 5 | 'form' => $form->createView(), |
|
| 246 | 5 | 'Order' => $Order, |
|
| 247 | ]; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * 購入処理 |
||
| 252 | * |
||
| 253 | * @Route("/shopping/order", name="shopping_order") |
||
| 254 | * @Method("POST") |
||
| 255 | * @Template("Shopping/index.twig") |
||
| 256 | */ |
||
| 257 | 6 | public function order(Request $request) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * 支払方法バーリデト |
||
| 298 | */ |
||
| 299 | private function isValidPayment(Application $app, $form) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * 購入完了画面表示 |
||
| 327 | * |
||
| 328 | * @Route("/shopping/complete", name="shopping_complete") |
||
| 329 | * @Template("Shopping/complete.twig") |
||
| 330 | */ |
||
| 331 | 1 | public function complete(Request $request) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * お届け先の設定一覧からの選択 |
||
| 365 | * |
||
| 366 | * @Route("/shopping/shipping/{id}", name="shopping_shipping", requirements={"id" = "\d+"}) |
||
| 367 | * @Template("Shopping/shipping.twig") |
||
| 368 | */ |
||
| 369 | public function shipping(Request $request, Shipping $Shipping) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * お届け先の設定(非会員でも使用する) |
||
| 439 | * |
||
| 440 | * @Route("/shopping/shipping_edit/{id}", name="shopping_shipping_edit", requirements={"id" = "\d+"}) |
||
| 441 | * @Template("Shopping/shipping_edit.twig") |
||
| 442 | */ |
||
| 443 | public function shippingEdit(Request $request, $id) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * ログイン |
||
| 555 | * |
||
| 556 | * @Route("/shopping/login", name="shopping_login") |
||
| 557 | * @Template("Shopping/login.twig") |
||
| 558 | */ |
||
| 559 | 2 | View Code Duplication | public function login(Request $request, AuthenticationUtils $authenticationUtils) |
| 590 | |||
| 591 | /** |
||
| 592 | * 購入エラー画面表示 |
||
| 593 | * |
||
| 594 | * @Route("/shopping/error", name="shopping_error") |
||
| 595 | * @Template("Shopping/shopping_error.twig") |
||
| 596 | */ |
||
| 597 | 1 | public function shoppingError(Request $request) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * カート画面のチェック |
||
| 614 | * |
||
| 615 | * @ForwardOnly |
||
| 616 | * @Route("/shopping/check_to_cart", name="shopping_check_to_cart") |
||
| 617 | */ |
||
| 618 | 55 | public function checkToCart(Request $request) |
|
| 636 | |||
| 637 | /** |
||
| 638 | * 受注情報を初期化する. |
||
| 639 | * |
||
| 640 | * @ForwardOnly |
||
| 641 | * @Route("/shopping/initialize_order", name="shopping_initialize_order") |
||
| 642 | */ |
||
| 643 | 50 | public function initializeOrder(Request $request) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * フォームを作成し, イベントハンドラを設定する |
||
| 694 | * |
||
| 695 | * @ForwardOnly |
||
| 696 | * @Route("/shopping/create_form", name="shopping_create_form") |
||
| 697 | */ |
||
| 698 | 50 | public function createShoppingForm(Request $request) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * mode に応じて各変更ページへリダイレクトする. |
||
| 722 | * |
||
| 723 | * @ForwardOnly |
||
| 724 | * @Route("/shopping/redirect_to_change", name="shopping_redirect_to_change") |
||
| 725 | */ |
||
| 726 | 20 | public function redirectToChange(Request $request) |
|
| 762 | |||
| 763 | /** |
||
| 764 | * 受注の存在チェック |
||
| 765 | * |
||
| 766 | * @ForwardOnly |
||
| 767 | * @Route("/shopping/exists_order", name="shopping_exists_order") |
||
| 768 | */ |
||
| 769 | 26 | public function existsOrder(Request $request) |
|
| 782 | |||
| 783 | /** |
||
| 784 | * 受注完了処理 |
||
| 785 | * |
||
| 786 | * @ForwardOnly |
||
| 787 | * @Route("/shopping/complete_order", name="shopping_complete_order") |
||
| 788 | */ |
||
| 789 | 6 | public function completeOrder(Request $request) |
|
| 873 | |||
| 874 | /** |
||
| 875 | * 決済完了処理 |
||
| 876 | * |
||
| 877 | * @ForwardOnly |
||
| 878 | * @Route("/shopping/do_checkout_order", name="shopping_do_checkout_order") |
||
| 879 | */ |
||
| 880 | 6 | public function doCheckoutOrder(Request $request) |
|
| 902 | |||
| 903 | /** |
||
| 904 | * 受注完了の後処理 |
||
| 905 | * |
||
| 906 | * @ForwardOnly |
||
| 907 | * @Route("/shopping/after_complete", name="shopping_after_complete") |
||
| 908 | */ |
||
| 909 | 6 | public function afterComplete(Request $request) |
|
| 957 | |||
| 958 | 6 | private function createPaymentMethod(Order $Order, FormInterface $form) |
|
| 966 | } |
||
| 967 |