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