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 |
||
43 | class OrderHelper |
||
44 | { |
||
45 | // FIXME 必要なメソッドのみ移植する |
||
46 | use ControllerTrait; |
||
47 | |||
48 | /** |
||
49 | * @var ContainerInterface |
||
50 | */ |
||
51 | protected $container; |
||
52 | |||
53 | /** |
||
54 | * @var string 非会員情報を保持するセッションのキー |
||
55 | */ |
||
56 | const SESSION_NON_MEMBER = 'eccube.front.shopping.nonmember'; |
||
57 | |||
58 | /** |
||
59 | * @var string 非会員の住所情報を保持するセッションのキー |
||
60 | */ |
||
61 | const SESSION_NON_MEMBER_ADDRESSES = 'eccube.front.shopping.nonmember.customeraddress'; |
||
62 | |||
63 | /** |
||
64 | * @var string 受注IDを保持するセッションのキー |
||
65 | */ |
||
66 | const SESSION_ORDER_ID = 'eccube.front.shopping.order.id'; |
||
67 | |||
68 | /** |
||
69 | * @var string カートが分割されているかどうかのフラグ. 購入フローからのログイン時にカートが分割された場合にtrueがセットされる. |
||
70 | * |
||
71 | * @see SecurityListener |
||
72 | */ |
||
73 | const SESSION_CART_DIVIDE_FLAG = 'eccube.front.cart.divide'; |
||
74 | |||
75 | /** |
||
76 | * @var SessionInterface |
||
77 | */ |
||
78 | protected $session; |
||
79 | |||
80 | /** |
||
81 | * @var PrefRepository |
||
82 | */ |
||
83 | protected $prefRepository; |
||
84 | |||
85 | /** |
||
86 | * @var OrderRepository |
||
87 | */ |
||
88 | protected $orderRepository; |
||
89 | |||
90 | /** |
||
91 | * @var OrderItemTypeRepository |
||
92 | */ |
||
93 | protected $orderItemTypeRepository; |
||
94 | |||
95 | /** |
||
96 | * @var OrderStatusRepository |
||
97 | */ |
||
98 | protected $orderStatusRepository; |
||
99 | |||
100 | /** |
||
101 | * @var DeliveryRepository |
||
102 | */ |
||
103 | protected $deliveryRepository; |
||
104 | |||
105 | /** |
||
106 | * @var PaymentRepository |
||
107 | */ |
||
108 | protected $paymentRepository; |
||
109 | |||
110 | /** |
||
111 | * @var DeviceTypeRepository |
||
112 | */ |
||
113 | protected $deviceTypeRepository; |
||
114 | |||
115 | 175 | /** |
|
116 | * @var MobileDetector |
||
117 | */ |
||
118 | protected $mobileDetector; |
||
119 | |||
120 | /** |
||
121 | * @var EntityManagerInterface |
||
122 | */ |
||
123 | protected $entityManager; |
||
124 | |||
125 | public function __construct( |
||
150 | |||
151 | 51 | /** |
|
152 | 51 | * 購入処理中の受注を生成する. |
|
153 | * |
||
154 | 51 | * @param Customer $Customer |
|
155 | * @param $CartItems |
||
156 | 51 | * |
|
157 | * @return Order |
||
158 | */ |
||
159 | public function createPurchaseProcessingOrder(Cart $Cart, Customer $Customer) |
||
198 | |||
199 | 2 | /** |
|
200 | * @param Cart $Cart |
||
201 | 2 | * |
|
202 | 2 | * @return bool |
|
203 | */ |
||
204 | 2 | public function verifyCart(Cart $Cart) |
|
221 | 51 | ||
222 | /** |
||
223 | 51 | * 注文手続き画面でログインが必要かどうかの判定 |
|
224 | * |
||
225 | 51 | * @return bool |
|
226 | */ |
||
227 | public function isLoginRequired() |
||
246 | |||
247 | /** |
||
248 | * 購入処理中の受注を取得する. |
||
249 | * |
||
250 | * @param null|string $preOrderId |
||
251 | * |
||
252 | * @return null|Order |
||
253 | */ |
||
254 | public function getPurchaseProcessingOrder($preOrderId = null) |
||
265 | 51 | ||
266 | /** |
||
267 | 51 | * セッションに保持されている非会員情報を取得する. |
|
268 | * 非会員購入時に入力されたお客様情報を返す. |
||
269 | 51 | * |
|
270 | 51 | * @return Customer |
|
271 | 51 | */ |
|
272 | 51 | public function getNonMember() |
|
282 | 51 | ||
283 | 51 | /** |
|
284 | 42 | * @param Cart $Cart |
|
285 | 42 | * @param Customer $Customer |
|
286 | * |
||
287 | * @return Order|null |
||
288 | 51 | */ |
|
289 | 51 | public function initializeOrder(Cart $Cart, Customer $Customer) |
|
302 | 51 | ||
303 | 51 | public function removeSession() |
|
310 | 51 | ||
311 | /** |
||
312 | * 会員情報の更新日時が受注の作成日時よりも新しければ, 受注の注文者情報を更新する. |
||
313 | 51 | * |
|
314 | 51 | * @param Order $Order |
|
315 | * @param Customer $Customer |
||
316 | 51 | */ |
|
317 | 51 | public function updateCustomerInfo(Order $Order, Customer $Customer) |
|
323 | 51 | ||
324 | public function createPreOrderId() |
||
339 | 51 | ||
340 | 51 | protected function setCustomer(Order $Order, Customer $Customer) |
|
356 | 51 | ||
357 | 51 | /** |
|
358 | 51 | * @param Collection|ArrayCollection|CartItem[] $CartItems |
|
359 | * |
||
360 | * @return OrderItem[] |
||
361 | */ |
||
362 | 51 | protected function createOrderItemsFromCartItems($CartItems) |
|
397 | |||
398 | /** |
||
399 | * @param Customer $Customer |
||
400 | * |
||
401 | * @return Shipping |
||
402 | */ |
||
403 | protected function createShippingFromCustomer(Customer $Customer) |
||
420 | |||
421 | /** |
||
422 | * @param Shipping $Shipping |
||
423 | */ |
||
424 | protected function setDefaultDelivery(Shipping $Shipping) |
||
444 | |||
445 | /** |
||
446 | * @param Order $Order |
||
447 | */ |
||
448 | protected function setDefaultPayment(Order $Order) |
||
488 | |||
489 | /** |
||
490 | * @param Order $Order |
||
491 | * @param Shipping $Shipping |
||
492 | * @param array $OrderItems |
||
493 | */ |
||
494 | protected function addOrderItems(Order $Order, Shipping $Shipping, array $OrderItems) |
||
503 | } |
||
504 |
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.