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 | ||
| 33 | class ChangeController extends AbstractController | ||
|  | |||
| 34 | { | ||
| 35 | /** | ||
| 36 | * 会員情報編集画面. | ||
| 37 | * | ||
| 38 | * @param Application $app | ||
| 39 | * @param Request $request | ||
| 40 | 4 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response | |
| 41 | */ | ||
| 42 | public function index(Application $app, Request $request) | ||
| 43 | 4 |     { | |
| 44 | $Customer = $app->user(); | ||
| 45 | $LoginCustomer = clone $Customer; | ||
| 46 | $app['orm.em']->detach($LoginCustomer); | ||
| 47 | |||
| 48 | $previous_password = $Customer->getPassword(); | ||
| 49 | $Customer->setPassword($app['config']['default_password']); | ||
| 50 | |||
| 51 | /* @var $builder \Symfony\Component\Form\FormBuilderInterface */ | ||
| 52 |         $builder = $app['form.factory']->createBuilder('entry', $Customer); | ||
| 53 | |||
| 54 | $event = new EventArgs( | ||
| 55 | array( | ||
| 56 | 'builder' => $builder, | ||
| 57 | 'Customer' => $Customer, | ||
| 58 | ), | ||
| 59 | $request | ||
| 60 | ); | ||
| 61 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_INITIALIZE, $event); | ||
| 62 | |||
| 63 | 1 | /* @var $form \Symfony\Component\Form\FormInterface */ | |
| 64 | $form = $builder->getForm(); | ||
| 65 | $form->handleRequest($request); | ||
| 66 | |||
| 67 | View Code Duplication |         if ($form->isSubmitted() && $form->isValid()) { | |
| 68 | |||
| 69 |             if ($Customer->getPassword() === $app['config']['default_password']) { | ||
| 70 | $Customer->setPassword($previous_password); | ||
| 71 | 2 |             } else { | |
| 72 | 2 | $Customer->setPassword( | |
| 73 | $app['eccube.repository.customer']->encryptPassword($app, $Customer) | ||
| 74 | 4 | ); | |
| 75 | } | ||
| 76 | $app['orm.em']->flush(); | ||
| 77 | |||
| 78 | $event = new EventArgs( | ||
| 79 | array( | ||
| 80 | 'form' => $form, | ||
| 81 | 'Customer' => $Customer, | ||
| 82 | ), | ||
| 83 | 1 | $request | |
| 84 | ); | ||
| 85 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_COMPLETE, $event); | ||
| 86 | 1 | ||
| 87 |             return $app->redirect($app->url('mypage_change_complete')); | ||
| 88 | } | ||
| 89 | |||
| 90 | $app['security']->getToken()->setUser($LoginCustomer); | ||
| 91 | |||
| 92 |         return $app->render('Mypage/change.twig', array( | ||
| 93 | 'form' => $form->createView(), | ||
| 94 | )); | ||
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * 会員情報編集完了画面. | ||
| 99 | * | ||
| 100 | * @param Application $app | ||
| 101 | * @param Request $request | ||
| 102 | * @return \Symfony\Component\HttpFoundation\Response | ||
| 103 | */ | ||
| 104 | public function complete(Application $app, Request $request) | ||
| 108 | } | ||
| 109 |