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 $productTypes |
||
465 | * @return array |
||
466 | */ |
||
467 | public function getDeliveries($productTypes) |
||
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 getFormDeliveryDates(Order $Order) |
||
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 | */ |
||
1131 | public function processPurchase(Order $Order) |
||
1145 | 1 | ||
1146 | |||
1147 | /** |
||
1148 | * 値引き可能かチェック |
||
1149 | * |
||
1150 | * @param Order $Order |
||
1151 | * @param $discount |
||
1152 | * @return bool |
||
1153 | */ |
||
1154 | public function isDiscount(Order $Order, $discount) |
||
1163 | |||
1164 | |||
1165 | /** |
||
1166 | * 値引き金額をセット |
||
1167 | * |
||
1168 | * @param Order $Order |
||
1169 | * @param $discount |
||
1170 | */ |
||
1171 | public function setDiscount(Order $Order, $discount) |
||
1177 | |||
1178 | |||
1179 | /** |
||
1180 | * 合計金額を計算 |
||
1181 | * |
||
1182 | * @param Order $Order |
||
1183 | * @return Order |
||
1184 | */ |
||
1185 | public function calculatePrice(Order $Order) |
||
1201 | 1 | ||
1202 | /** |
||
1203 | * 受注ステータスをセット |
||
1204 | * |
||
1205 | * @param Order $Order |
||
1206 | * @param $status |
||
1207 | * @return Order |
||
1208 | */ |
||
1209 | public function setOrderStatus(Order $Order, $status) |
||
1226 | 3 | ||
1227 | /** |
||
1228 | * 受注メール送信を行う |
||
1229 | * |
||
1230 | * @param Order $Order |
||
1231 | * @return MailHistory |
||
1232 | */ |
||
1233 | public function sendOrderMail(Order $Order) |
||
1253 | 2 | ||
1254 | |||
1255 | /** |
||
1256 | * 受注処理完了通知 |
||
1257 | * |
||
1258 | * @param Order $Order |
||
1259 | */ |
||
1260 | public function notifyComplete(Order $Order) |
||
1272 | |||
1273 | |||
1274 | } |
||
1275 |