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 string $prefixRoute prefix_route |
||
| 36 | * @param \Symfony\Component\HttpFoundation\Request $request Sort request |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public function abstractIndexAction($entityName, $prefixRoute, Request $request = null) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Finds and displays an item entity. |
||
| 56 | * |
||
| 57 | * @param Object $entity Entity |
||
| 58 | * @param string $prefixRoute prefix_route |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public function abstractShowAction($entity, $prefixRoute) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Displays a form to create a new item entity. |
||
| 73 | * |
||
| 74 | * @param string $entityName Name of Entity |
||
| 75 | * @param string $entityPath Path of Entity |
||
| 76 | * @param string $typePath Path of FormType |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function abstractNewAction($entityName, $entityPath, $typePath) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Creates a new item entity. |
||
| 104 | * |
||
| 105 | * @param \Symfony\Component\HttpFoundation\Request $request Request in progress |
||
| 106 | * @param string $entityName Entity name <i>First letter Upper</i> |
||
| 107 | * @param string $entityPath Path of Entity |
||
| 108 | * @param string $typePath Path of FormType |
||
| 109 | * @param string $prefixRoute Prefix of route |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function abstractCreateAction(Request $request, $entityName, $entityPath, $typePath, $prefixRoute) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Displays a form to edit an existing item entity. |
||
| 142 | * |
||
| 143 | * @param Object $entity Entity |
||
| 144 | * @param string $prefixRoute Prefix of Route |
||
| 145 | * @param string $typePath Path of FormType |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function abstractEditAction($entity, $prefixRoute, $typePath) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Edits an existing item entity. |
||
| 169 | * |
||
| 170 | * @param Object $entity Entity |
||
| 171 | * @param \Symfony\Component\HttpFoundation\Request $request Request in progress |
||
| 172 | * @param string $prefixRoute Prefix of Route |
||
| 173 | * @param string $typePath Path of FormType |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function abstractUpdateAction($entity, Request $request, $prefixRoute, $typePath) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Deletes an item entity. |
||
| 206 | * |
||
| 207 | * @param Object $entity Entity |
||
| 208 | * @param \Symfony\Component\HttpFoundation\Request $request Request in progress |
||
| 209 | * @param string $prefixRoute Prefix of Route |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | public function abstractDeleteAction($entity, Request $request, $prefixRoute) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Deletes a item entity with Articles. |
||
| 224 | * |
||
| 225 | * @param Object $entity Entity |
||
| 226 | * @param \Symfony\Component\HttpFoundation\Request $request Request in progress |
||
| 227 | * @param string $entityName Name of Entity |
||
| 228 | * @param string $prefixRoute Prefix of Route |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function abstractDeleteWithArticlesAction($entity, Request $request, $entityName, $prefixRoute) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * AddQueryBuilderSort for the SortAction in views. |
||
| 252 | * |
||
| 253 | * @param QueryBuilder $qbd |
||
| 254 | * @param string $name |
||
| 255 | */ |
||
| 256 | protected function addQueryBuilderSort(QueryBuilder $qbd, $name) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Create Delete form. |
||
| 277 | * |
||
| 278 | * @param int $id |
||
| 279 | * @param string $route |
||
| 280 | * |
||
| 281 | * @return \Symfony\Component\Form\Form |
||
| 282 | */ |
||
| 283 | protected function createDeleteForm($id, $route) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Test paramters to return. |
||
| 294 | * |
||
| 295 | * @param object $entity Entity to return |
||
| 296 | * @param string $entityName Entity name to test |
||
| 297 | * @return array Parameters to return |
||
| 298 | */ |
||
| 299 | private function testReturnParam($entity, $entityName) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the existing roles |
||
| 313 | * |
||
| 314 | * @return array Array of roles |
||
| 315 | */ |
||
| 316 | private function getExistingRoles() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Add roles to form |
||
| 330 | * |
||
| 331 | * @param \Symfony\Component\Form\Form $form The form in which to insert the roles |
||
| 332 | * @param \AppBundle\Entity\Group $group The entity to deal |
||
| 333 | * @return \Symfony\Component\Form\Form The form |
||
| 334 | */ |
||
| 335 | public function addRolesAction($form, $group) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the entity. |
||
| 352 | * |
||
| 353 | * @param string $entityName Name of Entity |
||
| 354 | * @param \Doctrine\Common\Persistence\ObjectManager $etm ObjectManager instances |
||
| 355 | * @return array|\Doctrine\ORM\QueryBuilder|null Entity elements |
||
| 356 | */ |
||
| 357 | private function getEntity($entityName, ObjectManager $etm) |
||
| 391 | } |
||
| 392 |