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 |
||
| 27 | class SiteController extends Controller |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @Route("/", name="homepage") |
||
| 31 | */ |
||
| 32 | 1 | View Code Duplication | public function indexAction(Request $request) |
|
|
|||
| 33 | { |
||
| 34 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 35 | 1 | $estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles(); |
|
| 36 | 1 | $paginator = $this->get('knp_paginator'); |
|
| 37 | 1 | $pagination = $paginator->paginate( |
|
| 38 | 1 | $estates, |
|
| 39 | 1 | $request->query->getInt('page', 1), |
|
| 40 | 5 |
||
| 41 | 1 | ); |
|
| 42 | 1 | $breadcrumbs = $this->get("white_october_breadcrumbs"); |
|
| 43 | 1 | $breadcrumbs->addItem("Главная"); |
|
| 44 | 1 | return $this->render("AppBundle::site/index.html.twig", array('pagination' => $pagination)); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @Route("/menu", name="menu") |
||
| 49 | */ |
||
| 50 | 4 | View Code Duplication | public function menuAction(Request $request) |
| 51 | { |
||
| 52 | 4 | $em = $this->getDoctrine()->getManager(); |
|
| 53 | 4 | $categoryEntity = $em->getRepository('AppBundle\Entity\Category'); |
|
| 54 | 4 | $categories = $categoryEntity->childrenHierarchy(); |
|
| 55 | |||
| 56 | 4 | return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]); |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Route("/show_category/{slug}", name="show_category") |
||
| 61 | * @ParamConverter("category", class="AppBundle\Entity\Category", options={"mapping": {"slug": "title"}}) |
||
| 62 | */ |
||
| 63 | 1 | View Code Duplication | public function showCategoryAction(Request $request, Category $category) |
| 64 | { |
||
| 65 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 66 | 1 | $estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($category->getTitle()); |
|
| 67 | 1 | $paginator = $this->get('knp_paginator'); |
|
| 68 | 1 | $pagination = $paginator->paginate( |
|
| 69 | 1 | $estates, |
|
| 70 | 1 | $request->query->getInt('page', 1), |
|
| 71 | 5 |
||
| 72 | 1 | ); |
|
| 73 | 1 | $this->container->get('app.breadcrumps_maker')->makeBreadcrumps($category); |
|
| 74 | |||
| 75 | 1 | return $this->render("AppBundle::site/index.html.twig", array('pagination' => $pagination)); |
|
| 76 | 1 | } |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @Route("/show_estate/{slug}", name="show_estate") |
||
| 80 | */ |
||
| 81 | 1 | public function showEstateAction(Request $request, $slug) |
|
| 82 | { |
||
| 83 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 84 | 1 | $estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug); |
|
| 85 | 1 | $this->container->get('app.breadcrumps_maker')->makeBreadcrumps($estate->getCategory(), $estate); |
|
| 86 | |||
| 87 | 1 | return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate)); |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @Security("is_granted('IS_AUTHENTICATED_FULLY')") |
||
| 92 | * @Route("/comment/{slug}/new", name = "comment_new") |
||
| 93 | * @Method("POST")estates |
||
| 94 | * @ParamConverter("estate", options={"mapping": {"slug": "slug"}}) |
||
| 95 | */ |
||
| 96 | 1 | public function commentNewAction(Estate $estate, Request $request) |
|
| 97 | { |
||
| 98 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 99 | $comment = new Comment(); |
||
| 100 | $form = $this->createForm(CommentType::class, $comment); |
||
| 101 | $form->handleRequest($request); |
||
| 102 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 103 | if ($this->getUser()->hasRole('ROLE_ADMIN')) { |
||
| 104 | $comment->setEnabled(true); |
||
| 105 | 1 | } else { |
|
| 106 | $comment->setEnabled(false); |
||
| 107 | 1 | } |
|
| 108 | $comment->setEstate($estate); |
||
| 109 | $entityManager->persist($comment); |
||
| 110 | 1 | $entityManager->flush(); |
|
| 111 | $this->get('session')->getFlashBag()->add('success', 'site.flush_comment'); |
||
| 112 | return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug())); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $this->render('@App/site/_comment_form.html.twig', array( |
||
| 116 | 'estate' => $estate, |
||
| 117 | 'form' => $form->createView(), |
||
| 118 | 1 | )); |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @Route("/search", name="site_search") |
||
| 123 | * */ |
||
| 124 | 4 | public function searchAction(Request $request) |
|
| 125 | { |
||
| 126 | 4 | $finalCategories = $this->container->get('app.final_category_finder')->findFinalCategories(); |
|
| 127 | 4 | $searchForm = $this->createForm(SearchType::class, null, array( |
|
| 128 | 4 | 'action' => $this->generateUrl('site_search_result'), |
|
| 129 | 4 | 'categories_choices' => $finalCategories)); |
|
| 130 | |||
| 131 | 4 | return $this->render('@App/site/search.html.twig', array( |
|
| 132 | 4 | 'form' => $searchForm->createView(), |
|
| 133 | 4 | )); |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @Route("/search/result", name="site_search_result") |
||
| 138 | * */ |
||
| 139 | public function searchResultAction(Request $request) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @Route("/menu_item", name="show_menu_item") |
||
| 163 | */ |
||
| 164 | 5 | public function showMenuItemAction(Request $request) |
|
| 165 | { |
||
| 166 | 5 | $em = $this->getDoctrine()->getManager(); |
|
| 167 | 5 | $menuitems = $em->getRepository('AppBundle:MenuItem')->findAll(); |
|
| 168 | 5 | return $this->render('AppBundle:includes:menu_items.html.twig', array('items' => $menuitems)); |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @Route("/description_menu/{id}", name="show_description_menu_item") |
||
| 173 | * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}}) |
||
| 174 | */ |
||
| 175 | 1 | public function showDescriptionMenuItem(Request $request, MenuItem $menuItem) |
|
| 176 | { |
||
| 177 | 1 | return $this->render('AppBundle:site:show_description_menu_item.html.twig', array('item' => $menuItem)); |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_favorites") |
||
| 182 | * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}}) |
||
| 183 | * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}}) |
||
| 184 | */ |
||
| 185 | 1 | View Code Duplication | public function addEstateToFavoritesAction(Estate $estate, User $user, Request $request) |
| 186 | { |
||
| 187 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 188 | 1 | if (!$user->hasEstate($estate)) { |
|
| 189 | 1 | $user->addEstate($estate); |
|
| 190 | 1 | $em->persist($user); |
|
| 191 | 1 | $em->flush(); |
|
| 192 | 1 | } |
|
| 193 | |||
| 194 | 1 | return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug())); |
|
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @Route("/delete_favorites/{estate}/{user}", name = "delete_estate_from_favorites") |
||
| 199 | * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}}) |
||
| 200 | * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}}) |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function deleteEstateFromFavoritesAction(Estate $estate, User $user, Request $request) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @Route("/pdf/{estate}", name = "pdf_estate") |
||
| 216 | * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}}) |
||
| 217 | */ |
||
| 218 | 1 | public function pdfEstateAction(Estate $estate, Request $request) |
|
| 219 | { |
||
| 220 | 1 | $html = $this->renderView('@App/site/pdf.html.twig', array('estate' => $estate)); |
|
| 221 | |||
| 222 | 1 | return new Response( |
|
| 230 | |||
| 231 | |||
| 232 | } |
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.