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 |
||
40 | class ProductController extends AbstractController |
||
41 | { |
||
42 | /** |
||
43 | * @var PurchaseFlow |
||
44 | */ |
||
45 | protected $purchaseFlow; |
||
46 | |||
47 | /** |
||
48 | * @var CustomerFavoriteProductRepository |
||
49 | */ |
||
50 | protected $customerFavoriteProductRepository; |
||
51 | |||
52 | /** |
||
53 | * @var CartService |
||
54 | */ |
||
55 | protected $cartService; |
||
56 | |||
57 | /** |
||
58 | * @var ProductRepository |
||
59 | */ |
||
60 | protected $productRepository; |
||
61 | |||
62 | /** |
||
63 | * @var BaseInfo |
||
64 | */ |
||
65 | protected $BaseInfo; |
||
66 | |||
67 | /** |
||
68 | * @var AuthenticationUtils |
||
69 | */ |
||
70 | protected $helper; |
||
71 | |||
72 | private $title = ''; |
||
73 | |||
74 | /** |
||
75 | * ProductController constructor. |
||
76 | * |
||
77 | * @param PurchaseFlow $cartPurchaseFlow |
||
78 | * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository |
||
79 | * @param CartService $cartService |
||
80 | * @param ProductRepository $productRepository |
||
81 | * @param BaseInfoRepository $baseInfoRepository |
||
82 | * @param AuthenticationUtils $helper |
||
83 | */ |
||
84 | 46 | View Code Duplication | public function __construct( |
|
|||
85 | PurchaseFlow $cartPurchaseFlow, |
||
86 | CustomerFavoriteProductRepository $customerFavoriteProductRepository, |
||
87 | CartService $cartService, |
||
88 | ProductRepository $productRepository, |
||
89 | BaseInfoRepository $baseInfoRepository, |
||
90 | AuthenticationUtils $helper |
||
91 | ) { |
||
92 | 46 | $this->purchaseFlow = $cartPurchaseFlow; |
|
93 | 46 | $this->customerFavoriteProductRepository = $customerFavoriteProductRepository; |
|
94 | 46 | $this->cartService = $cartService; |
|
95 | 46 | $this->productRepository = $productRepository; |
|
96 | 46 | $this->BaseInfo = $baseInfoRepository->get(); |
|
97 | 46 | $this->helper = $helper; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * 商品一覧画面. |
||
102 | * |
||
103 | * @Route("/products/list", name="product_list") |
||
104 | * @Template("Product/list.twig") |
||
105 | */ |
||
106 | 3 | public function index(Request $request, Paginator $paginator) |
|
244 | |||
245 | /** |
||
246 | * 商品詳細画面. |
||
247 | * |
||
248 | * @Method("GET") |
||
249 | * @Route("/products/detail/{id}", name="product_detail", requirements={"id" = "\d+"}) |
||
250 | * @Template("Product/detail.twig") |
||
251 | * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"}) |
||
252 | * |
||
253 | * @param Request $request |
||
254 | * @param Product $Product |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | 11 | public function detail(Request $request, Product $Product) |
|
297 | |||
298 | /** |
||
299 | * お気に入り追加. |
||
300 | * |
||
301 | * @Route("/products/add_favorite/{id}", name="product_add_favorite", requirements={"id" = "\d+"}) |
||
302 | */ |
||
303 | 3 | public function addFavorite(Request $request, Product $Product) |
|
346 | |||
347 | /** |
||
348 | * カートに追加. |
||
349 | * |
||
350 | * @Method("POST") |
||
351 | * @Route("/products/add_cart/{id}", name="product_add_cart", requirements={"id" = "\d+"}) |
||
352 | */ |
||
353 | 37 | public function addCart(Request $request, Product $Product) |
|
354 | { |
||
355 | // エラーメッセージの配列 |
||
356 | 37 | $errorMessages = []; |
|
357 | 37 | if (!$this->checkVisibility($Product)) { |
|
358 | 1 | throw new NotFoundHttpException(); |
|
359 | } |
||
360 | |||
361 | 36 | $builder = $this->formFactory->createNamedBuilder( |
|
362 | 36 | '', |
|
363 | 36 | AddCartType::class, |
|
364 | 36 | null, |
|
365 | [ |
||
366 | 36 | 'product' => $Product, |
|
367 | 'id_add_product_id' => false, |
||
368 | ] |
||
369 | ); |
||
370 | |||
371 | 36 | $event = new EventArgs( |
|
372 | [ |
||
373 | 36 | 'builder' => $builder, |
|
374 | 36 | 'Product' => $Product, |
|
375 | ], |
||
376 | 36 | $request |
|
377 | ); |
||
378 | 36 | $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_CART_ADD_INITIALIZE, $event); |
|
379 | |||
380 | /* @var $form \Symfony\Component\Form\FormInterface */ |
||
381 | 36 | $form = $builder->getForm(); |
|
382 | 36 | $form->handleRequest($request); |
|
383 | |||
384 | 36 | if (!$form->isValid()) { |
|
385 | 1 | throw new NotFoundHttpException(); |
|
386 | } |
||
387 | |||
388 | 35 | $addCartData = $form->getData(); |
|
389 | |||
390 | 35 | log_info( |
|
391 | 35 | 'カート追加処理開始', |
|
392 | [ |
||
393 | 35 | 'product_id' => $Product->getId(), |
|
394 | 35 | 'product_class_id' => $addCartData['product_class_id'], |
|
395 | 35 | 'quantity' => $addCartData['quantity'], |
|
396 | ] |
||
397 | ); |
||
398 | |||
399 | // カートへ追加 |
||
400 | 35 | $this->cartService->addProduct($addCartData['product_class_id'], $addCartData['quantity']); |
|
401 | |||
402 | // 明細の正規化 |
||
403 | 35 | $Carts = $this->cartService->getCarts(); |
|
404 | 35 | View Code Duplication | foreach ($Carts as $Cart) { |
405 | 35 | $result = $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $this->getUser())); |
|
406 | // 復旧不可のエラーが発生した場合は追加した明細を削除. |
||
407 | 35 | if ($result->hasError()) { |
|
408 | $this->cartService->removeProduct($addCartData['product_class_id']); |
||
409 | foreach ($result->getErrors() as $error) { |
||
410 | $errorMessages[] = $error->getMessage(); |
||
411 | } |
||
412 | } |
||
413 | 35 | foreach ($result->getWarning() as $warning) { |
|
414 | 35 | $errorMessages[] = $warning->getMessage(); |
|
415 | } |
||
416 | } |
||
417 | |||
418 | 35 | $this->cartService->save(); |
|
419 | |||
420 | 35 | log_info( |
|
421 | 35 | 'カート追加処理完了', |
|
422 | [ |
||
423 | 35 | 'product_id' => $Product->getId(), |
|
424 | 35 | 'product_class_id' => $addCartData['product_class_id'], |
|
425 | 35 | 'quantity' => $addCartData['quantity'], |
|
426 | ] |
||
427 | ); |
||
428 | |||
429 | 35 | $event = new EventArgs( |
|
430 | [ |
||
431 | 35 | 'form' => $form, |
|
432 | 35 | 'Product' => $Product, |
|
433 | ], |
||
434 | 35 | $request |
|
435 | ); |
||
436 | 35 | $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE, $event); |
|
437 | |||
438 | 35 | if ($event->getResponse() !== null) { |
|
439 | return $event->getResponse(); |
||
440 | } |
||
441 | |||
442 | 35 | if ($request->isXmlHttpRequest()) { |
|
443 | // ajaxでのリクエストの場合は結果をjson形式で返す。 |
||
444 | |||
445 | // 初期化 |
||
446 | $done = null; |
||
447 | $messages = []; |
||
448 | |||
449 | if (empty($errorMessages)) { |
||
450 | // エラーが発生していない場合 |
||
451 | $done = true; |
||
452 | array_push($messages, 'カートに追加しました。'); |
||
453 | } else { |
||
454 | // エラーが発生している場合 |
||
455 | $done = false; |
||
456 | $messages = $errorMessages; |
||
457 | } |
||
458 | |||
459 | return $this->json(['done' => $done, 'messages' => $messages]); |
||
460 | } else { |
||
461 | // ajax以外でのリクエストの場合はカート画面へリダイレクト |
||
462 | 35 | foreach ($errorMessages as $errorMessage) { |
|
463 | 5 | $this->addRequestError($errorMessage); |
|
464 | } |
||
465 | |||
466 | 35 | return $this->redirectToRoute('cart'); |
|
467 | } |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * ページタイトルの設定 |
||
472 | * |
||
473 | * @param null|array $searchData |
||
474 | * |
||
475 | * @return str |
||
476 | */ |
||
477 | 3 | private function getPageTitle($searchData) |
|
487 | |||
488 | /** |
||
489 | * 閲覧可能な商品かどうかを判定 |
||
490 | * |
||
491 | * @param Product $Product |
||
492 | * |
||
493 | * @return boolean 閲覧可能な場合はtrue |
||
494 | */ |
||
495 | 43 | private function checkVisibility(Product $Product) |
|
515 | } |
||
516 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.