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 OrderHelper 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 OrderHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class OrderHelper |
||
45 | { |
||
46 | // FIXME 必要なメソッドのみ移植する |
||
47 | use ControllerTrait; |
||
48 | |||
49 | /** |
||
50 | * @var ContainerInterface |
||
51 | */ |
||
52 | protected $container; |
||
53 | |||
54 | /** |
||
55 | * @var string 非会員情報を保持するセッションのキー |
||
56 | */ |
||
57 | const SESSION_NON_MEMBER = 'eccube.front.shopping.nonmember'; |
||
58 | |||
59 | /** |
||
60 | * @var string 非会員の住所情報を保持するセッションのキー |
||
61 | */ |
||
62 | const SESSION_NON_MEMBER_ADDRESSES = 'eccube.front.shopping.nonmember.customeraddress'; |
||
63 | |||
64 | /** |
||
65 | * @var string 受注IDを保持するセッションのキー |
||
66 | */ |
||
67 | const SESSION_ORDER_ID = 'eccube.front.shopping.order.id'; |
||
68 | |||
69 | /** |
||
70 | * @var string カートが分割されているかどうかのフラグ. 購入フローからのログイン時にカートが分割された場合にtrueがセットされる. |
||
71 | * |
||
72 | * @see SecurityListener |
||
73 | */ |
||
74 | const SESSION_CART_DIVIDE_FLAG = 'eccube.front.cart.divide'; |
||
75 | |||
76 | /** |
||
77 | * @var SessionInterface |
||
78 | */ |
||
79 | protected $session; |
||
80 | |||
81 | /** |
||
82 | * @var PrefRepository |
||
83 | */ |
||
84 | protected $prefRepository; |
||
85 | |||
86 | /** |
||
87 | * @var OrderRepository |
||
88 | */ |
||
89 | protected $orderRepository; |
||
90 | |||
91 | /** |
||
92 | * @var OrderItemTypeRepository |
||
93 | */ |
||
94 | protected $orderItemTypeRepository; |
||
95 | |||
96 | /** |
||
97 | * @var OrderStatusRepository |
||
98 | */ |
||
99 | protected $orderStatusRepository; |
||
100 | |||
101 | /** |
||
102 | * @var DeliveryRepository |
||
103 | */ |
||
104 | protected $deliveryRepository; |
||
105 | |||
106 | /** |
||
107 | * @var PaymentRepository |
||
108 | */ |
||
109 | protected $paymentRepository; |
||
110 | |||
111 | /** |
||
112 | * @var DeviceTypeRepository |
||
113 | */ |
||
114 | protected $deviceTypeRepository; |
||
115 | |||
116 | /** |
||
117 | * @var EntityManagerInterface |
||
118 | */ |
||
119 | protected $entityManager; |
||
120 | |||
121 | /** |
||
122 | * @var MobileDetector |
||
123 | */ |
||
124 | protected $mobileDetector; |
||
125 | |||
126 | /** |
||
127 | * @var TaxRuleRepository |
||
128 | */ |
||
129 | protected $taxRuleRepository; |
||
130 | |||
131 | public function __construct( |
||
158 | |||
159 | /** |
||
160 | * 購入処理中の受注を生成する. |
||
161 | * |
||
162 | * @param Customer $Customer |
||
163 | * @param $CartItems |
||
164 | * |
||
165 | * @return Order |
||
166 | */ |
||
167 | public function createPurchaseProcessingOrder(Cart $Cart, Customer $Customer) |
||
206 | |||
207 | /** |
||
208 | * @param Cart $Cart |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function verifyCart(Cart $Cart) |
||
229 | |||
230 | /** |
||
231 | * 注文手続き画面でログインが必要かどうかの判定 |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function isLoginRequired() |
||
254 | |||
255 | /** |
||
256 | * 購入処理中の受注を取得する. |
||
257 | * |
||
258 | * @param null|string $preOrderId |
||
259 | * |
||
260 | * @return null|Order |
||
261 | */ |
||
262 | public function getPurchaseProcessingOrder($preOrderId = null) |
||
273 | |||
274 | /** |
||
275 | * セッションに保持されている非会員情報を取得する. |
||
276 | * 非会員購入時に入力されたお客様情報を返す. |
||
277 | * |
||
278 | * @return Customer |
||
279 | */ |
||
280 | public function getNonMember() |
||
290 | |||
291 | /** |
||
292 | * @param Cart $Cart |
||
293 | * @param Customer $Customer |
||
294 | * |
||
295 | * @return Order|null |
||
296 | */ |
||
297 | public function initializeOrder(Cart $Cart, Customer $Customer) |
||
310 | |||
311 | public function removeSession() |
||
318 | |||
319 | /** |
||
320 | * 会員情報の更新日時が受注の作成日時よりも新しければ, 受注の注文者情報を更新する. |
||
321 | * |
||
322 | * @param Order $Order |
||
323 | * @param Customer $Customer |
||
324 | */ |
||
325 | public function updateCustomerInfo(Order $Order, Customer $Customer) |
||
331 | |||
332 | public function createPreOrderId() |
||
347 | |||
348 | protected function setCustomer(Order $Order, Customer $Customer) |
||
364 | |||
365 | /** |
||
366 | * @param Collection|ArrayCollection|CartItem[] $CartItems |
||
367 | * |
||
368 | * @return OrderItem[] |
||
369 | */ |
||
370 | protected function createOrderItemsFromCartItems($CartItems) |
||
411 | |||
412 | /** |
||
413 | * @param Customer $Customer |
||
414 | * |
||
415 | * @return Shipping |
||
416 | */ |
||
417 | protected function createShippingFromCustomer(Customer $Customer) |
||
434 | |||
435 | /** |
||
436 | * @param Shipping $Shipping |
||
437 | */ |
||
438 | protected function setDefaultDelivery(Shipping $Shipping) |
||
458 | |||
459 | /** |
||
460 | * @param Order $Order |
||
461 | */ |
||
462 | protected function setDefaultPayment(Order $Order) |
||
492 | |||
493 | /** |
||
494 | * @param Order $Order |
||
495 | * @param Shipping $Shipping |
||
496 | * @param array $OrderItems |
||
497 | */ |
||
498 | protected function addOrderItems(Order $Order, Shipping $Shipping, array $OrderItems) |
||
507 | } |
||
508 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.