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 EditController 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 EditController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class EditController extends AbstractController |
||
55 | { |
||
56 | /** |
||
57 | * @var TaxRuleService |
||
58 | */ |
||
59 | protected $taxRuleService; |
||
60 | |||
61 | /** |
||
62 | * @var DeviceTypeRepository |
||
63 | */ |
||
64 | protected $deviceTypeRepository; |
||
65 | |||
66 | /** |
||
67 | * @var ProductRepository |
||
68 | */ |
||
69 | protected $productRepository; |
||
70 | |||
71 | /** |
||
72 | * @var CategoryRepository |
||
73 | */ |
||
74 | protected $categoryRepository; |
||
75 | |||
76 | /** |
||
77 | * @var CustomerRepository |
||
78 | */ |
||
79 | protected $customerRepository; |
||
80 | |||
81 | /** |
||
82 | * @var Serializer |
||
83 | */ |
||
84 | protected $serializer; |
||
85 | |||
86 | /** |
||
87 | * @var DeliveryRepository |
||
88 | */ |
||
89 | protected $deliveryRepository; |
||
90 | |||
91 | /** |
||
92 | * @var PurchaseFlow |
||
93 | */ |
||
94 | protected $purchaseFlow; |
||
95 | |||
96 | /** |
||
97 | * @var OrderRepository |
||
98 | */ |
||
99 | protected $orderRepository; |
||
100 | |||
101 | /** |
||
102 | * @var OrderNoProcessor |
||
103 | */ |
||
104 | protected $orderNoProcessor; |
||
105 | |||
106 | /** |
||
107 | * @var OrderItemTypeRepository |
||
108 | */ |
||
109 | protected $orderItemTypeRepository; |
||
110 | |||
111 | /** |
||
112 | * @var OrderStateMachine |
||
113 | */ |
||
114 | protected $orderStateMachine; |
||
115 | |||
116 | /** |
||
117 | * @var OrderStatusRepository |
||
118 | */ |
||
119 | protected $orderStatusRepository; |
||
120 | |||
121 | /** |
||
122 | * EditController constructor. |
||
123 | * |
||
124 | * @param TaxRuleService $taxRuleService |
||
125 | * @param DeviceTypeRepository $deviceTypeRepository |
||
126 | * @param ProductRepository $productRepository |
||
127 | * @param CategoryRepository $categoryRepository |
||
128 | * @param CustomerRepository $customerRepository |
||
129 | * @param SerializerInterface $serializer |
||
130 | * @param DeliveryRepository $deliveryRepository |
||
131 | * @param PurchaseFlow $orderPurchaseFlow |
||
132 | * @param OrderRepository $orderRepository |
||
133 | * @param OrderNoProcessor $orderNoProcessor |
||
134 | */ |
||
135 | 11 | public function __construct( |
|
164 | |||
165 | /** |
||
166 | * 受注登録/編集画面. |
||
167 | * |
||
168 | * @Route("/%eccube_admin_route%/order/new", name="admin_order_new") |
||
169 | * @Route("/%eccube_admin_route%/order/{id}/edit", requirements={"id" = "\d+"}, name="admin_order_edit") |
||
170 | * @Template("@admin/Order/edit.twig") |
||
171 | */ |
||
172 | 8 | public function index(Request $request, $id = null) |
|
173 | { |
||
174 | 8 | $TargetOrder = null; |
|
175 | 8 | $OriginOrder = null; |
|
176 | |||
177 | 8 | if (null === $id) { |
|
178 | // 空のエンティティを作成. |
||
179 | 3 | $TargetOrder = new Order(); |
|
180 | 3 | $TargetOrder->addShipping((new Shipping())->setOrder($TargetOrder)); |
|
181 | } else { |
||
182 | 5 | $TargetOrder = $this->orderRepository->find($id); |
|
183 | 5 | if (null === $TargetOrder) { |
|
184 | throw new NotFoundHttpException(); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // 編集前の受注情報を保持 |
||
189 | 8 | $OriginOrder = clone $TargetOrder; |
|
190 | 8 | $OriginItems = new ArrayCollection(); |
|
191 | 8 | foreach ($TargetOrder->getOrderItems() as $Item) { |
|
192 | 5 | $OriginItems->add($Item); |
|
193 | } |
||
194 | |||
195 | 8 | $builder = $this->formFactory->createBuilder(OrderType::class, $TargetOrder); |
|
196 | |||
197 | 8 | $event = new EventArgs( |
|
198 | [ |
||
199 | 8 | 'builder' => $builder, |
|
200 | 8 | 'OriginOrder' => $OriginOrder, |
|
201 | 8 | 'TargetOrder' => $TargetOrder, |
|
202 | ], |
||
203 | 8 | $request |
|
204 | ); |
||
205 | 8 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_INITIALIZE, $event); |
|
206 | |||
207 | 8 | $form = $builder->getForm(); |
|
208 | |||
209 | 8 | $form->handleRequest($request); |
|
210 | 8 | $purchaseContext = new PurchaseContext($OriginOrder, $OriginOrder->getCustomer()); |
|
211 | |||
212 | 8 | if ($form->isSubmitted() && $form['OrderItems']->isValid()) { |
|
213 | 6 | $event = new EventArgs( |
|
214 | [ |
||
215 | 6 | 'builder' => $builder, |
|
216 | 6 | 'OriginOrder' => $OriginOrder, |
|
217 | 6 | 'TargetOrder' => $TargetOrder, |
|
218 | 6 | 'PurchaseContext' => $purchaseContext, |
|
219 | ], |
||
220 | 6 | $request |
|
221 | ); |
||
222 | 6 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS, $event); |
|
223 | |||
224 | 6 | $flowResult = $this->purchaseFlow->validate($TargetOrder, $purchaseContext); |
|
225 | |||
226 | 6 | if ($flowResult->hasWarning()) { |
|
227 | foreach ($flowResult->getWarning() as $warning) { |
||
228 | $this->addWarning($warning->getMessage(), 'admin'); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | 6 | if ($flowResult->hasError()) { |
|
233 | 1 | foreach ($flowResult->getErrors() as $error) { |
|
234 | 1 | $this->addError($error->getMessage(), 'admin'); |
|
235 | } |
||
236 | } |
||
237 | |||
238 | // 登録ボタン押下 |
||
239 | 6 | switch ($request->get('mode')) { |
|
240 | case 'register': |
||
241 | 6 | log_info('受注登録開始', [$TargetOrder->getId()]); |
|
242 | |||
243 | 6 | if (!$flowResult->hasError() && $form->isValid()) { |
|
244 | try { |
||
245 | 5 | $this->purchaseFlow->prepare($TargetOrder, $purchaseContext); |
|
246 | 5 | $this->purchaseFlow->commit($TargetOrder, $purchaseContext); |
|
247 | } catch (PurchaseException $e) { |
||
248 | $this->addError($e->getMessage(), 'admin'); |
||
249 | break; |
||
250 | } |
||
251 | |||
252 | 5 | $OldStatus = $OriginOrder->getOrderStatus(); |
|
253 | 5 | $NewStatus = $TargetOrder->getOrderStatus(); |
|
254 | |||
255 | // ステータスが変更されている場合はステートマシンを実行. |
||
256 | 5 | if ($TargetOrder->getId() && $OldStatus->getId() != $NewStatus->getId()) { |
|
257 | // 発送済に変更された場合は, 発送日をセットする. |
||
258 | 3 | if ($NewStatus->getId() == OrderStatus::DELIVERED) { |
|
259 | $TargetOrder->getShippings()->map(function (Shipping $Shipping) { |
||
260 | if (!$Shipping->isShipped()) { |
||
261 | $Shipping->setShippingDate(new \DateTime()); |
||
262 | } |
||
263 | }); |
||
264 | } |
||
265 | // ステートマシンでステータスは更新されるので, 古いステータスに戻す. |
||
266 | 3 | $TargetOrder->setOrderStatus($OldStatus); |
|
267 | // FormTypeでステータスの遷移チェックは行っているのでapplyのみ実行. |
||
268 | 3 | $this->orderStateMachine->apply($TargetOrder, $NewStatus); |
|
269 | } |
||
270 | |||
271 | 5 | $this->entityManager->persist($TargetOrder); |
|
272 | 5 | $this->entityManager->flush(); |
|
273 | |||
274 | 5 | foreach ($OriginItems as $Item) { |
|
275 | 3 | if ($TargetOrder->getOrderItems()->contains($Item) === false) { |
|
276 | 3 | $this->entityManager->remove($Item); |
|
277 | } |
||
278 | } |
||
279 | 5 | $this->entityManager->flush(); |
|
280 | |||
281 | // 新規登録時はMySQL対応のためflushしてから採番 |
||
282 | 5 | $this->orderNoProcessor->process($TargetOrder, $purchaseContext); |
|
283 | 5 | $this->entityManager->flush(); |
|
284 | |||
285 | // 会員の場合、購入回数、購入金額などを更新 |
||
286 | 5 | if ($Customer = $TargetOrder->getCustomer()) { |
|
287 | 5 | $this->orderRepository->updateOrderSummary($Customer); |
|
288 | 5 | $this->entityManager->flush($Customer); |
|
289 | } |
||
290 | |||
291 | 5 | $event = new EventArgs( |
|
292 | [ |
||
293 | 5 | 'form' => $form, |
|
294 | 5 | 'OriginOrder' => $OriginOrder, |
|
295 | 5 | 'TargetOrder' => $TargetOrder, |
|
296 | 5 | 'Customer' => $Customer, |
|
297 | ], |
||
298 | 5 | $request |
|
299 | ); |
||
300 | 5 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE, $event); |
|
301 | |||
302 | 5 | $this->addSuccess('admin.order.save.complete', 'admin'); |
|
303 | |||
304 | 5 | log_info('受注登録完了', [$TargetOrder->getId()]); |
|
305 | |||
306 | 5 | return $this->redirectToRoute('admin_order_edit', ['id' => $TargetOrder->getId()]); |
|
307 | } |
||
308 | |||
309 | 1 | break; |
|
310 | default: |
||
311 | break; |
||
312 | } |
||
313 | } |
||
314 | |||
315 | // 会員検索フォーム |
||
316 | 3 | $builder = $this->formFactory |
|
317 | 3 | ->createBuilder(SearchCustomerType::class); |
|
318 | |||
319 | 3 | $event = new EventArgs( |
|
320 | [ |
||
321 | 3 | 'builder' => $builder, |
|
322 | 3 | 'OriginOrder' => $OriginOrder, |
|
323 | 3 | 'TargetOrder' => $TargetOrder, |
|
324 | ], |
||
325 | 3 | $request |
|
326 | ); |
||
327 | 3 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_CUSTOMER_INITIALIZE, $event); |
|
328 | |||
329 | 3 | $searchCustomerModalForm = $builder->getForm(); |
|
330 | |||
331 | // 商品検索フォーム |
||
332 | 3 | $builder = $this->formFactory |
|
333 | 3 | ->createBuilder(SearchProductType::class); |
|
334 | |||
335 | 3 | $event = new EventArgs( |
|
336 | [ |
||
337 | 3 | 'builder' => $builder, |
|
338 | 3 | 'OriginOrder' => $OriginOrder, |
|
339 | 3 | 'TargetOrder' => $TargetOrder, |
|
340 | ], |
||
341 | 3 | $request |
|
342 | ); |
||
343 | 3 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_EDIT_SEARCH_PRODUCT_INITIALIZE, $event); |
|
344 | |||
345 | 3 | $searchProductModalForm = $builder->getForm(); |
|
346 | |||
347 | // 配送業者のお届け時間 |
||
348 | 3 | $times = []; |
|
349 | 3 | $deliveries = $this->deliveryRepository->findAll(); |
|
350 | 3 | View Code Duplication | foreach ($deliveries as $Delivery) { |
351 | 3 | $deliveryTiems = $Delivery->getDeliveryTimes(); |
|
352 | 3 | foreach ($deliveryTiems as $DeliveryTime) { |
|
353 | 3 | $times[$Delivery->getId()][$DeliveryTime->getId()] = $DeliveryTime->getDeliveryTime(); |
|
354 | } |
||
355 | } |
||
356 | |||
357 | return [ |
||
358 | 3 | 'form' => $form->createView(), |
|
359 | 3 | 'searchCustomerModalForm' => $searchCustomerModalForm->createView(), |
|
360 | 3 | 'searchProductModalForm' => $searchProductModalForm->createView(), |
|
361 | 3 | 'Order' => $TargetOrder, |
|
362 | 3 | 'id' => $id, |
|
363 | 3 | 'shippingDeliveryTimes' => $this->serializer->serialize($times, 'json'), |
|
364 | ]; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * 顧客情報を検索する. |
||
369 | * |
||
370 | * @Route("/%eccube_admin_route%/order/search/customer/html", name="admin_order_search_customer_html") |
||
371 | * @Route("/%eccube_admin_route%/order/search/customer/html/page/{page_no}", requirements={"page_No" = "\d+"}, name="admin_order_search_customer_html_page") |
||
372 | * @Template("@admin/Order/search_customer.twig") |
||
373 | * |
||
374 | * @param Request $request |
||
375 | * @param integer $page_no |
||
376 | * |
||
377 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
378 | */ |
||
379 | 1 | public function searchCustomerHtml(Request $request, $page_no = null, Paginator $paginator) |
|
462 | |||
463 | /** |
||
464 | * 顧客情報を検索する. |
||
465 | * |
||
466 | * @Method("POST") |
||
467 | * @Route("/%eccube_admin_route%/order/search/customer/id", name="admin_order_search_customer_by_id") |
||
468 | * |
||
469 | * @param Request $request |
||
470 | * |
||
471 | * @return \Symfony\Component\HttpFoundation\JsonResponse |
||
472 | */ |
||
473 | 1 | public function searchCustomerById(Request $request) |
|
526 | |||
527 | /** |
||
528 | * @Route("/%eccube_admin_route%/order/search/product", name="admin_order_search_product") |
||
529 | * @Route("/%eccube_admin_route%/order/search/product/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_order_search_product_page") |
||
530 | * @Template("@admin/Order/search_product.twig") |
||
531 | */ |
||
532 | 1 | public function searchProduct(Request $request, $page_no = null, Paginator $paginator) |
|
616 | |||
617 | /** |
||
618 | * その他明細情報を取得 |
||
619 | * |
||
620 | * @Route("/%eccube_admin_route%/order/search/order_item_type", name="admin_order_search_order_item_type") |
||
621 | * @Template("@admin/Order/order_item_type.twig") |
||
622 | * |
||
623 | * @param Request $request |
||
624 | * |
||
625 | * @return array |
||
626 | */ |
||
627 | public function searchOrderItemType(Request $request) |
||
656 | } |
||
657 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.