Complex classes like AbstractController 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 AbstractController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | abstract class AbstractController extends Controller |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Lists all items entity. |
||
| 33 | * |
||
| 34 | * @param string $entityName Name of Entity |
||
| 35 | * @param \Symfony\Component\HttpFoundation\Request $request Sort request |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function abstractIndexAction($entityName, Request $request = null) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get the entity |
||
| 55 | * |
||
| 56 | * @param string $entityName Name of Entity |
||
| 57 | * @param \Doctrine\Common\Persistence\ObjectManager $etm ObjectManager instances |
||
| 58 | * @return array|QueryBuilder|null Entity elements |
||
| 59 | */ |
||
| 60 | protected function getEntity($entityName, $etm) |
||
| 61 | { |
||
| 62 | $roles = ['ROLE_ADMIN', 'ROLE_SUPER_ADMIN']; |
||
| 63 | switch ($entityName) { |
||
| 64 | case 'Article': |
||
| 65 | case 'Supplier': |
||
| 66 | if ($this->getUser() !== null && in_array($this->getUser()->getRoles()[0], $roles)) { |
||
| 67 | $entities = $etm->getRepository('AppBundle:'.$entityName)->getAllItems(); |
||
| 68 | } else { |
||
| 69 | $entities = $etm->getRepository('AppBundle:'.$entityName)->getItems(); |
||
| 70 | } |
||
| 71 | break; |
||
| 72 | case 'User': |
||
| 73 | $entities = $etm->getRepository('AppBundle:'.$entityName)->getUsers(); |
||
| 74 | break; |
||
| 75 | case 'FamilyLog': |
||
| 76 | $entities = $etm->getRepository('AppBundle:'.$entityName)->childrenHierarchy(); |
||
| 77 | break; |
||
| 78 | case 'UnitStorage': |
||
| 79 | $entities = $etm->getRepository('AppBundle:'.$entityName)->createQueryBuilder('u'); |
||
| 80 | break; |
||
| 81 | default: |
||
| 82 | $entities = $etm->getRepository('AppBundle:'.$entityName)->findAll(); |
||
| 83 | } |
||
| 84 | return $entities; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Finds and displays an item entity. |
||
| 89 | * |
||
| 90 | * @param Object $entity Entity |
||
| 91 | * @param string $entityName Name of Entity |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function abstractShowAction($entity, $entityName) |
||
| 95 | { |
||
| 96 | $deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
||
| 97 | |||
| 98 | return array( |
||
| 99 | $entityName => $entity, |
||
| 100 | 'delete_form' => $deleteForm->createView(), |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Displays a form to create a new item entity. |
||
| 106 | * |
||
| 107 | * @param string $entity Entity |
||
| 108 | * @param string $entityPath Path of Entity |
||
| 109 | * @param string $typePath Path of FormType |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function abstractNewAction($entity, $entityPath, $typePath) |
||
| 113 | { |
||
| 114 | $etm = $this->getDoctrine()->getManager(); |
||
| 115 | $ctEntity = count($etm->getRepository('AppBundle:'.$entity)->findAll()); |
||
| 116 | |||
| 117 | if ($entity === 'Company' || $entity === 'Settings' && $ctEntity >= 1) { |
||
| 118 | $return = $this->redirectToRoute('_home'); |
||
| 119 | $this->addFlash('danger', 'gestock.settings.'.strtolower($entity).'.add2'); |
||
| 120 | } |
||
| 121 | |||
| 122 | $entityNew = $etm->getClassMetadata($entityPath)->newInstance(); |
||
| 123 | $form = $this->createForm($typePath, $entityNew, array( |
||
| 124 | 'action' => $this->generateUrl(strtolower($entity).'_create'), |
||
| 125 | )); |
||
| 126 | |||
| 127 | if ($entity === 'Group') { |
||
| 128 | $this->addRolesAction($form, $entityNew); |
||
| 129 | } |
||
| 130 | $return = array(strtolower($entity) => $entityNew, 'form' => $form->createView(),); |
||
| 131 | |||
| 132 | return $return; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Creates a new item entity. |
||
| 137 | * |
||
| 138 | * @param Request $request Request in progress |
||
| 139 | * @param string $entity Entity <i>First letter Upper</i> |
||
| 140 | * @param string $entityPath Path of Entity |
||
| 141 | * @param string $typePath Path of FormType |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function abstractCreateAction(Request $request, $entity, $entityPath, $typePath) |
||
| 145 | { |
||
| 146 | $param = array(); |
||
| 147 | $etm = $this->getDoctrine()->getManager(); |
||
| 148 | $entityNew = $etm->getClassMetadata($entityPath)->newInstance(); |
||
| 149 | $form = $this->createForm($typePath, $entityNew, array( |
||
| 150 | 'action' => $this->generateUrl(strtolower($entity).'_create'), |
||
| 151 | )); |
||
| 152 | if ($entity === 'Group') { |
||
| 153 | $this->addRolesAction($form, $entityNew); |
||
| 154 | } |
||
| 155 | $form->handleRequest($request); |
||
| 156 | $return = [$entity => $entityNew, 'form' => $form->createView(),]; |
||
| 157 | |||
| 158 | if ($form->isValid()) { |
||
| 159 | $etm = $this->getDoctrine()->getManager(); |
||
| 160 | $etm->persist($entityNew); |
||
| 161 | $etm->flush(); |
||
| 162 | |||
| 163 | $param = $this->get('app.helper.controller')->testReturnParam($entityNew, strtolower($entity)); |
||
| 164 | $route = $form->get('addmore')->isClicked() ? strtolower($entity).'_new' : strtolower($entity).'_show'; |
||
| 165 | |||
| 166 | $return = $this->redirectToRoute($route, $param); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $return; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Displays a form to edit an existing item entity. |
||
| 174 | * |
||
| 175 | * @param Object $entity Entity |
||
| 176 | * @param string $entityName Name of Entity |
||
| 177 | * @param string $typePath Path of FormType |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | public function abstractEditAction($entity, $entityName, $typePath) |
||
| 181 | { |
||
| 182 | $param = $this->get('app.helper.controller')->testReturnParam($entityNew, strtolower($entity)); |
||
| 183 | $editForm = $this->createForm($typePath, $entity, array( |
||
| 184 | 'action' => $this->generateUrl($entityName.'_update', $param), |
||
| 185 | 'method' => 'PUT', |
||
| 186 | )); |
||
| 187 | if ($entityName === 'group') { |
||
| 188 | $this->addRolesAction($editForm, $entity); |
||
| 189 | } |
||
| 190 | $deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
||
| 191 | |||
| 192 | return array( |
||
| 193 | $entityName => $entity, |
||
| 194 | 'edit_form' => $editForm->createView(), |
||
| 195 | 'delete_form' => $deleteForm->createView(), |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Edits an existing item entity. |
||
| 201 | * |
||
| 202 | * @param Object $entity Entity |
||
| 203 | * @param Request $request Request in progress |
||
| 204 | * @param string $entityName Name of Entity |
||
| 205 | * @param string $typePath Path of FormType |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function abstractUpdateAction($entity, Request $request, $entityName, $typePath) |
||
| 209 | { |
||
| 210 | $param = $this->get('app.helper.controller')->testReturnParam($entityNew, strtolower($entity)); |
||
| 211 | $editForm = $this->createForm($typePath, $entity, array( |
||
| 212 | 'action' => $this->generateUrl($entityName.'_update', $param), |
||
| 213 | 'method' => 'PUT', |
||
| 214 | )); |
||
| 215 | if ($entityName === 'group') { |
||
| 216 | $this->addRolesAction($editForm, $entity); |
||
| 217 | } |
||
| 218 | $editForm->handleRequest($request); |
||
| 219 | $deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
||
| 220 | |||
| 221 | $return = array( |
||
| 222 | $entityName => $entity, |
||
| 223 | 'edit_form' => $editForm->createView(), |
||
| 224 | 'delete_form' => $deleteForm->createView(),); |
||
| 225 | |||
| 226 | if ($editForm->isValid()) { |
||
| 227 | $this->getDoctrine()->getManager()->flush(); |
||
| 228 | $this->addFlash('info', 'gestock.edit.ok'); |
||
| 229 | |||
| 230 | $return = $this->redirectToRoute($entityName.'_edit', $param); |
||
| 231 | } |
||
| 232 | |||
| 233 | return $return; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Deletes an item entity. |
||
| 238 | * |
||
| 239 | * @param Object $entity Entity |
||
| 240 | * @param Request $request Request in progress |
||
| 241 | * @param string $entityName Name of Entity |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public function abstractDeleteAction($entity, Request $request, $entityName) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Deletes a item entity with Articles. |
||
| 256 | * |
||
| 257 | * @param Object $entity Entity |
||
| 258 | * @param Request $request Request in progress |
||
| 259 | * @param string $entityName Name of Entity |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function abstractDeleteWithArticlesAction($entity, Request $request, $entityName) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * AddQueryBuilderSort for the SortAction in views. |
||
| 283 | * |
||
| 284 | * @param QueryBuilder $qbd |
||
| 285 | * @param string $name |
||
| 286 | */ |
||
| 287 | protected function addQueryBuilderSort(QueryBuilder $qbd, $name) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Create Delete form. |
||
| 308 | * |
||
| 309 | * @param int $id |
||
| 310 | * @param string $route |
||
| 311 | * |
||
| 312 | * @return \Symfony\Component\Form\Form |
||
| 313 | */ |
||
| 314 | protected function createDeleteForm($id, $route) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Array of file (`pdf`) layout. |
||
| 325 | * |
||
| 326 | * @param string $date File date |
||
| 327 | * @param string $title File title |
||
| 328 | * @return array<string,integer|string|boolean> |
||
| 329 | */ |
||
| 330 | protected function getArray($date, $title) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the existing roles |
||
| 352 | * |
||
| 353 | * @return array Array of roles |
||
| 354 | */ |
||
| 355 | private function getExistingRoles() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Add roles to form |
||
| 369 | * |
||
| 370 | * @param \Symfony\Component\Form\Form $form The form in which to insert the roles |
||
| 371 | * @param \AppBundle\Entity\Group $group The entity to deal |
||
| 372 | * @return \Symfony\Component\Form\Form The form |
||
| 373 | */ |
||
| 374 | public function addRolesAction($form, $group) |
||
| 388 | } |
||
| 389 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.