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 ShoppingService 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 ShoppingService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | class ShoppingService |
||
| 65 | { |
||
| 66 | /** |
||
| 67 | * @Inject(MailTemplateRepository::class) |
||
| 68 | * @var MailTemplateRepository |
||
| 69 | */ |
||
| 70 | protected $mailTemplateRepository; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @Inject(MailService::class) |
||
| 74 | * @var MailService |
||
| 75 | */ |
||
| 76 | protected $mailService; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @Inject("eccube.event.dispatcher") |
||
| 80 | * @var EventDispatcher |
||
| 81 | */ |
||
| 82 | protected $eventDispatcher; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @Inject("form.factory") |
||
| 86 | * @var FormFactory |
||
| 87 | */ |
||
| 88 | protected $formFactory; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @Inject(DeliveryFeeRepository::class) |
||
| 92 | * @var DeliveryFeeRepository |
||
| 93 | */ |
||
| 94 | protected $deliveryFeeRepository; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @Inject(TaxRuleRepository::class) |
||
| 98 | * @var TaxRuleRepository |
||
| 99 | */ |
||
| 100 | protected $taxRuleRepository; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @Inject(CustomerAddressRepository::class) |
||
| 104 | * @var CustomerAddressRepository |
||
| 105 | */ |
||
| 106 | protected $customerAddressRepository; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @Inject(DeliveryRepository::class) |
||
| 110 | * @var DeliveryRepository |
||
| 111 | */ |
||
| 112 | protected $deliveryRepository; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @Inject(DeliveryTimeRepository::class) |
||
| 116 | * @var DeliveryTimeRepository |
||
| 117 | */ |
||
| 118 | protected $deliveryTimeRepository; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @Inject(OrderStatusRepository::class) |
||
| 122 | * @var OrderStatusRepository |
||
| 123 | */ |
||
| 124 | protected $orderStatusRepository; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @Inject(PaymentRepository::class) |
||
| 128 | * @var PaymentRepository |
||
| 129 | */ |
||
| 130 | protected $paymentRepository; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @Inject(DeviceTypeRepository::class) |
||
| 134 | * @var DeviceTypeRepository |
||
| 135 | */ |
||
| 136 | protected $deviceTypeRepository; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @Inject("orm.em") |
||
| 140 | * @var EntityManager |
||
| 141 | */ |
||
| 142 | protected $entityManager; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @Inject("config") |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $appConfig; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @Inject(PrefRepository::class) |
||
| 152 | * @var PrefRepository |
||
| 153 | */ |
||
| 154 | protected $prefRepository; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @Inject("session") |
||
| 158 | * @var Session |
||
| 159 | */ |
||
| 160 | protected $session; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @Inject(OrderRepository::class) |
||
| 164 | * @var OrderRepository |
||
| 165 | */ |
||
| 166 | protected $orderRepository; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @Inject(BaseInfo::class) |
||
| 170 | * @var BaseInfo |
||
| 171 | */ |
||
| 172 | protected $BaseInfo; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @Inject(Application::class) |
||
| 176 | * @var \Eccube\Application |
||
| 177 | */ |
||
| 178 | public $app; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @Inject(CartService::class) |
||
| 182 | * @var \Eccube\Service\CartService |
||
| 183 | */ |
||
| 184 | protected $cartService; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var \Eccube\Service\OrderService |
||
| 188 | * |
||
| 189 | * @deprecated |
||
| 190 | */ |
||
| 191 | protected $orderService; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * セッションにセットされた受注情報を取得 |
||
| 195 | * |
||
| 196 | * @param null $status |
||
| 197 | * @return null|object |
||
| 198 | */ |
||
| 199 | public function getOrder($status = null) |
||
| 223 | |||
| 224 | /** |
||
|
|
|||
| 225 | * 非会員情報を取得 |
||
| 226 | * |
||
| 227 | * @param $sesisonKey |
||
| 228 | * @return $Customer|null |
||
| 229 | */ |
||
| 230 | public function getNonMember($sesisonKey) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * 受注情報を作成 |
||
| 258 | * |
||
| 259 | * @param $Customer |
||
| 260 | * @return \Eccube\Entity\Order |
||
| 261 | */ |
||
| 262 | public function createOrder($Customer) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * 仮受注情報作成 |
||
| 286 | * |
||
| 287 | * @param $Customer |
||
| 288 | * @param $preOrderId |
||
| 289 | * @return mixed |
||
| 290 | * @throws \Doctrine\ORM\NoResultException |
||
| 291 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 292 | */ |
||
| 293 | public function registerPreOrder(Customer $Customer, $preOrderId) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * 受注情報を作成 |
||
| 361 | * |
||
| 362 | * @param $Customer |
||
| 363 | * @return \Eccube\Entity\Order |
||
| 364 | */ |
||
| 365 | public function getNewOrder(Customer $Customer) |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * 受注情報を作成 |
||
| 376 | * |
||
| 377 | * @return \Eccube\Entity\Order |
||
| 378 | */ |
||
| 379 | public function newOrder() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * 受注情報を作成 |
||
| 389 | * |
||
| 390 | * @param \Eccube\Entity\Order $Order |
||
| 391 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 392 | * @return \Eccube\Entity\Order |
||
| 393 | */ |
||
| 394 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
||
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * 配送業者情報を取得 |
||
| 432 | * |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | public function getDeliveriesCart() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * 配送業者情報を取得 |
||
| 447 | * |
||
| 448 | * @param Order $Order |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | public function getDeliveriesOrder(Order $Order) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * 配送業者情報を取得 |
||
| 463 | * |
||
| 464 | * @param $saleTypes |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | public function getDeliveries($saleTypes) |
||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * お届け先情報を作成 |
||
| 493 | * |
||
| 494 | * @param Order $Order |
||
| 495 | * @param Customer $Customer |
||
| 496 | * @param $deliveries |
||
| 497 | * @return Order |
||
| 498 | */ |
||
| 499 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * お届け先情報を作成 |
||
| 525 | * |
||
| 526 | * @param \Eccube\Entity\Shipping $Shipping |
||
| 527 | * @param \Eccube\Entity\Customer|null $Customer |
||
| 528 | * @return \Eccube\Entity\Shipping |
||
| 529 | */ |
||
| 530 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * 受注明細情報、配送商品情報を作成 |
||
| 587 | * |
||
| 588 | * @param Order $Order |
||
| 589 | * @return Order |
||
| 590 | */ |
||
| 591 | public function getNewDetails(Order $Order) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * 配送商品情報を作成 |
||
| 613 | * |
||
| 614 | * @param Order $Order |
||
| 615 | * @param Product $Product |
||
| 616 | * @param ProductClass $ProductClass |
||
| 617 | * @param $quantity |
||
| 618 | * @return \Eccube\Entity\OrderItem |
||
| 619 | */ |
||
| 620 | public function getNewOrderItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * お届け先ごとの送料合計を取得 |
||
| 677 | * |
||
| 678 | * @param $shippings |
||
| 679 | * @return int |
||
| 680 | */ |
||
| 681 | public function getShippingDeliveryFeeTotal($shippings) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * 商品ごとの配送料を取得 |
||
| 694 | * |
||
| 695 | * @param Shipping $Shipping |
||
| 696 | * @return int |
||
| 697 | */ |
||
| 698 | public function getProductDeliveryFee(Shipping $Shipping) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * 住所などの情報が変更された時に金額の再計算を行う |
||
| 711 | * @deprecated PurchaseFlowで行う |
||
| 712 | * @param Order $Order |
||
| 713 | * @return Order |
||
| 714 | */ |
||
| 715 | public function getAmount(Order $Order) |
||
| 737 | 1 | ||
| 738 | /** |
||
| 739 | * 配送料金の設定 |
||
| 740 | * |
||
| 741 | * @param Shipping $Shipping |
||
| 742 | * @param Delivery|null $Delivery |
||
| 743 | */ |
||
| 744 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
||
| 765 | |||
| 766 | /** |
||
| 767 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
| 768 | * |
||
| 769 | * @param Order $Order |
||
| 770 | */ |
||
| 771 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
|
| 787 | |||
| 788 | /** |
||
| 789 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
| 790 | * |
||
| 791 | * @param Order $Order |
||
| 792 | */ |
||
| 793 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * 受注情報、お届け先情報の更新 |
||
| 812 | * |
||
| 813 | * @param Order $Order 受注情報 |
||
| 814 | * @param $data フォームデータ |
||
| 815 | * |
||
| 816 | * @deprecated since 3.0.5, to be removed in 3.1 |
||
| 817 | */ |
||
| 818 | public function setOrderUpdate(Order $Order, $data) |
||
| 851 | |||
| 852 | |||
| 853 | /** |
||
| 854 | * 受注情報の更新 |
||
| 855 | * |
||
| 856 | * @param Order $Order 受注情報 |
||
| 857 | */ |
||
| 858 | public function setOrderUpdateData(Order $Order) |
||
| 866 | 3 | ||
| 867 | |||
| 868 | /** |
||
| 869 | * 会員情報の更新 |
||
| 870 | * |
||
| 871 | * @param Order $Order 受注情報 |
||
| 872 | * @param Customer $user ログインユーザ |
||
| 873 | */ |
||
| 874 | public function setCustomerUpdate(Order $Order, Customer $user) |
||
| 887 | 2 | ||
| 888 | 2 | ||
| 889 | /** |
||
| 890 | * 支払方法選択の表示設定 |
||
| 891 | * |
||
| 892 | * @param $payments 支払選択肢情報 |
||
| 893 | * @param $subTotal 小計 |
||
| 894 | * @return array |
||
| 895 | */ |
||
| 896 | public function getPayments($payments, $subTotal) |
||
| 914 | 1 | ||
| 915 | /** |
||
| 916 | * お届け日を取得 |
||
| 917 | * |
||
| 918 | * @param Order $Order |
||
| 919 | * @return array |
||
| 920 | */ |
||
| 921 | public function getFormDeliveryDurations(Order $Order) |
||
| 922 | { |
||
| 923 | |||
| 924 | 2 | // お届け日の設定 |
|
| 925 | $minDate = 0; |
||
| 926 | $deliveryDurationFlag = false; |
||
| 927 | |||
| 928 | 2 | // 配送時に最大となる商品日数を取得 |
|
| 929 | 2 | View Code Duplication | foreach ($Order->getOrderItems() as $item) { |
| 930 | if (!$item->isProduct()) { |
||
| 931 | continue; |
||
| 932 | 2 | } |
|
| 933 | 2 | $ProductClass = $item->getProductClass(); |
|
| 934 | 1 | $deliveryDuration = $ProductClass->getDeliveryDuration(); |
|
| 935 | if (!is_null($deliveryDuration)) { |
||
| 936 | 2 | if ($deliveryDuration->getDuration() < 0) { |
|
| 937 | 2 | // 配送日数がマイナスの場合はお取り寄せなのでスキップする |
|
| 938 | 2 | $deliveryDurationFlag = false; |
|
| 939 | 2 | break; |
|
| 940 | } |
||
| 941 | 1 | ||
| 942 | 1 | if ($minDate < $deliveryDuration->getDuration()) { |
|
| 943 | $minDate = $deliveryDuration->getDuration(); |
||
| 944 | } |
||
| 945 | 1 | // 配送日数が設定されている |
|
| 946 | $deliveryDurationFlag = true; |
||
| 947 | } |
||
| 948 | } |
||
| 949 | 1 | ||
| 950 | // 配達最大日数期間を設定 |
||
| 951 | $deliveryDurations = array(); |
||
| 952 | |||
| 953 | // 配送日数が設定されている |
||
| 954 | 2 | View Code Duplication | if ($deliveryDurationFlag) { |
| 955 | $period = new \DatePeriod ( |
||
| 956 | new \DateTime($minDate.' day'), |
||
| 957 | 2 | new \DateInterval('P1D'), |
|
| 958 | 1 | new \DateTime($minDate + $this->appConfig['deliv_date_end_max'].' day') |
|
| 959 | 1 | ); |
|
| 960 | 1 | ||
| 961 | 1 | foreach ($period as $day) { |
|
| 962 | $deliveryDurations[$day->format('Y/m/d')] = $day->format('Y/m/d'); |
||
| 963 | } |
||
| 964 | 1 | } |
|
| 965 | 1 | ||
| 966 | return $deliveryDurations; |
||
| 967 | } |
||
| 968 | |||
| 969 | 2 | /** |
|
| 970 | * 支払方法を取得 |
||
| 971 | * |
||
| 972 | * @param $deliveries |
||
| 973 | * @param Order $Order |
||
| 974 | * @return array |
||
| 975 | */ |
||
| 976 | public function getFormPayments($deliveries, Order $Order) |
||
| 997 | |||
| 998 | /** |
||
| 999 | * お届け先ごとにFormを作成 |
||
| 1000 | * |
||
| 1001 | * @param Order $Order |
||
| 1002 | * @return \Symfony\Component\Form\Form |
||
| 1003 | * @deprecated since 3.0, to be removed in 3.1 |
||
| 1004 | */ |
||
| 1005 | View Code Duplication | public function getShippingForm(Order $Order) |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * お届け先ごとにFormBuilderを作成 |
||
| 1034 | * |
||
| 1035 | * @param Order $Order |
||
| 1036 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
| 1037 | * |
||
| 1038 | * @deprecated 利用している箇所なし |
||
| 1039 | */ |
||
| 1040 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
|
| 1064 | |||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * フォームデータを更新 |
||
| 1068 | * |
||
| 1069 | * @param Order $Order |
||
| 1070 | * @param array $data |
||
| 1071 | * |
||
| 1072 | * @deprecated |
||
| 1073 | */ |
||
| 1074 | public function setFormData(Order $Order, array $data) |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * 配送料の合計金額を計算 |
||
| 1100 | * |
||
| 1101 | * @param Order $Order |
||
| 1102 | * @return Order |
||
| 1103 | */ |
||
| 1104 | public function calculateDeliveryFee(Order $Order) |
||
| 1123 | 2 | ||
| 1124 | |||
| 1125 | /** |
||
| 1126 | * 購入処理を行う |
||
| 1127 | * |
||
| 1128 | * @param Order $Order |
||
| 1129 | * @throws ShoppingException |
||
| 1130 | * @deprecated PurchaseFlow::purchase() を使用してください |
||
| 1131 | */ |
||
| 1132 | public function processPurchase(Order $Order) |
||
| 1146 | |||
| 1147 | |||
| 1148 | /** |
||
| 1149 | * 値引き可能かチェック |
||
| 1150 | * |
||
| 1151 | * @param Order $Order |
||
| 1152 | * @param $discount |
||
| 1153 | * @return bool |
||
| 1154 | */ |
||
| 1155 | public function isDiscount(Order $Order, $discount) |
||
| 1164 | |||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * 値引き金額をセット |
||
| 1168 | * |
||
| 1169 | * @param Order $Order |
||
| 1170 | * @param $discount |
||
| 1171 | */ |
||
| 1172 | public function setDiscount(Order $Order, $discount) |
||
| 1178 | |||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * 合計金額を計算 |
||
| 1182 | * |
||
| 1183 | * @param Order $Order |
||
| 1184 | * @return Order |
||
| 1185 | */ |
||
| 1186 | public function calculatePrice(Order $Order) |
||
| 1202 | |||
| 1203 | /** |
||
| 1204 | * 受注ステータスをセット |
||
| 1205 | * |
||
| 1206 | * @param Order $Order |
||
| 1207 | * @param $status |
||
| 1208 | * @return Order |
||
| 1209 | */ |
||
| 1210 | public function setOrderStatus(Order $Order, $status) |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * 受注メール送信を行う |
||
| 1230 | * |
||
| 1231 | * @param Order $Order |
||
| 1232 | * @return MailHistory |
||
| 1233 | */ |
||
| 1234 | public function sendOrderMail(Order $Order) |
||
| 1254 | |||
| 1255 | |||
| 1256 | /** |
||
| 1257 | * 受注処理完了通知 |
||
| 1258 | * |
||
| 1259 | * @param Order $Order |
||
| 1260 | */ |
||
| 1261 | public function notifyComplete(Order $Order) |
||
| 1273 | |||
| 1274 | |||
| 1275 | } |
||
| 1276 |