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 |
||
| 31 | class CartController extends AbstractController |
||
|
|
|||
| 32 | { |
||
| 33 | 1 | public function index(Application $app) |
|
| 34 | { |
||
| 35 | $Cart = $app['eccube.service.cart']->getCart(); |
||
| 36 | |||
| 37 | /* @var $BaseInfo \Eccube\Entity\BaseInfo */ |
||
| 38 | /* @var $Cart \Eccube\Entity\Cart */ |
||
| 39 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 40 | |||
| 41 | 1 | $isDeliveryFree = false; |
|
| 42 | 1 | $least = 0; |
|
| 43 | 1 | $quantity = 0; |
|
| 44 | if ($BaseInfo->getDeliveryFreeAmount()) { |
||
| 45 | if ($BaseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) { |
||
| 46 | // 送料無料(金額)を超えている |
||
| 47 | $isDeliveryFree = true; |
||
| 48 | } else { |
||
| 49 | $least = $BaseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice(); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | if ($BaseInfo->getDeliveryFreeQuantity()) { |
||
| 54 | if ($BaseInfo->getDeliveryFreeQuantity() <= $Cart->getTotalQuantity()) { |
||
| 55 | // 送料無料(個数)を超えている |
||
| 56 | $isDeliveryFree = true; |
||
| 57 | } else { |
||
| 58 | $quantity = $BaseInfo->getDeliveryFreeQuantity() - $Cart->getTotalQuantity(); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | return $app->render( |
||
| 63 | 1 | 'Cart/index.twig', |
|
| 64 | array( |
||
| 65 | 'Cart' => $Cart, |
||
| 66 | 'least' => $least, |
||
| 67 | 'quantity' => $quantity, |
||
| 68 | 'is_delivery_free' => $isDeliveryFree, |
||
| 69 | 1 | ) |
|
| 70 | ); |
||
| 71 | 1 | } |
|
| 72 | |||
| 73 | 21 | public function add(Application $app, Request $request) |
|
| 74 | { |
||
| 75 | $productClassId = $request->get('product_class_id'); |
||
| 76 | $quantity = $request->request->has('quantity') ? $request->get('quantity') : 1; |
||
| 77 | try { |
||
| 78 | $app['eccube.service.cart']->addProduct($productClassId, $quantity)->save(); |
||
| 79 | } catch (CartException $e) { |
||
| 80 | $app->addRequestError($e->getMessage()); |
||
| 81 | 21 | } |
|
| 82 | |||
| 83 | return $app->redirect($app->url('cart')); |
||
| 84 | 21 | } |
|
| 85 | |||
| 86 | 1 | View Code Duplication | public function up(Application $app, $productClassId) |
| 98 | |||
| 99 | 1 | View Code Duplication | public function down(Application $app, $productClassId) |
| 111 | |||
| 112 | 1 | public function remove(Application $app, $productClassId) |
|
| 120 | |||
| 121 | 1 | public function setQuantity(Application $app, $productClassId, $quantity) |
|
| 129 | |||
| 130 | public function buystep(Application $app) |
||
| 137 | } |
||
| 138 |