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 | View Code Duplication | class ArticleController extends AbstractController |
|
|
|
|||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Lists all Article entities. |
||
| 37 | * |
||
| 38 | * @Route("/", name="articles") |
||
| 39 | * @Method("GET") |
||
| 40 | * @Template() |
||
| 41 | */ |
||
| 42 | public function indexAction(Request $request) |
||
| 43 | { |
||
| 44 | $em = $this->getDoctrine()->getManager(); |
||
| 45 | $qb = $em->getRepository('AppBundle:Article')->createQueryBuilder('a'); |
||
| 46 | $this->addQueryBuilderSort($qb, 'article'); |
||
| 47 | $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), 5); |
||
| 48 | |||
| 49 | return array( |
||
| 50 | 'paginator' => $paginator, |
||
| 51 | ); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Finds and displays a Article entity. |
||
| 56 | * |
||
| 57 | * @Route("/{slug}/show", name="articles_show") |
||
| 58 | * @Method("GET") |
||
| 59 | * @Template() |
||
| 60 | */ |
||
| 61 | public function showAction(Article $article) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Displays a form to create a new Article entity. |
||
| 73 | * |
||
| 74 | * @Route("/new", name="articles_new") |
||
| 75 | * @Method("GET") |
||
| 76 | * @Template() |
||
| 77 | */ |
||
| 78 | public function newAction() |
||
| 79 | { |
||
| 80 | $article = new Article(); |
||
| 81 | $form = $this->createForm(new ArticleType(), $article); |
||
| 82 | |||
| 83 | return array( |
||
| 84 | 'article' => $article, |
||
| 85 | 'form' => $form->createView(), |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Creates a new Article entity. |
||
| 91 | * |
||
| 92 | * @Route("/create", name="articles_create") |
||
| 93 | * @Method("POST") |
||
| 94 | * @Template("AppBundle:Article:new.html.twig") |
||
| 95 | */ |
||
| 96 | public function createAction(Request $request) |
||
| 97 | { |
||
| 98 | $article = new Article(); |
||
| 99 | $form = $this->createForm(new ArticleType(), $article); |
||
| 100 | if ($form->handleRequest($request)->isValid()) { |
||
| 101 | $em = $this->getDoctrine()->getManager(); |
||
| 102 | $em->persist($article); |
||
| 103 | $em->flush(); |
||
| 104 | |||
| 105 | return $this->redirectToRoute('articles_show', array('slug' => $article->getSlug())); |
||
| 106 | } |
||
| 107 | |||
| 108 | return array( |
||
| 109 | 'article' => $article, |
||
| 110 | 'form' => $form->createView(), |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Displays a form to edit an existing Article entity. |
||
| 116 | * |
||
| 117 | * @Route("/{slug}/edit", name="articles_edit") |
||
| 118 | * @Method("GET") |
||
| 119 | * @Template() |
||
| 120 | */ |
||
| 121 | public function editAction(Article $article) |
||
| 122 | { |
||
| 123 | $editForm = $this->createForm(new ArticleType(), $article, array( |
||
| 124 | 'action' => $this->generateUrl('articles_update', array('slug' => $article->getSlug())), |
||
| 125 | 'method' => 'PUT', |
||
| 126 | )); |
||
| 127 | $deleteForm = $this->createDeleteForm($article->getId(), 'articles_delete'); |
||
| 128 | |||
| 129 | return array( |
||
| 130 | 'article' => $article, |
||
| 131 | 'edit_form' => $editForm->createView(), |
||
| 132 | 'delete_form' => $deleteForm->createView(), |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Edits an existing Article entity. |
||
| 138 | * |
||
| 139 | * @Route("/{slug}/update", name="articles_update") |
||
| 140 | * @Method("PUT") |
||
| 141 | * @Template("AppBundle:Article:edit.html.twig") |
||
| 142 | */ |
||
| 143 | public function updateAction(Article $article, Request $request) |
||
| 144 | { |
||
| 145 | $editForm = $this->createForm(new ArticleType(), $article, array( |
||
| 146 | 'action' => $this->generateUrl('articles_update', array('slug' => $article->getSlug())), |
||
| 147 | 'method' => 'PUT', |
||
| 148 | )); |
||
| 149 | if ($editForm->handleRequest($request)->isValid()) { |
||
| 150 | $this->getDoctrine()->getManager()->flush(); |
||
| 151 | |||
| 152 | return $this->redirectToRoute('articles_edit', array('slug' => $article->getSlug())); |
||
| 153 | } |
||
| 154 | $deleteForm = $this->createDeleteForm($article->getId(), 'articles_delete'); |
||
| 155 | |||
| 156 | return array( |
||
| 157 | 'article' => $article, |
||
| 158 | 'edit_form' => $editForm->createView(), |
||
| 159 | 'delete_form' => $deleteForm->createView(), |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * Save order. |
||
| 166 | * |
||
| 167 | * @Route("/order/{field}/{type}", name="articles_sort") |
||
| 168 | */ |
||
| 169 | public function sortAction($field, $type) |
||
| 175 | /** |
||
| 176 | * Deletes a Article entity. |
||
| 177 | * |
||
| 178 | * @Route("/{id}/delete", name="articles_delete", requirements={"id"="\d+"}) |
||
| 179 | * @Method("DELETE") |
||
| 180 | */ |
||
| 181 | public function deleteAction(Article $article, Request $request) |
||
| 192 | } |
||
| 193 |
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.