Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Finds and displays an item entity. |
||
| 95 | * |
||
| 96 | * @param Object $entity Entity |
||
| 97 | * @param string $entityName Name of Entity |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function abstractShowAction($entity, $entityName) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Displays a form to create a new item entity. |
||
| 112 | * |
||
| 113 | * @param string $entity Entity |
||
| 114 | * @param string $entityPath Path of Entity |
||
| 115 | * @param string $typePath Path of FormType |
||
| 116 | * @param string|null $options Options of Form |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function abstractNewAction($entity, $entityPath, $typePath, $options = null) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Creates a new item entity. |
||
| 150 | * |
||
| 151 | * @param Request $request Request in progress |
||
| 152 | * @param string $entity Entity <i>First letter Upper</i> |
||
| 153 | * @param string $entityPath Path of Entity |
||
| 154 | * @param string $typePath Path of FormType |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function abstractCreateAction(Request $request, $entity, $entityPath, $typePath) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Displays a form to edit an existing item entity. |
||
| 188 | * |
||
| 189 | * @param Object $entity Entity |
||
| 190 | * @param string $entityName Name of Entity |
||
| 191 | * @param string $typePath Path of FormType |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | public function abstractEditAction($entity, $entityName, $typePath) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Edits an existing item entity. |
||
| 215 | * |
||
| 216 | * @param Object $entity Entity |
||
| 217 | * @param Request $request Request in progress |
||
| 218 | * @param string $entityName Name of Entity |
||
| 219 | * @param string $typePath Path of FormType |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | public function abstractUpdateAction($entity, Request $request, $entityName, $typePath) |
||
| 223 | { |
||
| 224 | $param = $this->testReturnParam($entity, $entityName); |
||
| 225 | $editForm = $this->createForm($typePath, $entity, array( |
||
| 226 | 'action' => $this->generateUrl($entityName.'_update', $param), |
||
| 227 | 'method' => 'PUT', |
||
| 228 | )); |
||
| 229 | if ($entityName === 'group') { |
||
| 230 | $this->addRolesAction($editForm, $entity); |
||
| 231 | } |
||
| 232 | $editForm->handleRequest($request); |
||
| 233 | $deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
||
| 234 | |||
| 235 | $return = array( |
||
| 236 | $entityName => $entity, |
||
| 237 | 'edit_form' => $editForm->createView(), |
||
| 238 | 'delete_form' => $deleteForm->createView(), |
||
| 239 | ); |
||
| 240 | |||
| 241 | if ($editForm->isValid()) { |
||
| 242 | $this->getDoctrine()->getManager()->flush(); |
||
| 243 | $this->addFlash('info', 'gestock.edit.ok'); |
||
| 244 | |||
| 245 | $return = $this->redirectToRoute($entityName.'_edit', $param); |
||
| 246 | } |
||
| 247 | |||
| 248 | return $return; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Deletes an item entity. |
||
| 253 | * |
||
| 254 | * @param Object $entity Entity |
||
| 255 | * @param Request $request Request in progress |
||
| 256 | * @param string $entityName Name of Entity |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | public function abstractDeleteAction($entity, Request $request, $entityName) |
||
| 268 | |||
| 269 | private function testReturnParam($entity, $entityName) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * SetOrder for the SortAction in views. |
||
| 283 | * |
||
| 284 | * @param string $name session name |
||
| 285 | * @param string $entity entity name |
||
| 286 | * @param string $field field name |
||
| 287 | * @param string $type sort type ("ASC"/"DESC") |
||
| 288 | */ |
||
| 289 | protected function setOrder($name, $entity, $field, $type = 'ASC') |
||
| 295 | |||
| 296 | /** |
||
| 297 | * GetOrder for the SortAction in views. |
||
| 298 | * |
||
| 299 | * @param string $name session name |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | protected function getOrder($name) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * AddQueryBuilderSort for the SortAction in views. |
||
| 312 | * |
||
| 313 | * @param QueryBuilder $qbd |
||
| 314 | * @param string $name |
||
| 315 | */ |
||
| 316 | protected function addQueryBuilderSort(QueryBuilder $qbd, $name) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Create Delete form. |
||
| 337 | * |
||
| 338 | * @param int $id |
||
| 339 | * @param string $route |
||
| 340 | * |
||
| 341 | * @return \Symfony\Component\Form\Form |
||
| 342 | */ |
||
| 343 | protected function createDeleteForm($id, $route) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Array of file (`pdf`) layout. |
||
| 354 | * |
||
| 355 | * @param string $date File date |
||
| 356 | * @param string $title File title |
||
| 357 | * @return array<string,integer|string|boolean> |
||
| 358 | */ |
||
| 359 | protected function getArray($date, $title) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get the existing roles |
||
| 381 | * |
||
| 382 | * @return array Array of roles |
||
| 383 | */ |
||
| 384 | private function getExistingRoles() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Add roles to form |
||
| 398 | * |
||
| 399 | * @param \Symfony\Component\Form\Form $form The form in which to insert the roles |
||
| 400 | * @param \AppBundle\Entity\Group $group The entity to deal |
||
| 401 | * @return \Symfony\Component\Form\Form The form |
||
| 402 | */ |
||
| 403 | public function addRolesAction($form, $group) |
||
| 417 | } |
||
| 418 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.