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(DeliveryTimeRepository::class) |
||
118 | * @var DeliveryTimeRepository |
||
119 | */ |
||
120 | protected $deliveryTimeRepository; |
||
121 | |||
122 | /** |
||
123 | * @Inject(OrderStatusRepository::class) |
||
124 | * @var OrderStatusRepository |
||
125 | */ |
||
126 | protected $orderStatusRepository; |
||
127 | |||
128 | /** |
||
129 | * @Inject(PaymentRepository::class) |
||
130 | * @var PaymentRepository |
||
131 | */ |
||
132 | protected $paymentRepository; |
||
133 | |||
134 | /** |
||
135 | * @Inject(DeviceTypeRepository::class) |
||
136 | * @var DeviceTypeRepository |
||
137 | */ |
||
138 | protected $deviceTypeRepository; |
||
139 | |||
140 | /** |
||
141 | * @Inject("orm.em") |
||
142 | * @var EntityManager |
||
143 | */ |
||
144 | protected $entityManager; |
||
145 | |||
146 | /** |
||
147 | * @Inject("config") |
||
148 | * @var array |
||
149 | */ |
||
150 | protected $appConfig; |
||
151 | |||
152 | /** |
||
153 | * @Inject(PrefRepository::class) |
||
154 | * @var PrefRepository |
||
155 | */ |
||
156 | protected $prefRepository; |
||
157 | |||
158 | /** |
||
159 | * @Inject("session") |
||
160 | * @var Session |
||
161 | */ |
||
162 | protected $session; |
||
163 | |||
164 | /** |
||
165 | * @Inject(OrderRepository::class) |
||
166 | * @var OrderRepository |
||
167 | */ |
||
168 | protected $orderRepository; |
||
169 | |||
170 | /** |
||
171 | * @Inject(BaseInfo::class) |
||
172 | * @var BaseInfo |
||
173 | */ |
||
174 | protected $BaseInfo; |
||
175 | |||
176 | /** |
||
177 | * @Inject(Application::class) |
||
178 | * @var \Eccube\Application |
||
179 | */ |
||
180 | public $app; |
||
181 | |||
182 | /** |
||
183 | * @Inject(CartService::class) |
||
184 | * @var \Eccube\Service\CartService |
||
185 | */ |
||
186 | protected $cartService; |
||
187 | |||
188 | /** |
||
189 | * @var \Eccube\Service\OrderService |
||
190 | * |
||
191 | * @deprecated |
||
192 | */ |
||
193 | protected $orderService; |
||
194 | |||
195 | /** |
||
196 | * セッションにセットされた受注情報を取得 |
||
197 | * |
||
198 | * @param null $status |
||
199 | * @return null|object |
||
200 | */ |
||
201 | 11 | public function getOrder($status = null) |
|
225 | |||
226 | /** |
||
|
|||
227 | * 非会員情報を取得 |
||
228 | * |
||
229 | * @param $sesisonKey |
||
230 | * @return $Customer|null |
||
231 | */ |
||
232 | 4 | public function getNonMember($sesisonKey) |
|
257 | |||
258 | /** |
||
259 | * 受注情報を作成 |
||
260 | * |
||
261 | * @param $Customer |
||
262 | * @return \Eccube\Entity\Order |
||
263 | */ |
||
264 | public function createOrder($Customer) |
||
285 | |||
286 | /** |
||
287 | * 仮受注情報作成 |
||
288 | * |
||
289 | * @param $Customer |
||
290 | * @param $preOrderId |
||
291 | * @return mixed |
||
292 | * @throws \Doctrine\ORM\NoResultException |
||
293 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
294 | */ |
||
295 | public function registerPreOrder(Customer $Customer, $preOrderId) |
||
360 | |||
361 | /** |
||
362 | * 受注情報を作成 |
||
363 | * |
||
364 | * @param $Customer |
||
365 | * @return \Eccube\Entity\Order |
||
366 | */ |
||
367 | public function getNewOrder(Customer $Customer) |
||
374 | |||
375 | |||
376 | /** |
||
377 | * 受注情報を作成 |
||
378 | * |
||
379 | * @return \Eccube\Entity\Order |
||
380 | */ |
||
381 | public function newOrder() |
||
388 | |||
389 | /** |
||
390 | * 受注情報を作成 |
||
391 | * |
||
392 | * @param \Eccube\Entity\Order $Order |
||
393 | * @param \Eccube\Entity\Customer|null $Customer |
||
394 | * @return \Eccube\Entity\Order |
||
395 | */ |
||
396 | public function copyToOrderFromCustomer(Order $Order, Customer $Customer = null) |
||
430 | |||
431 | |||
432 | /** |
||
433 | * 配送業者情報を取得 |
||
434 | * |
||
435 | * @return array |
||
436 | */ |
||
437 | public function getDeliveriesCart() |
||
446 | |||
447 | /** |
||
448 | * 配送業者情報を取得 |
||
449 | * |
||
450 | * @param Order $Order |
||
451 | * @return array |
||
452 | */ |
||
453 | public function getDeliveriesOrder(Order $Order) |
||
462 | |||
463 | /** |
||
464 | * 配送業者情報を取得 |
||
465 | * |
||
466 | * @param $productTypes |
||
467 | * @return array |
||
468 | */ |
||
469 | 3 | public function getDeliveries($productTypes) |
|
491 | |||
492 | |||
493 | /** |
||
494 | * お届け先情報を作成 |
||
495 | * |
||
496 | * @param Order $Order |
||
497 | * @param Customer $Customer |
||
498 | * @param $deliveries |
||
499 | * @return Order |
||
500 | */ |
||
501 | public function getNewShipping(Order $Order, Customer $Customer, $deliveries) |
||
525 | |||
526 | /** |
||
527 | * お届け先情報を作成 |
||
528 | * |
||
529 | * @param \Eccube\Entity\Shipping $Shipping |
||
530 | * @param \Eccube\Entity\Customer|null $Customer |
||
531 | * @return \Eccube\Entity\Shipping |
||
532 | */ |
||
533 | 1 | public function copyToShippingFromCustomer(Shipping $Shipping, Customer $Customer = null) |
|
586 | |||
587 | |||
588 | /** |
||
589 | * 受注明細情報、配送商品情報を作成 |
||
590 | * |
||
591 | * @param Order $Order |
||
592 | * @return Order |
||
593 | */ |
||
594 | public function getNewDetails(Order $Order) |
||
613 | |||
614 | /** |
||
615 | * 配送商品情報を作成 |
||
616 | * |
||
617 | * @param Order $Order |
||
618 | * @param Product $Product |
||
619 | * @param ProductClass $ProductClass |
||
620 | * @param $quantity |
||
621 | * @return \Eccube\Entity\OrderItem |
||
622 | */ |
||
623 | public function getNewOrderItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) |
||
677 | |||
678 | /** |
||
679 | * お届け先ごとの送料合計を取得 |
||
680 | * |
||
681 | * @param $shippings |
||
682 | * @return int |
||
683 | */ |
||
684 | public function getShippingDeliveryFeeTotal($shippings) |
||
694 | |||
695 | /** |
||
696 | * 商品ごとの配送料を取得 |
||
697 | * |
||
698 | * @param Shipping $Shipping |
||
699 | * @return int |
||
700 | */ |
||
701 | public function getProductDeliveryFee(Shipping $Shipping) |
||
711 | |||
712 | /** |
||
713 | * 住所などの情報が変更された時に金額の再計算を行う |
||
714 | * @deprecated PurchaseFlowで行う |
||
715 | * @param Order $Order |
||
716 | * @return Order |
||
717 | */ |
||
718 | 1 | public function getAmount(Order $Order) |
|
740 | |||
741 | /** |
||
742 | * 配送料金の設定 |
||
743 | * |
||
744 | * @param Shipping $Shipping |
||
745 | * @param Delivery|null $Delivery |
||
746 | */ |
||
747 | public function setShippingDeliveryFee(Shipping $Shipping, Delivery $Delivery = null) |
||
769 | |||
770 | /** |
||
771 | * 配送料無料条件(合計金額)の条件を満たしていれば配送料金を0に設定 |
||
772 | * |
||
773 | * @param Order $Order |
||
774 | */ |
||
775 | 4 | View Code Duplication | public function setDeliveryFreeAmount(Order $Order) |
791 | |||
792 | /** |
||
793 | * 配送料無料条件(合計数量)の条件を満たしていれば配送料金を0に設定 |
||
794 | * |
||
795 | * @param Order $Order |
||
796 | */ |
||
797 | 3 | View Code Duplication | public function setDeliveryFreeQuantity(Order $Order) |
813 | |||
814 | /** |
||
815 | * 受注情報、お届け先情報の更新 |
||
816 | * |
||
817 | * @param Order $Order 受注情報 |
||
818 | * @param $data フォームデータ |
||
819 | * |
||
820 | * @deprecated since 3.0.5, to be removed in 3.1 |
||
821 | */ |
||
822 | public function setOrderUpdate(Order $Order, $data) |
||
855 | |||
856 | |||
857 | /** |
||
858 | * 受注情報の更新 |
||
859 | * |
||
860 | * @param Order $Order 受注情報 |
||
861 | */ |
||
862 | 3 | public function setOrderUpdateData(Order $Order) |
|
870 | |||
871 | |||
872 | /** |
||
873 | * 会員情報の更新 |
||
874 | * |
||
875 | * @param Order $Order 受注情報 |
||
876 | * @param Customer $user ログインユーザ |
||
877 | */ |
||
878 | 2 | public function setCustomerUpdate(Order $Order, Customer $user) |
|
891 | |||
892 | |||
893 | /** |
||
894 | * 支払方法選択の表示設定 |
||
895 | * |
||
896 | * @param $payments 支払選択肢情報 |
||
897 | * @param $subTotal 小計 |
||
898 | * @return array |
||
899 | */ |
||
900 | 1 | public function getPayments($payments, $subTotal) |
|
918 | |||
919 | /** |
||
920 | * お届け日を取得 |
||
921 | * |
||
922 | * @param Order $Order |
||
923 | * @return array |
||
924 | */ |
||
925 | 2 | public function getFormDeliveryDates(Order $Order) |
|
972 | |||
973 | /** |
||
974 | * 支払方法を取得 |
||
975 | * |
||
976 | * @param $deliveries |
||
977 | * @param Order $Order |
||
978 | * @return array |
||
979 | */ |
||
980 | public function getFormPayments($deliveries, Order $Order) |
||
1001 | |||
1002 | /** |
||
1003 | * お届け先ごとにFormを作成 |
||
1004 | * |
||
1005 | * @param Order $Order |
||
1006 | * @return \Symfony\Component\Form\Form |
||
1007 | * @deprecated since 3.0, to be removed in 3.1 |
||
1008 | */ |
||
1009 | View Code Duplication | public function getShippingForm(Order $Order) |
|
1035 | |||
1036 | /** |
||
1037 | * お届け先ごとにFormBuilderを作成 |
||
1038 | * |
||
1039 | * @param Order $Order |
||
1040 | * @return \Symfony\Component\Form\FormBuilderInterface |
||
1041 | * |
||
1042 | * @deprecated 利用している箇所なし |
||
1043 | */ |
||
1044 | View Code Duplication | public function getShippingFormBuilder(Order $Order) |
|
1068 | |||
1069 | |||
1070 | /** |
||
1071 | * フォームデータを更新 |
||
1072 | * |
||
1073 | * @param Order $Order |
||
1074 | * @param array $data |
||
1075 | * |
||
1076 | * @deprecated |
||
1077 | */ |
||
1078 | 1 | public function setFormData(Order $Order, array $data) |
|
1101 | |||
1102 | /** |
||
1103 | * 配送料の合計金額を計算 |
||
1104 | * |
||
1105 | * @param Order $Order |
||
1106 | * @return Order |
||
1107 | */ |
||
1108 | 2 | public function calculateDeliveryFee(Order $Order) |
|
1127 | |||
1128 | |||
1129 | /** |
||
1130 | * 購入処理を行う |
||
1131 | * |
||
1132 | * @param Order $Order |
||
1133 | * @throws ShoppingException |
||
1134 | */ |
||
1135 | 2 | public function processPurchase(Order $Order) |
|
1149 | |||
1150 | |||
1151 | /** |
||
1152 | * 値引き可能かチェック |
||
1153 | * |
||
1154 | * @param Order $Order |
||
1155 | * @param $discount |
||
1156 | * @return bool |
||
1157 | */ |
||
1158 | public function isDiscount(Order $Order, $discount) |
||
1167 | |||
1168 | |||
1169 | /** |
||
1170 | * 値引き金額をセット |
||
1171 | * |
||
1172 | * @param Order $Order |
||
1173 | * @param $discount |
||
1174 | */ |
||
1175 | public function setDiscount(Order $Order, $discount) |
||
1181 | |||
1182 | |||
1183 | /** |
||
1184 | * 合計金額を計算 |
||
1185 | * |
||
1186 | * @param Order $Order |
||
1187 | * @return Order |
||
1188 | */ |
||
1189 | 1 | public function calculatePrice(Order $Order) |
|
1205 | |||
1206 | /** |
||
1207 | * 受注ステータスをセット |
||
1208 | * |
||
1209 | * @param Order $Order |
||
1210 | * @param $status |
||
1211 | * @return Order |
||
1212 | */ |
||
1213 | 3 | public function setOrderStatus(Order $Order, $status) |
|
1230 | |||
1231 | /** |
||
1232 | * 受注メール送信を行う |
||
1233 | * |
||
1234 | * @param Order $Order |
||
1235 | * @return MailHistory |
||
1236 | */ |
||
1237 | 2 | public function sendOrderMail(Order $Order) |
|
1257 | |||
1258 | |||
1259 | /** |
||
1260 | * 受注処理完了通知 |
||
1261 | * |
||
1262 | * @param Order $Order |
||
1263 | */ |
||
1264 | public function notifyComplete(Order $Order) |
||
1276 | |||
1277 | |||
1278 | } |
||
1279 |