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 |
||
| 34 | class MypageController extends AbstractController |
||
|
|
|||
| 35 | { |
||
| 36 | 2 | public function login(Application $app, Request $request) |
|
| 37 | { |
||
| 38 | if ($app->isGranted('IS_AUTHENTICATED_FULLY')) { |
||
| 39 | return $app->redirect($app->url('mypage')); |
||
| 40 | } |
||
| 41 | |||
| 42 | /* @var $form \Symfony\Component\Form\FormInterface */ |
||
| 43 | $builder = $app['form.factory'] |
||
| 44 | ->createNamedBuilder('', 'customer_login'); |
||
| 45 | |||
| 46 | View Code Duplication | if ($app->isGranted('IS_AUTHENTICATED_REMEMBERED')) { |
|
| 47 | $Customer = $app->user(); |
||
| 48 | if ($Customer) { |
||
| 49 | $builder->get('login_email')->setData($Customer->getEmail()); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | $form = $builder->getForm(); |
||
| 54 | |||
| 55 | 1 | return $app->render('Mypage/login.twig', array( |
|
| 56 | 'error' => $app['security.last_error']($request), |
||
| 57 | 1 | 'form' => $form->createView(), |
|
| 58 | )); |
||
| 59 | 2 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param Application $app |
||
| 63 | * @param Request $request |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | 1 | public function index(Application $app, Request $request) |
|
| 68 | { |
||
| 69 | $Customer = $app['user']; |
||
| 70 | |||
| 71 | $app['orm.em'] |
||
| 72 | ->getFilters() |
||
| 73 | ->enable('incomplete_order_status_hidden'); |
||
| 74 | |||
| 75 | // paginator |
||
| 76 | $qb = $app['eccube.repository.order']->getQueryBuilderByCustomer($Customer); |
||
| 77 | $pagination = $app['paginator']()->paginate( |
||
| 78 | $qb, |
||
| 79 | 1 | $request->get('pageno', 1), |
|
| 80 | 1 | $app['config']['search_pmax'] |
|
| 81 | ); |
||
| 82 | |||
| 83 | 1 | return $app->render('Mypage/index.twig', array( |
|
| 84 | 'pagination' => $pagination, |
||
| 85 | )); |
||
| 86 | 1 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param Application $app |
||
| 90 | * @param Request $request |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 2 | public function history(Application $app, Request $request, $id) |
|
| 95 | { |
||
| 96 | |||
| 97 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
| 98 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
||
| 99 | $softDeleteFilter->setExcludes(array( |
||
| 100 | 'Eccube\Entity\ProductClass', |
||
| 101 | )); |
||
| 102 | |||
| 103 | $Order = $app['eccube.repository.order']->findOneBy(array( |
||
| 104 | 'id' => $id, |
||
| 105 | 2 | 'Customer' => $app->user(), |
|
| 106 | )); |
||
| 107 | 2 | if (!$Order) { |
|
| 108 | throw new NotFoundHttpException(); |
||
| 109 | } |
||
| 110 | |||
| 111 | 1 | return $app->render('Mypage/history.twig', array( |
|
| 112 | 'Order' => $Order, |
||
| 113 | )); |
||
| 114 | 2 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param Application $app |
||
| 118 | * @param Request $request |
||
| 119 | * @param id $id |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | 1 | public function order(Application $app, Request $request, $id) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param Application $app |
||
| 156 | * @param Request $request |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | 1 | public function favorite(Application $app, Request $request) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param Application $app |
||
| 185 | * @param Request $request |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 1 | public function delete(Application $app, $id) |
|
| 202 | } |
||
| 203 |