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:
Complex classes like ElementController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ElementController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class ElementController extends Controller |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @Route("/listar/{page}/{path}", name="organization_element_list", requirements={"page" = "\d+", "path" = ".+"}, defaults={"page" = "1", "path" = null}, methods={"GET"}) |
||
| 48 | */ |
||
| 49 | public function listAction($page, $path = null, Request $request) |
||
| 50 | { |
||
| 51 | $organization = $this->get('AppBundle\Service\UserExtensionService')->getCurrentOrganization(); |
||
| 52 | $this->denyAccessUnlessGranted(OrganizationVoter::MANAGE, $organization); |
||
| 53 | |||
| 54 | $q = $request->get('q', null); |
||
| 55 | |||
| 56 | $element = $this->getSelectedElement($path, $organization); |
||
| 57 | $pager = $this->getElementListPager($page, $element, $q); |
||
| 58 | |||
| 59 | $breadcrumb = $this->generateBreadcrumb($element); |
||
| 60 | |||
| 61 | return $this->render('organization/element/list.html.twig', [ |
||
| 62 | 'breadcrumb' => $breadcrumb, |
||
| 63 | 'title' => $element->getName(), |
||
| 64 | 'pager' => $pager, |
||
| 65 | 'current' => $element, |
||
| 66 | 'q' => $q, |
||
| 67 | 'domain' => 'element' |
||
| 68 | ]); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @Route("/operar/{path}", name="organization_element_operation", requirements={"path" = ".+"}, defaults={"path" = null}, methods={"POST"}) |
||
| 73 | */ |
||
| 74 | public function operationAction($path = null, Request $request) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @Route("/carpeta/{path}", name="organization_element_folder_new", requirements={"path" = ".+"}, methods={"GET", "POST"}) |
||
| 108 | * @Route("/nuevo/{path}", name="organization_element_new", requirements={"path" = ".+"}, methods={"GET", "POST"}) |
||
| 109 | * @Route("/modificar/{path}", name="organization_element_form", requirements={"path" = ".+"}, methods={"GET", "POST"}) |
||
| 110 | */ |
||
| 111 | public function formAction($path, Request $request) |
||
| 112 | { |
||
| 113 | $organization = $this->get('AppBundle\Service\UserExtensionService')->getCurrentOrganization(); |
||
| 114 | $this->denyAccessUnlessGranted(OrganizationVoter::MANAGE, $organization); |
||
| 115 | |||
| 116 | /** @var ObjectManager $em */ |
||
| 117 | $em = $this->getDoctrine()->getManager(); |
||
| 118 | |||
| 119 | /** @var Element|null $element */ |
||
| 120 | if (null === $element = $em->getRepository('AppBundle:Element')->findOneByOrganizationAndPath($organization, $path)) { |
||
| 121 | throw $this->createNotFoundException(); |
||
| 122 | } |
||
| 123 | |||
| 124 | list($breadcrumb, $element, $title) = $this->getElementBreadcrumbAndTitle($request, $element, $organization); |
||
| 125 | |||
| 126 | $form = $this->createForm(ElementType::class, $element); |
||
| 127 | |||
| 128 | $this->setElementReferencesInForm($element, $form); |
||
|
|
|||
| 129 | $this->setElementRolesInForm($element, $form, $organization); |
||
| 130 | |||
| 131 | $form->handleRequest($request); |
||
| 132 | |||
| 133 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 134 | try { |
||
| 135 | $em->persist($element); |
||
| 136 | $em->flush(); |
||
| 137 | $this->updateElementReferences($element, $em, $form); |
||
| 138 | $this->updateElementRoles($element, $em, $form); |
||
| 139 | $this->addFlash('success', $this->get('translator')->trans('message.saved', [], 'element')); |
||
| 140 | return $this->redirectToRoute('organization_element_list', ['page' => 1, 'path' => $element->getParent()->getPath()]); |
||
| 141 | } catch (\Exception $e) { |
||
| 142 | $this->addFlash('error', $this->get('translator')->trans('message.save_error', [], 'element')); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->render('organization/element/form.html.twig', [ |
||
| 147 | 'menu_path' => 'organization_element_list', |
||
| 148 | 'breadcrumb' => $breadcrumb, |
||
| 149 | 'title' => $title, |
||
| 150 | 'element' => $element, |
||
| 151 | 'form' => $form->createView() |
||
| 152 | ]); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns breadcrumb that matches the element (ignores root element) |
||
| 157 | * @param Element $element |
||
| 158 | * @param bool $ignoreLast |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | View Code Duplication | private function generateBreadcrumb(Element $element = null, $ignoreLast = true) |
|
| 162 | { |
||
| 163 | $breadcrumb = []; |
||
| 164 | |||
| 165 | if (null === $element) { |
||
| 166 | return null; |
||
| 167 | } |
||
| 168 | |||
| 169 | $item = $element; |
||
| 170 | while ($item->getParent()) { |
||
| 171 | $entry = ['fixed' => $item->getName()]; |
||
| 172 | if ($item !== $element || !$ignoreLast) { |
||
| 173 | $entry['routeName'] = 'organization_element_list'; |
||
| 174 | $entry['routeParams'] = ['path' => $item->getPath()]; |
||
| 175 | } |
||
| 176 | array_unshift($breadcrumb, $entry); |
||
| 177 | $item = $item->getParent(); |
||
| 178 | } |
||
| 179 | return $breadcrumb; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param $path |
||
| 184 | * @param $organization |
||
| 185 | * @return Element |
||
| 186 | */ |
||
| 187 | private function getSelectedElement($path, $organization) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param Element $element |
||
| 206 | * @param ObjectManager $em |
||
| 207 | * @param Form $form |
||
| 208 | */ |
||
| 209 | private function updateElementReferences($element, $em, $form) |
||
| 210 | { |
||
| 211 | /** @var Reference $reference */ |
||
| 212 | foreach ($element->getPathReferences() as $reference) { |
||
| 213 | $items = $em->getRepository('AppBundle:Element')->getChildrenQueryBuilder($reference->getTarget()) |
||
| 214 | ->andWhere('node.folder = false') |
||
| 215 | ->getQuery() |
||
| 216 | ->getResult(); |
||
| 217 | |||
| 218 | $data = $form |
||
| 219 | ->get('reference'.$reference->getTarget()->getId())->getData(); |
||
| 220 | |||
| 221 | if (!is_array($data)) { |
||
| 222 | $data = [$data]; |
||
| 223 | } |
||
| 224 | |||
| 225 | foreach ($items as $item) { |
||
| 226 | $childItems = $em->getRepository('AppBundle:Element')->getChildrenQueryBuilder($element) |
||
| 227 | ->getQuery() |
||
| 228 | ->getResult(); |
||
| 229 | |||
| 230 | if (in_array($item, $data)) { |
||
| 231 | $element->addLabel($item); |
||
| 232 | /** @var Element $child */ |
||
| 233 | foreach ($childItems as $child) { |
||
| 234 | $child->addLabel($item); |
||
| 235 | } |
||
| 236 | } else { |
||
| 237 | $element->removeLabel($item); |
||
| 238 | /** @var Element $child */ |
||
| 239 | foreach ($childItems as $child) { |
||
| 240 | $child->removeLabel($item); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | $em->flush(); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param Element $element |
||
| 250 | * @param ObjectManager $em |
||
| 251 | * @param Form $form |
||
| 252 | */ |
||
| 253 | private function updateElementRoles($element, $em, $form) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param Element $element |
||
| 292 | * @param Form $form |
||
| 293 | */ |
||
| 294 | private function setElementReferencesInForm($element, $form) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param Element $element |
||
| 327 | * @param Form $form |
||
| 328 | * @param Organization $organization |
||
| 329 | */ |
||
| 330 | private function setElementRolesInForm($element, $form, $organization) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param $page |
||
| 356 | * @param Element|null $element |
||
| 357 | * @param $q |
||
| 358 | * @return Pagerfanta |
||
| 359 | */ |
||
| 360 | private function getElementListPager($page, $element, $q) |
||
| 361 | { |
||
| 362 | /** @var ElementRepository $elementRepository */ |
||
| 363 | $elementRepository = $this->getDoctrine()->getManager()->getRepository('AppBundle:Element'); |
||
| 364 | |||
| 365 | /** @var QueryBuilder $queryBuilder */ |
||
| 366 | $queryBuilder = $elementRepository->getChildrenQueryBuilder($element, true) |
||
| 367 | ->addSelect('ref') |
||
| 368 | ->addSelect('r') |
||
| 369 | ->addSelect('u') |
||
| 370 | ->addSelect('l') |
||
| 371 | ->leftJoin('node.references', 'ref') |
||
| 372 | ->leftJoin('node.roles', 'r') |
||
| 373 | ->leftJoin('r.profile', 'p') |
||
| 374 | ->leftJoin('node.labels', 'l') |
||
| 375 | ->leftJoin('r.user', 'u'); |
||
| 376 | |||
| 377 | if ($q) { |
||
| 378 | $queryBuilder |
||
| 379 | ->andWhere('node.name LIKE :tq OR p.nameNeutral LIKE :tq OR l.name LIKE :tq OR u.firstName LIKE :tq OR u.lastName LIKE :tq') |
||
| 380 | ->setParameter('tq', '%'.$q.'%'); |
||
| 381 | } |
||
| 382 | |||
| 383 | $adapter = new DoctrineORMAdapter($queryBuilder, false); |
||
| 384 | $pager = new Pagerfanta($adapter); |
||
| 385 | $pager |
||
| 386 | ->setMaxPerPage($this->getParameter('page.size')) |
||
| 387 | ->setCurrentPage($q ? 1 : $page); |
||
| 388 | return $pager; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param Request $request |
||
| 393 | * @param Element|null $element |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | private function processElementMovementOperation(Request $request, $element) |
||
| 397 | { |
||
| 398 | $ok = false; |
||
| 399 | $em = $this->getDoctrine()->getManager(); |
||
| 400 | |||
| 401 | foreach (['up', 'down'] as $op) { |
||
| 402 | if ($request->get($op)) { |
||
| 403 | $item = $em->getRepository('AppBundle:Element')->find($request->get($op)); |
||
| 404 | if (null === $item || $item->getParent() !== $element) { |
||
| 405 | throw $this->createNotFoundException(); |
||
| 406 | } |
||
| 407 | $method = 'move'.ucfirst($op); |
||
| 408 | $em->getRepository('AppBundle:Element')->$method($item); |
||
| 409 | $ok = true; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | return $ok; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param $items |
||
| 417 | * @param Element|null $element |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | View Code Duplication | private function filterElementsFromItems($items, $element) |
|
| 421 | { |
||
| 422 | $em = $this->getDoctrine()->getManager(); |
||
| 423 | |||
| 424 | $elements = $em->createQueryBuilder() |
||
| 425 | ->select('e') |
||
| 426 | ->from('AppBundle:Element', 'e') |
||
| 427 | ->where('e.id IN (:items)') |
||
| 428 | ->andWhere('e.parent = :current') |
||
| 429 | ->andWhere('e.code IS NULL') |
||
| 430 | ->setParameter('items', $items) |
||
| 431 | ->setParameter('current', $element) |
||
| 432 | ->orderBy('e.left') |
||
| 433 | ->getQuery() |
||
| 434 | ->getResult(); |
||
| 435 | |||
| 436 | return $elements; |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param $items |
||
| 441 | * @param Element|null $element |
||
| 442 | */ |
||
| 443 | View Code Duplication | private function deleteElements($items, $element) |
|
| 444 | { |
||
| 445 | $em = $this->getDoctrine()->getManager(); |
||
| 446 | |||
| 447 | $em->createQueryBuilder() |
||
| 448 | ->delete('AppBundle:Element', 'e') |
||
| 449 | ->where('e.id IN (:items)') |
||
| 450 | ->andWhere('e.parent = :current') |
||
| 451 | ->andWhere('e.code IS NULL') |
||
| 452 | ->setParameter('items', $items) |
||
| 453 | ->setParameter('current', $element) |
||
| 454 | ->getQuery() |
||
| 455 | ->execute(); |
||
| 456 | |||
| 457 | $em->flush(); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param $path |
||
| 462 | * @param Request $request |
||
| 463 | * @param $items |
||
| 464 | * @param Element|null $element |
||
| 465 | * @return bool|\Symfony\Component\HttpFoundation\RedirectResponse |
||
| 466 | */ |
||
| 467 | private function processElementRemoveOperation($path, Request $request, $items, $element) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param Request $request |
||
| 483 | * @param $element |
||
| 484 | * @param $organization |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | private function getElementBreadcrumbAndTitle(Request $request, $element, $organization) |
||
| 508 | } |
||
| 509 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.