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:
1 | <?php |
||
38 | class OrderHelper |
||
39 | { |
||
40 | /** |
||
41 | * @Inject(OrderItemTypeRepository::class) |
||
42 | * @var OrderItemTypeRepository |
||
43 | */ |
||
44 | protected $orderItemTypeRepository; |
||
45 | |||
46 | /** |
||
47 | * @Inject(OrderStatusRepository::class) |
||
48 | * @var OrderStatusRepository |
||
49 | */ |
||
50 | protected $orderStatusRepository; |
||
51 | |||
52 | /** |
||
53 | * @Inject(TaxRuleRepository::class) |
||
54 | * @var TaxRuleRepository |
||
55 | */ |
||
56 | protected $taxRuleRepository; |
||
57 | |||
58 | /** |
||
59 | * @Inject(DeliveryFeeRepository::class) |
||
60 | * @var DeliveryFeeRepository |
||
61 | */ |
||
62 | protected $deliveryFeeRepository; |
||
63 | |||
64 | /** |
||
65 | * @Inject(DeliveryRepository::class) |
||
66 | * @var DeliveryRepository |
||
67 | */ |
||
68 | protected $deliveryRepository; |
||
69 | |||
70 | /** |
||
71 | * @Inject(PaymentRepository::class) |
||
72 | * @var PaymentRepository |
||
73 | */ |
||
74 | protected $paymentRepository; |
||
75 | |||
76 | /** |
||
77 | * @Inject(OrderRepository::class) |
||
78 | * @var OrderRepository |
||
79 | */ |
||
80 | protected $orderRepository; |
||
81 | |||
82 | /** |
||
83 | * @Inject(ShippingStatusRepository::class) |
||
84 | * @var ShippingStatusRepository |
||
85 | */ |
||
86 | protected $shippingStatusRepository; |
||
87 | |||
88 | /** |
||
89 | * @Inject("orm.em") |
||
90 | * @var EntityManager |
||
91 | */ |
||
92 | protected $entityManager; |
||
93 | |||
94 | /** |
||
95 | * @Inject("config") |
||
96 | * @var array |
||
97 | */ |
||
98 | protected $appConfig; |
||
99 | |||
100 | /** |
||
101 | * 購入処理中の受注データを生成する. |
||
102 | * |
||
103 | * @param Customer $Customer |
||
|
|||
104 | * @param CustomerAddress $CustomerAddress |
||
105 | * @param array $CartItems |
||
106 | * @return Order |
||
107 | */ |
||
108 | 11 | public function createProcessingOrder(Customer $Customer, CustomerAddress $CustomerAddress, $CartItems) |
|
142 | |||
143 | 11 | public function createPreOrderId() |
|
159 | |||
160 | 11 | public function setCustomer(Order $Order, Customer $Customer) |
|
176 | |||
177 | /** |
||
178 | * @param ArrayCollection $CartItems |
||
179 | * @return OrderItem[] |
||
180 | */ |
||
181 | 11 | private function createOrderItemsFromCartItems($CartItems) |
|
182 | { |
||
183 | 11 | $ProductItemType = $this->orderItemTypeRepository->find(OrderItemType::PRODUCT); |
|
184 | // TODO |
||
185 | 11 | $TaxExclude = $this->entityManager->getRepository(TaxDisplayType::class)->find(TaxDisplayType::EXCLUDED); |
|
186 | 11 | $Taxion = $this->entityManager->getRepository(TaxType::class)->find(TaxType::TAXATION); |
|
187 | |||
188 | 11 | return array_map(function($item) use ($ProductItemType, $TaxExclude, $Taxion) { |
|
189 | /* @var $item CartItem */ |
||
190 | /* @var $ProductClass \Eccube\Entity\ProductClass */ |
||
191 | 11 | $ProductClass = $item->getObject(); |
|
192 | /* @var $Product \Eccube\Entity\Product */ |
||
193 | 11 | $Product = $ProductClass->getProduct(); |
|
194 | 11 | $TaxRule = $this->taxRuleRepository->getByRule($Product, $ProductClass); |
|
195 | |||
196 | 11 | $OrderItem = new OrderItem(); |
|
197 | $OrderItem |
||
198 | 11 | ->setProduct($Product) |
|
199 | 11 | ->setProductClass($ProductClass) |
|
200 | 11 | ->setProductName($Product->getName()) |
|
201 | 11 | ->setProductCode($ProductClass->getCode()) |
|
202 | 11 | ->setPrice($ProductClass->getPrice02()) |
|
203 | 11 | ->setQuantity($item->getQuantity()) |
|
204 | 11 | ->setTaxRule($TaxRule->getId()) |
|
205 | 11 | ->setTaxRate($TaxRule->getTaxRate()) |
|
206 | 11 | ->setOrderItemType($ProductItemType) |
|
207 | 11 | ->setTaxDisplayType($TaxExclude) |
|
208 | 11 | ->setTaxType($Taxion); |
|
209 | |||
210 | 11 | $ClassCategory1 = $ProductClass->getClassCategory1(); |
|
211 | 11 | if (!is_null($ClassCategory1)) { |
|
212 | 11 | $OrderItem->setClasscategoryName1($ClassCategory1->getName()); |
|
213 | 11 | $OrderItem->setClassName1($ClassCategory1->getClassName()->getName()); |
|
214 | } |
||
215 | 11 | $ClassCategory2 = $ProductClass->getClassCategory2(); |
|
216 | 11 | if (!is_null($ClassCategory2)) { |
|
217 | 11 | $OrderItem->setClasscategoryName2($ClassCategory2->getName()); |
|
218 | 11 | $OrderItem->setClassName2($ClassCategory2->getClassName()->getName()); |
|
219 | } |
||
220 | |||
221 | 11 | return $OrderItem; |
|
222 | 11 | }, $CartItems->toArray()); |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * @deprecated |
||
227 | */ |
||
228 | public function createOrderDetailsFromCartItems($CartItems) |
||
267 | |||
268 | /** |
||
269 | * @deprecated |
||
270 | */ |
||
271 | public function addOrderDetails(Order $Order, array $OrderDetails) |
||
279 | |||
280 | 11 | public function createShippingFromCustomerAddress(CustomerAddress $CustomerAddress) |
|
307 | |||
308 | /** |
||
309 | * @deprecated |
||
310 | */ |
||
311 | public function addShipping(Order $Order, Shipping $Shipping) |
||
317 | |||
318 | 11 | public function setDefaultDelivery(Shipping $Shipping) |
|
341 | |||
342 | 11 | public function setDefaultPayment(Order $Order) |
|
372 | |||
373 | /** |
||
374 | * @deprecated |
||
375 | */ |
||
376 | public function createOrderItemsFromOrderDetails($OrderDetails, $groupByProductType = true) |
||
414 | |||
415 | 11 | public function addOrderItems(Order $Order, Shipping $Shipping, array $OrderItems) |
|
424 | |||
425 | } |
||
426 |