Complex classes like AbstractAppController 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 AbstractAppController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | abstract class AbstractAppController extends AbstractController |
||
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 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function abstractIndexAction($entityName, $prefixRoute, Request $request = null) |
||
58 | |||
59 | /** |
||
60 | * Finds and displays an item entity. |
||
61 | * |
||
62 | * @param object $entity Entity |
||
63 | * @param string $prefixRoute prefix_route |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function abstractShowAction($entity, $prefixRoute) |
||
73 | |||
74 | /** |
||
75 | * Displays a form to create a new item entity. |
||
76 | * |
||
77 | * @param string $entityName Name of Entity |
||
78 | * @param string $entityPath Path of Entity |
||
79 | * @param string $typePath Path of FormType |
||
80 | * @param string $prefixRoute Prefix of Route |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function abstractNewAction($entityName, $entityPath, $typePath, $prefixRoute) |
||
106 | |||
107 | /** |
||
108 | * Creates a new item entity. |
||
109 | * |
||
110 | * @param \Symfony\Component\HttpFoundation\Request $request Request in progress |
||
111 | * @param string $entityName Entity name <i>First letter Upper</i> |
||
112 | * @param string $entityPath Path of Entity |
||
113 | * @param string $typePath Path of FormType |
||
114 | * @param string $prefixRoute Prefix of route |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | public function abstractCreateAction(Request $request, $entityName, $entityPath, $typePath, $prefixRoute) |
||
145 | |||
146 | /** |
||
147 | * Displays a form to edit an existing item entity. |
||
148 | * |
||
149 | * @param object $entity Entity |
||
150 | * @param string $prefixRoute Prefix of Route |
||
151 | * @param string $typePath Path of FormType |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public function abstractEditAction($entity, $prefixRoute, $typePath) |
||
171 | |||
172 | /** |
||
173 | * Edits an existing item entity. |
||
174 | * |
||
175 | * @param object $entity Entity |
||
176 | * @param \Symfony\Component\HttpFoundation\Request $request Request in progress |
||
177 | * @param string $prefixRoute Prefix of Route |
||
178 | * @param string $typePath Path of FormType |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | public function abstractUpdateAction($entity, Request $request, $prefixRoute, $typePath) |
||
208 | |||
209 | /** |
||
210 | * Deletes an item entity. |
||
211 | * |
||
212 | * @param object $entity Entity |
||
213 | * @param \Symfony\Component\HttpFoundation\Request $request Request in progress |
||
214 | * @param string $prefixRoute Prefix of Route |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public function abstractDeleteAction($entity, Request $request, $prefixRoute) |
||
227 | |||
228 | /** |
||
229 | * Deletes a item entity with Articles. |
||
230 | * |
||
231 | * @param object $entity Entity |
||
232 | * @param \Symfony\Component\HttpFoundation\Request $request Request in progress |
||
233 | * @param string $entityName Name of Entity |
||
234 | * @param string $prefixRoute Prefix of Route |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function abstractDeleteWithArticlesAction($entity, Request $request, $entityName, $prefixRoute) |
||
256 | |||
257 | /** |
||
258 | * AddQueryBuilderSort for the SortAction in views. |
||
259 | * |
||
260 | * @param QueryBuilder $qbd QueryBuilder |
||
261 | * @param string $name Order name |
||
262 | */ |
||
263 | protected function addQueryBuilderSort(QueryBuilder $qbd, $name) |
||
282 | |||
283 | /** |
||
284 | * Create Delete form. |
||
285 | * |
||
286 | * @param int $id Id to delete |
||
287 | * @param string $route Route to redirect |
||
288 | * |
||
289 | * @return \Symfony\Component\Form\Form |
||
290 | */ |
||
291 | protected function createDeleteForm($id, $route) |
||
298 | |||
299 | /** |
||
300 | * Test paramters to return. |
||
301 | * |
||
302 | * @param object $entity Entity to return |
||
303 | * @param string $prefixRoute Entity name to test |
||
304 | * |
||
305 | * @return array Parameters to return |
||
306 | */ |
||
307 | protected function testReturnParam($entity, $prefixRoute) |
||
319 | |||
320 | /** |
||
321 | * Get the existing roles. |
||
322 | * |
||
323 | * @return array Array of roles |
||
324 | */ |
||
325 | private function getExistingRoles() |
||
337 | |||
338 | /** |
||
339 | * Add roles to form. |
||
340 | * |
||
341 | * @param \Symfony\Component\Form\Form $form The form in which to insert the roles |
||
342 | * @param \App\Entity\Staff\Group $group The entity to deal |
||
343 | * |
||
344 | * @return \Symfony\Component\Form\Form The form |
||
345 | */ |
||
346 | public function addRolesAction($form, $group) |
||
364 | |||
365 | /** |
||
366 | * Get the entity. |
||
367 | * |
||
368 | * @param string $entityName Name of Entity |
||
369 | * @param \Doctrine\Common\Persistence\ObjectManager $etm ObjectManager instances |
||
370 | * |
||
371 | * @return array|\Doctrine\ORM\QueryBuilder|null Entity elements |
||
372 | */ |
||
373 | private function getEntity($entityName, ObjectManager $etm) |
||
390 | } |
||
391 |
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: