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 |
||
| 30 | class ContactController |
||
|
|
|||
| 31 | { |
||
| 32 | |||
| 33 | 5 | public function index(Application $app, Request $request) |
|
| 34 | { |
||
| 35 | |||
| 36 | /* @var $builder \Symfony\Component\Form\FormBuilderInterface */ |
||
| 37 | $builder = $app['form.factory']->createBuilder('contact'); |
||
| 38 | |||
| 39 | /* @var $form \Symfony\Component\Form\FormInterface */ |
||
| 40 | $form = $builder->getForm(); |
||
| 41 | |||
| 42 | if ($app['security']->isGranted('ROLE_USER')) { |
||
| 43 | /* @var $user \Eccube\Entity\Customer */ |
||
| 44 | $user = $app['user']; |
||
| 45 | $form->setData(array( |
||
| 46 | 'name01' => $user->getName01(), |
||
| 47 | 'name02' => $user->getName02(), |
||
| 48 | 'kana01' => $user->getKana01(), |
||
| 49 | 'kana02' => $user->getKana02(), |
||
| 50 | 'zip01' => $user->getZip01(), |
||
| 51 | 'zip02' => $user->getZip02(), |
||
| 52 | 'pref' => $user->getPref(), |
||
| 53 | 'addr01' => $user->getAddr01(), |
||
| 54 | 'addr02' => $user->getAddr02(), |
||
| 55 | 'tel01' => $user->getTel01(), |
||
| 56 | 'tel02' => $user->getTel02(), |
||
| 57 | 'tel03' => $user->getTel03(), |
||
| 58 | 'email' => $user->getEmail(), |
||
| 59 | )); |
||
| 60 | } |
||
| 61 | |||
| 62 | if ('POST' === $request->getMethod()) { |
||
| 63 | $form->handleRequest($request); |
||
| 64 | |||
| 65 | if ($form->isValid()) { |
||
| 66 | 4 | switch ($request->get('mode')) { |
|
| 67 | 4 | View Code Duplication | case 'confirm': |
| 68 | $builder->setAttribute('freeze', true); |
||
| 69 | $form = $builder->getForm(); |
||
| 70 | $form->handleRequest($request); |
||
| 71 | |||
| 72 | 1 | return $app->render('Contact/confirm.twig', array( |
|
| 73 | 1 | 'form' => $form->createView(), |
|
| 74 | )); |
||
| 75 | |||
| 76 | 3 | case 'complete': |
|
| 77 | // メール送信 |
||
| 78 | $app['eccube.service.mail']->sendrContactMail($form->getData()); |
||
| 79 | |||
| 80 | return $app->redirect($app->url('contact_complete')); |
||
| 81 | break; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | 1 | return $app->render('Contact/index.twig', array( |
|
| 87 | 1 | 'form' => $form->createView(), |
|
| 88 | )); |
||
| 89 | 5 | } |
|
| 90 | |||
| 91 | 1 | public function complete(Application $app) |
|
| 95 | } |
||
| 96 |