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