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 | * Finds and displays an item entity. |
||
| 55 | * |
||
| 56 | * @param Object $entity Entity |
||
| 57 | * @param string $entityName Name of Entity |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function abstractShowAction($entity, $entityName) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Displays a form to create a new item entity. |
||
| 72 | * |
||
| 73 | * @param string $entity Entity |
||
| 74 | * @param string $entityPath Path of Entity |
||
| 75 | * @param string $typePath Path of FormType |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function abstractNewAction($entity, $entityPath, $typePath) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Creates a new item entity. |
||
| 103 | * |
||
| 104 | * @param Request $request Request in progress |
||
| 105 | * @param string $entity Entity <i>First letter Upper</i> |
||
| 106 | * @param string $entityPath Path of Entity |
||
| 107 | * @param string $typePath Path of FormType |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | public function abstractCreateAction(Request $request, $entity, $entityPath, $typePath) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Displays a form to edit an existing item entity. |
||
| 140 | * |
||
| 141 | * @param Object $entity Entity |
||
| 142 | * @param string $entityName Name of Entity |
||
| 143 | * @param string $typePath Path of FormType |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function abstractEditAction($entity, $entityName, $typePath) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Edits an existing item entity. |
||
| 167 | * |
||
| 168 | * @param Object $entity Entity |
||
| 169 | * @param Request $request Request in progress |
||
| 170 | * @param string $entityName Name of Entity |
||
| 171 | * @param string $typePath Path of FormType |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function abstractUpdateAction($entity, Request $request, $entityName, $typePath) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Deletes an item entity. |
||
| 204 | * |
||
| 205 | * @param Object $entity Entity |
||
| 206 | * @param Request $request Request in progress |
||
| 207 | * @param string $entityName Name of Entity |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function abstractDeleteAction($entity, Request $request, $entityName) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Deletes a item entity with Articles. |
||
| 222 | * |
||
| 223 | * @param Object $entity Entity |
||
| 224 | * @param Request $request Request in progress |
||
| 225 | * @param string $entityName Name of Entity |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function abstractDeleteWithArticlesAction($entity, Request $request, $entityName) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * AddQueryBuilderSort for the SortAction in views. |
||
| 249 | * |
||
| 250 | * @param QueryBuilder $qbd |
||
| 251 | * @param string $name |
||
| 252 | */ |
||
| 253 | protected function addQueryBuilderSort(QueryBuilder $qbd, $name) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Create Delete form. |
||
| 274 | * |
||
| 275 | * @param int $id |
||
| 276 | * @param string $route |
||
| 277 | * |
||
| 278 | * @return \Symfony\Component\Form\Form |
||
| 279 | */ |
||
| 280 | protected function createDeleteForm($id, $route) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the existing roles |
||
| 291 | * |
||
| 292 | * @return array Array of roles |
||
| 293 | */ |
||
| 294 | private function getExistingRoles() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Add roles to form |
||
| 308 | * |
||
| 309 | * @param \Symfony\Component\Form\Form $form The form in which to insert the roles |
||
| 310 | * @param \AppBundle\Entity\Group $group The entity to deal |
||
| 311 | * @return \Symfony\Component\Form\Form The form |
||
| 312 | */ |
||
| 313 | private function addRoles($form, $group) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the entity. |
||
| 330 | * |
||
| 331 | * @param string $entityName Name of Entity |
||
| 332 | * @param \Doctrine\Common\Persistence\ObjectManager $etm ObjectManager instances |
||
| 333 | * @return array|\Doctrine\ORM\QueryBuilder|null Entity elements |
||
| 334 | */ |
||
| 335 | private function getEntity($entityName, ObjectManager $etm) |
||
| 368 | } |
||
| 369 |