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 |
||
| 32 | class ClassNameController extends AbstractController |
||
|
|
|||
| 33 | { |
||
| 34 | 3 | public function index(Application $app, Request $request, $id = null) |
|
| 35 | { |
||
| 36 | 3 | if ($id) { |
|
| 37 | $TargetClassName = $app['eccube.repository.class_name']->find($id); |
||
| 38 | 1 | if (!$TargetClassName) { |
|
| 39 | throw new NotFoundHttpException(); |
||
| 40 | } |
||
| 41 | } else { |
||
| 42 | $TargetClassName = new \Eccube\Entity\ClassName(); |
||
| 43 | 1 | } |
|
| 44 | |||
| 45 | $form = $app['form.factory'] |
||
| 46 | ->createBuilder('admin_class_name', $TargetClassName) |
||
| 47 | ->getForm(); |
||
| 48 | |||
| 49 | View Code Duplication | if ($request->getMethod() === 'POST') { |
|
| 50 | $form->handleRequest($request); |
||
| 51 | if ($form->isValid()) { |
||
| 52 | $status = $app['eccube.repository.class_name']->save($TargetClassName); |
||
| 53 | |||
| 54 | 1 | if ($status) { |
|
| 55 | $app->addSuccess('admin.class_name.save.complete', 'admin'); |
||
| 56 | |||
| 57 | return $app->redirect($app->url('admin_product_class_name')); |
||
| 58 | } else { |
||
| 59 | $app->addError('admin.class_name.save.error', 'admin'); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | $ClassNames = $app['eccube.repository.class_name']->getList(); |
||
| 65 | |||
| 66 | 2 | return $app->render('Product/class_name.twig', array( |
|
| 67 | 2 | 'form' => $form->createView(), |
|
| 68 | 'ClassNames' => $ClassNames, |
||
| 69 | 'TargetClassName' => $TargetClassName, |
||
| 70 | )); |
||
| 71 | 3 | } |
|
| 72 | |||
| 73 | 1 | View Code Duplication | public function delete(Application $app, Request $request, $id) |
| 93 | |||
| 94 | 1 | View Code Duplication | public function moveRank(Application $app, Request $request) |
| 108 | } |
||
| 109 |