| Conditions | 11 |
| Paths | 27 |
| Total Lines | 76 |
| Code Lines | 57 |
| Lines | 16 |
| Ratio | 21.05 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 86 | public function operationAction($path = null, Request $request) |
||
| 87 | { |
||
| 88 | $organization = $this->get('AppBundle\Service\UserExtensionService')->getCurrentOrganization(); |
||
| 89 | $this->denyAccessUnlessGranted(OrganizationVoter::MANAGE, $organization); |
||
| 90 | |||
| 91 | $em = $this->getDoctrine()->getManager(); |
||
| 92 | |||
| 93 | $elementRepository = $em->getRepository('AppBundle:Element'); |
||
| 94 | $element = $this->getSelectedElement($path, $elementRepository, $organization); |
||
| 95 | |||
| 96 | $ok = false; |
||
| 97 | View Code Duplication | if ($request->get('up')) { |
|
| 98 | $item = $em->getRepository('AppBundle:Element')->find($request->get('up')); |
||
| 99 | if (null === $item || $item->getParent() !== $element) { |
||
| 100 | throw $this->createNotFoundException(); |
||
| 101 | } |
||
| 102 | $em->getRepository('AppBundle:Element')->moveUp($item); |
||
| 103 | $ok = true; |
||
| 104 | } |
||
| 105 | View Code Duplication | if ($request->get('down')) { |
|
| 106 | $item = $em->getRepository('AppBundle:Element')->find($request->get('down')); |
||
| 107 | if (null === $item || $item->getParent() !== $element) { |
||
| 108 | throw $this->createNotFoundException(); |
||
| 109 | } |
||
| 110 | $em->getRepository('AppBundle:Element')->moveDown($item); |
||
| 111 | $ok = true; |
||
| 112 | } |
||
| 113 | |||
| 114 | $items = $request->request->get('elements', []); |
||
| 115 | if ($ok || count($items) === 0) { |
||
| 116 | return $this->redirectToRoute('organization_element_list', ['path' => $path]); |
||
| 117 | } |
||
| 118 | |||
| 119 | $elements = $em->createQueryBuilder() |
||
| 120 | ->select('e') |
||
| 121 | ->from('AppBundle:Element', 'e') |
||
| 122 | ->where('e.id IN (:items)') |
||
| 123 | ->andWhere('e.parent = :current') |
||
| 124 | ->andWhere('e.code IS NULL') |
||
| 125 | ->setParameter('items', $items) |
||
| 126 | ->setParameter('current', $element) |
||
| 127 | ->orderBy('e.left') |
||
| 128 | ->getQuery() |
||
| 129 | ->getResult(); |
||
| 130 | |||
| 131 | if ($request->get('confirm', '') === 'ok') { |
||
| 132 | try { |
||
| 133 | $em->createQueryBuilder() |
||
| 134 | ->delete('AppBundle:Element', 'e') |
||
| 135 | ->where('e.id IN (:items)') |
||
| 136 | ->andWhere('e.parent = :current') |
||
| 137 | ->andWhere('e.code IS NULL') |
||
| 138 | ->setParameter('items', $items) |
||
| 139 | ->setParameter('current', $element) |
||
| 140 | ->getQuery() |
||
| 141 | ->execute(); |
||
| 142 | |||
| 143 | $em->flush(); |
||
| 144 | $this->addFlash('success', $this->get('translator')->trans('message.deleted', [], 'element')); |
||
| 145 | } catch (\Exception $e) { |
||
| 146 | $this->addFlash('error', $this->get('translator')->trans('message.delete_error', [], 'element')); |
||
| 147 | } |
||
| 148 | return $this->redirectToRoute('organization_element_list', ['path' => $path]); |
||
| 149 | } |
||
| 150 | |||
| 151 | $title = $this->get('translator')->trans('title.delete', [], 'element'); |
||
| 152 | $breadcrumb = $this->generateBreadcrumb($element, false); |
||
| 153 | $breadcrumb[] = ['fixed' => $this->get('translator')->trans('title.delete', [], 'element')]; |
||
| 154 | |||
| 155 | return $this->render('organization/element/delete.html.twig', [ |
||
| 156 | 'menu_path' => 'organization_element_list', |
||
| 157 | 'breadcrumb' => $breadcrumb, |
||
| 158 | 'title' => $title, |
||
| 159 | 'elements' => $elements |
||
| 160 | ]); |
||
| 161 | } |
||
| 162 | |||
| 268 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: