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 | class ArticleController extends AbstractController |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Lists all Article entities. |
||
| 37 | * |
||
| 38 | * @Route("/", name="article") |
||
| 39 | * @Method("GET") |
||
| 40 | * @Template() |
||
| 41 | * |
||
| 42 | * @param \Symfony\Component\HttpFoundation\Request $request Paginate request |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | View Code Duplication | public function indexAction(Request $request) |
|
|
|
|||
| 46 | { |
||
| 47 | $item = $this->container->getParameter('knp_paginator.page_range'); |
||
| 48 | $etm = $this->getDoctrine()->getManager(); |
||
| 49 | $qbd = $etm->getRepository('AppBundle:Article')->getArticles(); |
||
| 50 | $this->addQueryBuilderSort($qbd, 'article'); |
||
| 51 | $paginator = $this->get('knp_paginator')->paginate($qbd, $request->query->get('page', 1), $item); |
||
| 52 | |||
| 53 | return array( |
||
| 54 | 'paginator' => $paginator, |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Finds and displays a Article entity. |
||
| 60 | * |
||
| 61 | * @Route("/{slug}/show", name="article_show") |
||
| 62 | * @Method("GET") |
||
| 63 | * @Template() |
||
| 64 | * |
||
| 65 | * @param \AppBundle\Entity\Article $article Article item to display |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | public function showAction(Article $article) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Displays a form to create a new Article entity. |
||
| 77 | * |
||
| 78 | * @Route("/admin/new", name="article_new") |
||
| 79 | * @Method("GET") |
||
| 80 | * @Template() |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | public function newAction() |
||
| 85 | { |
||
| 86 | $return = $this->abstractNewAction( |
||
| 87 | 'Article', |
||
| 88 | 'AppBundle\Entity\Article', |
||
| 89 | 'AppBundle\Form\Type\ArticleType' |
||
| 90 | ); |
||
| 91 | |||
| 92 | return $return; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Creates a new Article entity. |
||
| 97 | * |
||
| 98 | * @Route("/admin/create", name="article_create") |
||
| 99 | * @Method("POST") |
||
| 100 | * @Template("AppBundle:Article:new.html.twig") |
||
| 101 | * |
||
| 102 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function createAction(Request $request) |
||
| 106 | { |
||
| 107 | $return = $this->abstractCreateAction( |
||
| 108 | $request, |
||
| 109 | 'Article', |
||
| 110 | 'AppBundle\Entity\Article', |
||
| 111 | 'AppBundle\Form\Type\ArticleType' |
||
| 112 | ); |
||
| 113 | |||
| 114 | return $return; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Displays a form to edit an existing Article entity. |
||
| 119 | * |
||
| 120 | * @Route("/admin/{slug}/edit", name="article_edit") |
||
| 121 | * @Method("GET") |
||
| 122 | * @Template() |
||
| 123 | * |
||
| 124 | * @param \AppBundle\Entity\Article $article Article item to edit |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function editAction(Article $article) |
||
| 128 | { |
||
| 129 | $return = $this->abstractEditAction($article, 'article', 'AppBundle\Form\Type\ArticleType'); |
||
| 130 | |||
| 131 | return $return; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Edits an existing Article entity. |
||
| 136 | * |
||
| 137 | * @Route("/{slug}/update", name="article_update") |
||
| 138 | * @Method("PUT") |
||
| 139 | * @Template("AppBundle:Article:edit.html.twig") |
||
| 140 | * |
||
| 141 | * @param \AppBundle\Entity\Article $article Article item to update |
||
| 142 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public function updateAction(Article $article, Request $request) |
||
| 146 | { |
||
| 147 | $return = $this->abstractUpdateAction( |
||
| 148 | $article, |
||
| 149 | $request, |
||
| 150 | 'article', |
||
| 151 | 'AppBundle\Form\Type\ArticleType' |
||
| 152 | ); |
||
| 153 | |||
| 154 | return $return; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Réassigner les articles d'un fournisseur. |
||
| 159 | * |
||
| 160 | * @param Supplier $supplier Fournisseur à réassigner |
||
| 161 | * @Route("/{slug}/reassign", name="article_reassign") |
||
| 162 | * @Method("GET") |
||
| 163 | * @Template() |
||
| 164 | * |
||
| 165 | * @param \AppBundle\Entity\Supplier $supplier Supplier articles to reassign |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function reassignAction(Supplier $supplier) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Creates a new Article entity. |
||
| 190 | * |
||
| 191 | * @Route("/{slug}/change", name="article_change") |
||
| 192 | * @Method("POST") |
||
| 193 | * @Template("AppBundle:Article:reassign.html.twig") |
||
| 194 | * |
||
| 195 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
| 196 | * @param \AppBundle\Entity\Supplier $supplier Supplier to desactivate |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public function changeAction(Request $request, Supplier $supplier) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Save order. |
||
| 231 | * |
||
| 232 | * @Route("/order/{entity}/{field}/{type}", name="article_sort") |
||
| 233 | * |
||
| 234 | * @param string $entity Entity of the field to sort |
||
| 235 | * @param string $field Field to sort |
||
| 236 | * @param string $type type of sort |
||
| 237 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 238 | */ |
||
| 239 | public function sortAction($entity, $field, $type) |
||
| 245 | /** |
||
| 246 | * Deletes a Article entity. |
||
| 247 | * |
||
| 248 | * @Route("/admin/{id}/delete", name="article_delete", requirements={"id"="\d+"}) |
||
| 249 | * @Method("DELETE") |
||
| 250 | * |
||
| 251 | * @param \AppBundle\Entity\Article $article Article item to delete |
||
| 252 | * @param \Symfony\Component\HttpFoundation\Request $request Form request |
||
| 253 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 254 | */ |
||
| 255 | public function deleteAction(Article $article, Request $request) |
||
| 267 | } |
||
| 268 |
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.