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 |
||
66 | class ShoppingService |
||
67 | { |
||
68 | /** |
||
69 | * @Inject(MailTemplateRepository::class) |
||
70 | * @var MailTemplateRepository |
||
71 | */ |
||
72 | protected $mailTemplateRepository; |
||
73 | |||
74 | /** |
||
75 | * @Inject(MailService::class) |
||
76 | * @var MailService |
||
77 | */ |
||
78 | protected $mailService; |
||
79 | |||
80 | /** |
||
81 | * @Inject("eccube.event.dispatcher") |
||
82 | * @var EventDispatcher |
||
83 | */ |
||
84 | protected $eventDispatcher; |
||
85 | |||
86 | /** |
||
87 | * @Inject("form.factory") |
||
88 | * @var FormFactory |
||
89 | */ |
||
90 | protected $formFactory; |
||
91 | |||
92 | /** |
||
93 | * @Inject(DeliveryFeeRepository::class) |
||
94 | * @var DeliveryFeeRepository |
||
95 | */ |
||
96 | protected $deliveryFeeRepository; |
||
97 | |||
98 | /** |
||
99 | * @Inject(TaxRuleRepository::class) |
||
100 | * @var TaxRuleRepository |
||
101 | */ |
||
102 | protected $taxRuleRepository; |
||
103 | |||
104 | /** |
||
105 | * @Inject(CustomerAddressRepository::class) |
||
106 | * @var CustomerAddressRepository |
||
107 | */ |
||
108 | protected $customerAddressRepository; |
||
109 | |||
110 | /** |
||
111 | * @Inject(DeliveryRepository::class) |
||
112 | * @var DeliveryRepository |
||
113 | */ |
||
114 | protected $deliveryRepository; |
||
115 | |||
116 | /** |
||
117 | * @Inject(OrderStatusRepository::class) |
||
118 | * @var OrderStatusRepository |
||
119 | */ |
||
120 | protected $orderStatusRepository; |
||
121 | |||
122 | /** |
||
123 | * @Inject(PaymentRepository::class) |
||
124 | * @var PaymentRepository |
||
125 | */ |
||
126 | protected $paymentRepository; |
||
127 | |||
128 | /** |
||
129 | * @Inject(DeviceTypeRepository::class) |
||
130 | * @var DeviceTypeRepository |
||
131 | */ |
||
132 | protected $deviceTypeRepository; |
||
133 | |||
134 | /** |
||
135 | * @Inject("orm.em") |
||
136 | * @var EntityManager |
||
137 | */ |
||
138 | protected $entityManager; |
||
139 | |||
140 | /** |
||
141 | * @Inject("config") |
||
142 | * @var array |
||
143 | */ |
||
144 | protected $appConfig; |
||
145 | |||
146 | /** |
||
147 | * @Inject(PrefRepository::class) |
||
148 | * @var PrefRepository |
||
149 | */ |
||
150 | protected $prefRepository; |
||
151 | |||
152 | /** |
||
153 | * @Inject("session") |
||
154 | * @var Session |
||
155 | */ |
||
156 | protected $session; |
||
157 | |||
158 | /** |
||
159 | * @Inject(OrderRepository::class) |
||
160 | * @var OrderRepository |
||
161 | */ |
||
162 | protected $orderRepository; |
||
163 | |||
164 | /** |
||
165 | * @Inject(BaseInfo::class) |
||
166 | * @var BaseInfo |
||
167 | */ |
||
168 | protected $BaseInfo; |
||
169 | |||
170 | /** |
||
171 | * @Inject(Application::class) |
||
172 | * @var \Eccube\Application |
||
173 | */ |
||
174 | public $app; |
||
175 | |||
176 | /** |
||
177 | * @Inject(CartService::class) |
||
178 | * @var \Eccube\Service\CartService |
||
179 | */ |
||
180 | protected $cartService; |
||
181 | |||
182 | /** |
||
183 | * @var \Eccube\Service\OrderService |
||
184 | * |
||
185 | * @deprecated |
||
186 | */ |
||
187 | protected $orderService; |
||
188 | |||
189 | /** |
||
190 | * セッションにセットされた受注情報を取得 |
||
191 | * |
||
192 | * @param null $status |
||
193 | * @return null|object |
||
194 | */ |
||
195 | 11 | public function getOrder($status = null) |
|
219 | |||
220 | /** |
||
|
|||
221 | * 非会員情報を取得 |
||
222 | * |
||
223 | * @param $sesisonKey |
||
224 | * @return $Customer|null |
||
225 | */ |
||
226 | 4 | public function getNonMember($sesisonKey) |
|
251 | |||
252 | /** |
||
253 | * 受注情報を作成 |
||
254 | * |
||
255 | * @param $Customer |
||
256 | * @return \Eccube\Entity\Order |
||
257 | */ |
||
258 | public function createOrder($Customer) |
||
279 | |||
280 | /** |
||
281 | * 仮受注情報作成 |
||
282 | * |
||
283 | * @param $Customer |
||
284 | * @param $preOrderId |
||
285 | * @return mixed |
||
286 | * @throws \Doctrine\ORM\NoResultException |
||
287 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
288 | */ |
||
289 | public function registerPreOrder(Customer $Customer, $preOrderId) |
||
354 | |||
355 | /** |
||
356 | * 受注情報を作成 |
||
357 | * |
||
358 | * @param $Customer |
||
359 | * @return \Eccube\Entity\Order |
||
360 | */ |
||
361 | public function getNewOrder(Customer $Customer) |
||
368 | |||
369 | |||
370 | /** |
||
371 | * 受注情報を作成 |
||
372 | * |
||
373 | * @return \Eccube\Entity\Order |
||
374 | */ |
||
375 | public function newOrder() |
||
382 | |||
383 | /** |
||
384 | * 受注情報を作成 |
||
385 | * |
||
386 | * @param \Eccube\Entity\Order $Order |
||
387 | * @param \Eccube\Entity\Customer|null $Customer |
||
388 | * @return \Eccube\Entity\Order |
||
389 | */ |
||
390 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
||
424 | |||
425 | |||
426 | /** |
||
427 | * 配送業者情報を取得 |
||
428 | * |
||
429 | * @return array |
||
430 | */ |
||
431 | public function getDeliveriesCart() |
||
440 | |||
441 | /** |
||
442 | * 配送業者情報を取得 |
||
443 | * |
||
444 | * @param Order $Order |
||
445 | * @return array |
||
446 | */ |
||
447 | public function getDeliveriesOrder(Order $Order) |
||
456 | |||
457 | /** |
||
458 | * 配送業者情報を取得 |
||
459 | * |
||
460 | * @param $productTypes |
||
461 | * @return array |
||
462 | */ |
||
463 | 3 | public function getDeliveries($productTypes) |
|
485 | |||
486 | |||
487 | /** |
||
488 | * お届け先情報を作成 |
||
489 | * |
||
490 | * @param Order $Order |
||
491 | * @param Customer $Customer |
||
492 | * @param $deliveries |
||
493 | * @return Order |
||
494 | */ |
||
495 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
||
519 | |||
520 | /** |
||
521 | * お届け先情報を作成 |
||
522 | * |
||
523 | * @param \Eccube\Entity\Shipping $Shipping |
||
524 | * @param \Eccube\Entity\Customer|null $Customer |
||
525 | * @return \Eccube\Entity\Shipping |
||
526 | */ |
||
527 | 1 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
|
580 | |||
581 | |||
582 | /** |
||
583 | * 受注明細情報、配送商品情報を作成 |
||
584 | * |
||
585 | * @param Order $Order |
||
586 | * @return Order |
||
587 | */ |
||
588 | public function getNewDetails(Order $Order) |
||
612 | |||
613 | /** |
||
614 | * 受注明細情報を作成 |
||
615 | * |
||
616 | * @param Product $Product |
||
617 | * @param ProductClass $ProductClass |
||
618 | * @param $quantity |
||
619 | * @return \Eccube\Entity\OrderDetail |
||
620 | */ |
||
621 | public function getNewOrderDetail(Product $Product, ProductClass $ProductClass, $quantity) |
||
649 | |||
650 | /** |
||
651 | * 配送商品情報を作成 |
||
652 | * |
||
653 | * @param Order $Order |
||
654 | * @param Product $Product |
||
655 | * @param ProductClass $ProductClass |
||
656 | * @param $quantity |
||
657 | * @return \Eccube\Entity\OrderItem |
||
658 | */ |
||
659 | public function getNewOrderItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
||
660 | { |
||
661 | |||
662 | $OrderItem = new OrderItem(); |
||
663 | $shippings = $Order->getShippings(); |
||
664 | |||
665 | // 選択された商品がどのお届け先情報と関連するかチェック |
||
666 | $Shipping = null; |
||
667 | foreach ($shippings as $s) { |
||
668 | if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) { |
||
669 | // 商品種別が同一のお届け先情報と関連させる |
||
670 | $Shipping = $s; |
||
671 | break; |
||
672 | } |
||
673 | } |
||
674 | |||
675 | if (is_null($Shipping)) { |
||
676 | // お届け先情報と関連していない場合、エラー |
||
677 | throw new CartException('shopping.delivery.not.producttype'); |
||
678 | } |
||
679 | |||
680 | // 商品ごとの配送料合計 |
||
681 | $productDeliveryFeeTotal = 0; |
||
682 | if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) { |
||
683 | $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity; |
||
684 | } |
||
685 | |||
686 | $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal); |
||
687 | |||
688 | $OrderItem->setShipping($Shipping) |
||
689 | ->setOrder($Order) |
||
690 | ->setProductClass($ProductClass) |
||
691 | ->setProduct($Product) |
||
692 | ->setProductName($Product->getName()) |
||
693 | ->setProductCode($ProductClass->getCode()) |
||
694 | ->setPrice($ProductClass->getPrice02()) |
||
695 | ->setQuantity($quantity); |
||
696 | |||
697 | $ClassCategory1 = $ProductClass->getClassCategory1(); |
||
698 | if (!is_null($ClassCategory1)) { |
||
699 | $OrderItem->setClasscategoryName1($ClassCategory1->getName()); |
||
700 | $OrderItem->setClassName1($ClassCategory1->getClassName()->getName()); |
||
701 | } |
||
702 | $ClassCategory2 = $ProductClass->getClassCategory2(); |
||
703 | if (!is_null($ClassCategory2)) { |
||
704 | $OrderItem->setClasscategoryName2($ClassCategory2->getName()); |
||
705 | $OrderItem->setClassName2($ClassCategory2->getClassName()->getName()); |
||
706 | } |
||
707 | $Shipping->addOrderItem($OrderItem); |
||
708 | $this->entityManager->persist($OrderItem); |
||
709 | |||
710 | return $OrderItem; |
||
711 | |||
712 | } |
||
713 | |||
714 | /** |
||
715 | * お届け先ごとの送料合計を取得 |
||
716 | * |
||
717 | * @param $shippings |
||
718 | * @return int |
||
719 | */ |
||
720 | public function getShippingDeliveryFeeTotal($shippings) |
||
730 | |||
731 | /** |
||
732 | * 商品ごとの配送料を取得 |
||
733 | * |
||
734 | * @param Shipping $Shipping |
||
735 | * @return int |
||
736 | */ |
||
737 | public function getProductDeliveryFee(Shipping $Shipping) |
||
747 | |||
748 | /** |
||
749 | * 住所などの情報が変更された時に金額の再計算を行う |
||
750 | * @deprecated PurchaseFlowで行う |
||
751 | * @param Order $Order |
||
752 | * @return Order |
||
753 | */ |
||
754 | 1 | public function getAmount(Order $Order) |
|
776 | |||
777 | /** |
||
778 | * 配送料金の設定 |
||
779 | * |
||
780 | * @param Shipping $Shipping |
||
781 | * @param Delivery|null $Delivery |
||
782 | */ |
||
783 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
||
803 | |||
804 | /** |
||
805 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
806 | * |
||
807 | * @param Order $Order |
||
808 | */ |
||
809 | 4 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
825 | |||
826 | /** |
||
827 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
828 | * |
||
829 | * @param Order $Order |
||
830 | */ |
||
831 | 3 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
847 | |||
848 | |||
849 | /** |
||
850 | * 商品公開ステータスチェック、在庫チェック、購入制限数チェックを行い、在庫情報をロックする |
||
851 | * |
||
852 | * @param $em トランザクション制御されているEntityManager |
||
853 | * @param Order $Order 受注情報 |
||
854 | * @return bool true : 成功、false : 失敗 |
||
855 | */ |
||
856 | 4 | public function isOrderProduct($em, \Eccube\Entity\Order $Order) |
|
933 | |||
934 | /** |
||
935 | * 受注情報、お届け先情報の更新 |
||
936 | * |
||
937 | * @param Order $Order 受注情報 |
||
938 | * @param $data フォームデータ |
||
939 | * |
||
940 | * @deprecated since 3.0.5, to be removed in 3.1 |
||
941 | */ |
||
942 | public function setOrderUpdate(Order $Order, $data) |
||
974 | |||
975 | |||
976 | /** |
||
977 | * 受注情報の更新 |
||
978 | * |
||
979 | * @param Order $Order 受注情報 |
||
980 | */ |
||
981 | 3 | public function setOrderUpdateData(Order $Order) |
|
989 | |||
990 | |||
991 | /** |
||
992 | * 在庫情報の更新 |
||
993 | * |
||
994 | * @param $em トランザクション制御されているEntityManager |
||
995 | * @param Order $Order 受注情報 |
||
996 | */ |
||
997 | 3 | public function setStockUpdate($em, Order $Order) |
|
1026 | |||
1027 | |||
1028 | /** |
||
1029 | * 会員情報の更新 |
||
1030 | * |
||
1031 | * @param Order $Order 受注情報 |
||
1032 | * @param Customer $user ログインユーザ |
||
1033 | */ |
||
1034 | 2 | public function setCustomerUpdate(Order $Order, Customer $user) |
|
1051 | |||
1052 | |||
1053 | /** |
||
1054 | * 支払方法選択の表示設定 |
||
1055 | * |
||
1056 | * @param $payments 支払選択肢情報 |
||
1057 | * @param $subTotal 小計 |
||
1058 | * @return array |
||
1059 | */ |
||
1060 | 1 | public function getPayments($payments, $subTotal) |
|
1078 | |||
1079 | /** |
||
1080 | * お届け日を取得 |
||
1081 | * |
||
1082 | * @param Order $Order |
||
1083 | * @return array |
||
1084 | */ |
||
1085 | 2 | public function getFormDeliveryDates(Order $Order) |
|
1129 | |||
1130 | /** |
||
1131 | * 支払方法を取得 |
||
1132 | * |
||
1133 | * @param $deliveries |
||
1134 | * @param Order $Order |
||
1135 | * @return array |
||
1136 | */ |
||
1137 | public function getFormPayments($deliveries, Order $Order) |
||
1158 | |||
1159 | /** |
||
1160 | * お届け先ごとにFormを作成 |
||
1161 | * |
||
1162 | * @param Order $Order |
||
1163 | * @return \Symfony\Component\Form\Form |
||
1164 | * @deprecated since 3.0, to be removed in 3.1 |
||
1165 | */ |
||
1166 | View Code Duplication | public function getShippingForm(Order $Order) |
|
1192 | |||
1193 | /** |
||
1194 | * お届け先ごとにFormBuilderを作成 |
||
1195 | * |
||
1196 | * @param Order $Order |
||
1197 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
1198 | * |
||
1199 | * @deprecated 利用している箇所なし |
||
1200 | */ |
||
1201 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
|
1225 | |||
1226 | |||
1227 | /** |
||
1228 | * フォームデータを更新 |
||
1229 | * |
||
1230 | * @param Order $Order |
||
1231 | * @param array $data |
||
1232 | * |
||
1233 | * @deprecated |
||
1234 | */ |
||
1235 | 1 | public function setFormData(Order $Order, array $data) |
|
1253 | |||
1254 | /** |
||
1255 | * 配送料の合計金額を計算 |
||
1256 | * |
||
1257 | * @param Order $Order |
||
1258 | * @return Order |
||
1259 | */ |
||
1260 | 2 | public function calculateDeliveryFee(Order $Order) |
|
1279 | |||
1280 | |||
1281 | /** |
||
1282 | * 購入処理を行う |
||
1283 | * |
||
1284 | * @param Order $Order |
||
1285 | * @throws ShoppingException |
||
1286 | */ |
||
1287 | 2 | public function processPurchase(Order $Order) |
|
1314 | |||
1315 | |||
1316 | /** |
||
1317 | * 値引き可能かチェック |
||
1318 | * |
||
1319 | * @param Order $Order |
||
1320 | * @param $discount |
||
1321 | * @return bool |
||
1322 | */ |
||
1323 | public function isDiscount(Order $Order, $discount) |
||
1332 | |||
1333 | |||
1334 | /** |
||
1335 | * 値引き金額をセット |
||
1336 | * |
||
1337 | * @param Order $Order |
||
1338 | * @param $discount |
||
1339 | */ |
||
1340 | public function setDiscount(Order $Order, $discount) |
||
1346 | |||
1347 | |||
1348 | /** |
||
1349 | * 合計金額を計算 |
||
1350 | * |
||
1351 | * @param Order $Order |
||
1352 | * @return Order |
||
1353 | */ |
||
1354 | 1 | public function calculatePrice(Order $Order) |
|
1370 | |||
1371 | /** |
||
1372 | * 受注ステータスをセット |
||
1373 | * |
||
1374 | * @param Order $Order |
||
1375 | * @param $status |
||
1376 | * @return Order |
||
1377 | */ |
||
1378 | 3 | public function setOrderStatus(Order $Order, $status) |
|
1395 | |||
1396 | /** |
||
1397 | * 受注メール送信を行う |
||
1398 | * |
||
1399 | * @param Order $Order |
||
1400 | * @return MailHistory |
||
1401 | */ |
||
1402 | 2 | public function sendOrderMail(Order $Order) |
|
1422 | |||
1423 | |||
1424 | /** |
||
1425 | * 受注処理完了通知 |
||
1426 | * |
||
1427 | * @param Order $Order |
||
1428 | */ |
||
1429 | public function notifyComplete(Order $Order) |
||
1441 | |||
1442 | |||
1443 | } |
||
1444 |