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 | View Code Duplication | class FamilyLogController extends AbstractController |
|
|
1 ignored issue
–
show
|
|||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Lists all FamilyLog entities. |
||
| 36 | * |
||
| 37 | * @Route("/", name="admin_familylog") |
||
| 38 | * @Method("GET") |
||
| 39 | * @Template() |
||
| 40 | */ |
||
| 41 | public function indexAction() |
||
| 42 | { |
||
| 43 | $em = $this->getDoctrine()->getManager(); |
||
| 44 | $entities = $em->getRepository('AppBundle:FamilyLog')->childrenHierarchy(); |
||
| 45 | |||
| 46 | return array( |
||
| 47 | 'entities' => $entities, |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Finds and displays a FamilyLog entity. |
||
| 53 | * |
||
| 54 | * @Route("/{slug}/show", name="admin_familylog_show") |
||
| 55 | * @Method("GET") |
||
| 56 | * @Template() |
||
| 57 | */ |
||
| 58 | public function showAction(FamilyLog $familylog) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Displays a form to create a new FamilyLog entity. |
||
| 70 | * |
||
| 71 | * @Route("/new", name="admin_familylog_new") |
||
| 72 | * @Method("GET") |
||
| 73 | * @Template() |
||
| 74 | */ |
||
| 75 | public function newAction() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Creates a new FamilyLog entity. |
||
| 88 | * |
||
| 89 | * @Route("/create", name="admin_familylog_create") |
||
| 90 | * @Method("POST") |
||
| 91 | * @Template("AppBundle:FamilyLog:new.html.twig") |
||
| 92 | */ |
||
| 93 | public function createAction(Request $request) |
||
| 94 | { |
||
| 95 | $familylog = new FamilyLog(); |
||
| 96 | $form = $this->createForm(new FamilyLogType(), $familylog); |
||
| 97 | if ($form->handleRequest($request)->isValid()) { |
||
| 98 | $em = $this->getDoctrine()->getManager(); |
||
| 99 | $em->persist($familylog); |
||
| 100 | $em->flush(); |
||
| 101 | |||
| 102 | if ($form->get('save')->isSubmitted()) { |
||
| 103 | $url = $this->redirect($this->generateUrl( |
||
| 104 | 'admin_familylog_show', |
||
| 105 | array('slug' => $familylog->getSlug()) |
||
| 106 | )); |
||
| 107 | } elseif ($form->get('addmore')->isSubmitted()) { |
||
| 108 | $this->addFlash('info', 'gestock.settings.add_ok'); |
||
| 109 | $url = $this->redirectToRoute('admin_familylog_new'); |
||
| 110 | } |
||
| 111 | return $url; |
||
| 112 | } |
||
| 113 | |||
| 114 | return array( |
||
| 115 | 'familylog' => $familylog, |
||
| 116 | 'form' => $form->createView(), |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Displays a form to edit an existing FamilyLog entity. |
||
| 122 | * |
||
| 123 | * @Route("/{slug}/edit", name="admin_familylog_edit") |
||
| 124 | * @Method("GET") |
||
| 125 | * @Template() |
||
| 126 | */ |
||
| 127 | public function editAction(FamilyLog $familylog) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Edits an existing FamilyLog entity. |
||
| 147 | * |
||
| 148 | * @Route("/{slug}/update", name="admin_familylog_update") |
||
| 149 | * @Method("PUT") |
||
| 150 | * @Template("AppBundle:FamilyLog:edit.html.twig") |
||
| 151 | */ |
||
| 152 | public function updateAction(FamilyLog $famlog, Request $request) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Deletes a FamilyLog entity. |
||
| 178 | * |
||
| 179 | * @Route("/{id}/delete", name="admin_familylog_delete", requirements={"id"="\d+"}) |
||
| 180 | * @Method("DELETE") |
||
| 181 | */ |
||
| 182 | public function deleteAction(FamilyLog $familylog, Request $request) |
||
| 193 | } |
||
| 194 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.