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 type |
||
| 59 | */ |
||
| 60 | protected function getEntity($entityName, $etm) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Finds and displays an item entity. |
||
| 91 | * |
||
| 92 | * @param Object $entity Entity |
||
| 93 | * @param string $entityName Name of Entity |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public function abstractShowAction($entity, $entityName) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Displays a form to create a new item entity. |
||
| 108 | * |
||
| 109 | * @param string $entity Entity |
||
| 110 | * @param string $entityPath Path of Entity |
||
| 111 | * @param string $typePath Path of FormType |
||
| 112 | * @param string|null $options Options of Form |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function abstractNewAction($entity, $entityPath, $typePath, $options = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Creates a new item entity. |
||
| 143 | * |
||
| 144 | * @param Request $request Request in progress |
||
| 145 | * @param string $entity Entity <i>First letter Upper</i> |
||
| 146 | * @param string $entityPath Path of Entity |
||
| 147 | * @param string $typePath Path of FormType |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function abstractCreateAction(Request $request, $entity, $entityPath, $typePath) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Displays a form to edit an existing item entity. |
||
| 178 | * |
||
| 179 | * @param Object $entity Entity |
||
| 180 | * @param string $entityName Name of Entity |
||
| 181 | * @param string $typePath Path of FormType |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function abstractEditAction($entity, $entityName, $typePath) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Edits an existing item entity. |
||
| 205 | * |
||
| 206 | * @param Object $entity Entity |
||
| 207 | * @param Request $request Request in progress |
||
| 208 | * @param string $entityName Name of Entity |
||
| 209 | * @param string $typePath Path of FormType |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | public function abstractUpdateAction($entity, Request $request, $entityName, $typePath) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Deletes an item entity. |
||
| 243 | * |
||
| 244 | * @param Object $entity Entity |
||
| 245 | * @param Request $request Request in progress |
||
| 246 | * @param string $entityName Name of Entity |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function abstractDeleteAction($entity, Request $request, $entityName) |
||
| 258 | |||
| 259 | private function testReturnParam($entity, $entityName) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * SetOrder for the SortAction in views. |
||
| 273 | * |
||
| 274 | * @param string $name session name |
||
| 275 | * @param string $entity entity name |
||
| 276 | * @param string $field field name |
||
| 277 | * @param string $type sort type ("ASC"/"DESC") |
||
| 278 | */ |
||
| 279 | protected function setOrder($name, $entity, $field, $type = 'ASC') |
||
| 285 | |||
| 286 | /** |
||
| 287 | * GetOrder for the SortAction in views. |
||
| 288 | * |
||
| 289 | * @param string $name session name |
||
| 290 | * |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | protected function getOrder($name) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * AddQueryBuilderSort for the SortAction in views. |
||
| 302 | * |
||
| 303 | * @param QueryBuilder $qbd |
||
| 304 | * @param string $name |
||
| 305 | */ |
||
| 306 | protected function addQueryBuilderSort(QueryBuilder $qbd, $name) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Create Delete form. |
||
| 327 | * |
||
| 328 | * @param int $id |
||
| 329 | * @param string $route |
||
| 330 | * |
||
| 331 | * @return \Symfony\Component\Form\Form |
||
| 332 | */ |
||
| 333 | protected function createDeleteForm($id, $route) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Array of file (`pdf`) layout. |
||
| 344 | * |
||
| 345 | * @param string $date File date |
||
| 346 | * @param string $title File title |
||
| 347 | * @return array<string,integer|string|boolean> |
||
| 348 | */ |
||
| 349 | protected function getArray($date, $title) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get the existing roles |
||
| 371 | * |
||
| 372 | * @return array Array of roles |
||
| 373 | */ |
||
| 374 | private function getExistingRoles() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Add roles to form |
||
| 388 | * |
||
| 389 | * @param \Symfony\Component\Form\Form $form The form in which to insert the roles |
||
| 390 | * @param \AppBundle\Entity\Group $group The entity to deal |
||
| 391 | * @return \Symfony\Component\Form\Form The form |
||
| 392 | */ |
||
| 393 | private function addRoles($form, $group) |
||
| 407 | } |
||
| 408 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: