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 AdminListController 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 AdminListController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | abstract class AdminListController extends Controller |
||
|
|||
32 | { |
||
33 | /** |
||
34 | * You can override this method to return the correct entity manager when using multiple databases ... |
||
35 | * |
||
36 | * @return \Doctrine\Common\Persistence\ObjectManager|object |
||
37 | */ |
||
38 | protected function getEntityManager() |
||
42 | |||
43 | /** |
||
44 | * Shows the list of entities |
||
45 | * |
||
46 | * @param AbstractAdminListConfigurator $configurator |
||
47 | * @param null|Request $request |
||
48 | * |
||
49 | * @return Response |
||
50 | */ |
||
51 | protected function doIndexAction(AbstractAdminListConfigurator $configurator, Request $request) |
||
67 | |||
68 | /** |
||
69 | * Export a list of Entities |
||
70 | * |
||
71 | * @param AbstractAdminListConfigurator $configurator The adminlist configurator |
||
72 | * @param string $_format The format to export to |
||
73 | * @param null|Request $request |
||
74 | * |
||
75 | * @throws AccessDeniedHttpException |
||
76 | * |
||
77 | * @return Response |
||
78 | */ |
||
79 | protected function doExportAction(AbstractAdminListConfigurator $configurator, $_format, Request $request = null) |
||
93 | |||
94 | /** |
||
95 | * Creates and processes the form to add a new Entity |
||
96 | * |
||
97 | * @param AbstractAdminListConfigurator $configurator The adminlist configurator |
||
98 | * @param string $type The type to add |
||
99 | * @param null|Request $request |
||
100 | * |
||
101 | * @throws AccessDeniedHttpException |
||
102 | * |
||
103 | * @return Response |
||
104 | */ |
||
105 | protected function doAddAction(AbstractAdminListConfigurator $configurator, $type = null, Request $request) |
||
188 | |||
189 | /** |
||
190 | * Creates and processes the edit form for an Entity using its ID |
||
191 | * |
||
192 | * @param AbstractAdminListConfigurator $configurator The adminlist configurator |
||
193 | * @param string $entityId The id of the entity that will be edited |
||
194 | * @param null|Request $request |
||
195 | * |
||
196 | * @throws NotFoundHttpException |
||
197 | * @throws AccessDeniedHttpException |
||
198 | * |
||
199 | * @return Response |
||
200 | */ |
||
201 | protected function doEditAction(AbstractAdminListConfigurator $configurator, $entityId, Request $request) |
||
316 | |||
317 | protected function doViewAction(AbstractAdminListConfigurator $configurator, $entityId, Request $request) |
||
318 | { |
||
319 | /* @var EntityManager $em */ |
||
320 | $em = $this->getEntityManager(); |
||
321 | $helper = $em->getRepository($configurator->getRepositoryName())->findOneById($entityId); |
||
322 | if ($helper === null) { |
||
323 | throw new NotFoundHttpException('Entity not found.'); |
||
324 | } |
||
325 | |||
326 | if (!$configurator->canView($helper)) { |
||
327 | throw $this->createAccessDeniedException('You do not have sufficient rights to access this page.'); |
||
328 | } |
||
329 | |||
330 | $MetaData = $em->getClassMetadata($configurator->getRepositoryName()); |
||
331 | $fields = array(); |
||
332 | $accessor = PropertyAccess::createPropertyAccessor(); |
||
333 | foreach ($MetaData->fieldNames as $value) { |
||
334 | $fields[$value] = $accessor->getValue($helper, $value); |
||
335 | } |
||
336 | |||
337 | return new Response( |
||
338 | $this->renderView( |
||
339 | $configurator->getViewTemplate(), |
||
340 | array('entity' => $helper, 'adminlistconfigurator' => $configurator, 'fields' => $fields) |
||
341 | ) |
||
342 | ); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Delete the Entity using its ID |
||
347 | * |
||
348 | * @param AbstractAdminListConfigurator $configurator The adminlist configurator |
||
349 | * @param int $entityId The id to delete |
||
350 | * @param null|Request $request |
||
351 | * |
||
352 | * @throws NotFoundHttpException |
||
353 | * @throws AccessDeniedHttpException |
||
354 | * |
||
355 | * @return Response |
||
356 | */ |
||
357 | protected function doDeleteAction(AbstractAdminListConfigurator $configurator, $entityId, Request $request) |
||
358 | { |
||
359 | /* @var $em EntityManager */ |
||
360 | $em = $this->getEntityManager(); |
||
361 | $helper = $em->getRepository($configurator->getRepositoryName())->findOneById($entityId); |
||
362 | if ($helper === null) { |
||
363 | throw new NotFoundHttpException('Entity not found.'); |
||
364 | } |
||
365 | if (!$configurator->canDelete($helper)) { |
||
366 | throw $this->createAccessDeniedException('You do not have sufficient rights to access this page.'); |
||
367 | } |
||
368 | |||
369 | $indexUrl = $configurator->getIndexUrl(); |
||
370 | if ($request->isMethod('POST')) { |
||
371 | $adminListEvent = new AdminListEvent($helper, $request); |
||
372 | $this->container->get('event_dispatcher')->dispatch( |
||
373 | AdminListEvents::PRE_DELETE, |
||
374 | $adminListEvent |
||
375 | ); |
||
376 | |||
377 | // Check if Response is given |
||
378 | if ($adminListEvent->getResponse() instanceof Response) { |
||
379 | return $adminListEvent->getResponse(); |
||
380 | } |
||
381 | |||
382 | $em->remove($helper); |
||
383 | $em->flush(); |
||
384 | $this->container->get('event_dispatcher')->dispatch( |
||
385 | AdminListEvents::POST_DELETE, |
||
386 | $adminListEvent |
||
387 | ); |
||
388 | |||
389 | // Check if Response is given |
||
390 | if ($adminListEvent->getResponse() instanceof Response) { |
||
391 | return $adminListEvent->getResponse(); |
||
392 | } |
||
393 | } |
||
394 | |||
395 | return new RedirectResponse( |
||
396 | $this->generateUrl($indexUrl['path'], isset($indexUrl['params']) ? $indexUrl['params'] : array()) |
||
397 | ); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Move an item up in the list. |
||
402 | * |
||
403 | * @return RedirectResponse |
||
404 | */ |
||
405 | View Code Duplication | protected function doMoveUpAction(AbstractAdminListConfigurator $configurator, $entityId, Request $request) |
|
406 | { |
||
407 | $em = $this->getEntityManager(); |
||
408 | $sortableField = $configurator->getSortableField(); |
||
409 | |||
410 | $repositoryName = $this->getAdminListRepositoryName($configurator); |
||
411 | |||
412 | $repo = $em->getRepository($repositoryName); |
||
413 | $item = $repo->find($entityId); |
||
414 | |||
415 | $setter = 'set'.ucfirst($sortableField); |
||
416 | $getter = 'get'.ucfirst($sortableField); |
||
417 | |||
418 | $nextItem = $repo->createQueryBuilder('i') |
||
419 | ->where('i.'.$sortableField.' < :weight') |
||
420 | ->setParameter('weight', $item->$getter()) |
||
421 | ->orderBy('i.'.$sortableField, 'DESC') |
||
422 | ->setMaxResults(1) |
||
423 | ->getQuery() |
||
424 | ->getOneOrNullResult(); |
||
425 | if ($nextItem) { |
||
426 | $nextItem->$setter($item->$getter()); |
||
427 | $em->persist($nextItem); |
||
428 | $item->$setter($item->$getter() - 1); |
||
429 | |||
430 | $em->persist($item); |
||
431 | $em->flush(); |
||
432 | } |
||
433 | |||
434 | $indexUrl = $configurator->getIndexUrl(); |
||
435 | |||
436 | return new RedirectResponse( |
||
437 | $this->generateUrl($indexUrl['path'], isset($indexUrl['params']) ? $indexUrl['params'] : array()) |
||
438 | ); |
||
439 | } |
||
440 | |||
441 | View Code Duplication | protected function doMoveDownAction(AbstractAdminListConfigurator $configurator, $entityId, Request $request) |
|
442 | { |
||
443 | $em = $this->getEntityManager(); |
||
444 | $sortableField = $configurator->getSortableField(); |
||
445 | |||
446 | $repositoryName = $this->getAdminListRepositoryName($configurator); |
||
447 | |||
448 | $repo = $em->getRepository($repositoryName); |
||
449 | $item = $repo->find($entityId); |
||
450 | |||
451 | $setter = 'set'.ucfirst($sortableField); |
||
452 | $getter = 'get'.ucfirst($sortableField); |
||
453 | |||
454 | $nextItem = $repo->createQueryBuilder('i') |
||
455 | ->where('i.'.$sortableField.' > :weight') |
||
456 | ->setParameter('weight', $item->$getter()) |
||
457 | ->orderBy('i.'.$sortableField, 'ASC') |
||
458 | ->setMaxResults(1) |
||
459 | ->getQuery() |
||
460 | ->getOneOrNullResult(); |
||
461 | if ($nextItem) { |
||
462 | $nextItem->$setter($item->$getter()); |
||
463 | $em->persist($nextItem); |
||
464 | $item->$setter($item->$getter() + 1); |
||
465 | |||
466 | $em->persist($item); |
||
467 | $em->flush(); |
||
468 | } |
||
469 | |||
470 | $indexUrl = $configurator->getIndexUrl(); |
||
471 | |||
472 | return new RedirectResponse( |
||
473 | $this->generateUrl($indexUrl['path'], isset($indexUrl['params']) ? $indexUrl['params'] : array()) |
||
474 | ); |
||
475 | } |
||
476 | |||
477 | private function getMaxSortableField($repo, $sort) |
||
478 | { |
||
479 | $maxWeight = $repo->createQueryBuilder('i') |
||
480 | ->select('max(i.'.$sort.')') |
||
481 | ->getQuery() |
||
482 | ->getSingleScalarResult(); |
||
483 | |||
484 | return (int) $maxWeight; |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * @param LockableEntityInterface $entity |
||
489 | * |
||
490 | * @return bool |
||
491 | */ |
||
492 | protected function isLockableEntityLocked(LockableEntityInterface $entity) |
||
502 | |||
503 | /** |
||
504 | * Sets the sort weight on a new item. Can be overridden if a non-default sorting implementation is being used. |
||
505 | * |
||
506 | * @param AbstractAdminListConfigurator $configurator The adminlist configurator |
||
507 | * @param $item |
||
508 | * |
||
509 | * @return mixed |
||
510 | */ |
||
511 | protected function setSortWeightOnNewItem(AbstractAdminListConfigurator $configurator, $item) |
||
523 | |||
524 | protected function buildSortableFieldActions(AbstractAdminListConfigurator $configurator) |
||
549 | |||
550 | /** |
||
551 | * @param AbstractAdminListConfigurator $configurator |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | protected function getAdminListRepositoryName(AbstractAdminListConfigurator $configurator) |
||
567 | } |
||
568 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.