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 AbstractAdminListConfigurator 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 AbstractAdminListConfigurator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | abstract class AbstractAdminListConfigurator implements AdminListConfiguratorInterface, ExportListConfiguratorInterface |
||
24 | { |
||
25 | const SUFFIX_ADD = 'add'; |
||
26 | const SUFFIX_EDIT = 'edit'; |
||
27 | const SUFFIX_EXPORT = 'export'; |
||
28 | const SUFFIX_DELETE = 'delete'; |
||
29 | const SUFFIX_VIEW = 'view'; |
||
30 | |||
31 | /** |
||
32 | * @var Field[] |
||
33 | */ |
||
34 | private $fields = array(); |
||
35 | |||
36 | /** |
||
37 | * @var Field[] |
||
38 | */ |
||
39 | private $exportFields = array(); |
||
40 | |||
41 | /** |
||
42 | * @var ItemActionInterface[] |
||
43 | */ |
||
44 | private $itemActions = array(); |
||
45 | |||
46 | /** |
||
47 | * @var ListActionInterface[] |
||
48 | */ |
||
49 | private $listActions = array(); |
||
50 | |||
51 | /** |
||
52 | * @var BulkActionInterface[] |
||
53 | */ |
||
54 | private $bulkActions = array(); |
||
55 | |||
56 | /** |
||
57 | * @var AbstractType |
||
58 | */ |
||
59 | private $type = null; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $typeOptions = array(); |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $listTemplate = 'KunstmaanAdminListBundle:Default:list.html.twig'; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $addTemplate = 'KunstmaanAdminListBundle:Default:add_or_edit.html.twig'; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | private $editTemplate = 'KunstmaanAdminListBundle:Default:add_or_edit.html.twig'; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | private $viewTemplate = 'KunstmaanAdminListBundle:Default:view.html.twig'; |
||
85 | |||
86 | /** |
||
87 | * @var string |
||
88 | */ |
||
89 | private $deleteTemplate = 'KunstmaanAdminListBundle:Default:delete.html.twig'; |
||
90 | |||
91 | /** |
||
92 | * @var FilterBuilder |
||
93 | */ |
||
94 | private $filterBuilder = null; |
||
95 | |||
96 | /** |
||
97 | * @var int |
||
98 | */ |
||
99 | protected $page = 1; |
||
100 | |||
101 | /** |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $orderBy = ''; |
||
105 | |||
106 | /** |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $orderDirection = ''; |
||
110 | |||
111 | /** |
||
112 | * Return current bundle name. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | abstract public function getBundleName(); |
||
117 | |||
118 | /** |
||
119 | * Return current entity name. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | abstract public function getEntityName(); |
||
124 | |||
125 | /** |
||
126 | * Return default repository name. |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getRepositoryName() |
||
134 | |||
135 | /** |
||
136 | * Configure the fields you can filter on |
||
137 | */ |
||
138 | public function buildFilters() |
||
141 | |||
142 | /** |
||
143 | * Configure the actions for each line |
||
144 | */ |
||
145 | public function buildItemActions() |
||
148 | |||
149 | /** |
||
150 | * Configure the actions that can be executed on the whole list |
||
151 | */ |
||
152 | public function buildListActions() |
||
155 | |||
156 | /** |
||
157 | * Configure the export fields |
||
158 | */ |
||
159 | public function buildExportFields() |
||
170 | |||
171 | /** |
||
172 | * Build iterator (if needed) |
||
173 | */ |
||
174 | public function buildIterator() |
||
177 | |||
178 | /** |
||
179 | * Reset all built members |
||
180 | */ |
||
181 | public function resetBuilds() |
||
189 | |||
190 | /** |
||
191 | * Configure the types of items you can add |
||
192 | * |
||
193 | * @param array $params |
||
194 | * |
||
195 | * @return array |
||
|
|||
196 | */ |
||
197 | public function getAddUrlFor(array $params = array()) |
||
214 | |||
215 | /** |
||
216 | * Get the url to export the listed items |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | public function getExportUrl() |
||
229 | |||
230 | public function getViewUrlFor($item) |
||
245 | |||
246 | /** |
||
247 | * Return the url to list all the items |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | public function getIndexUrl() |
||
260 | |||
261 | /** |
||
262 | * @param object $entity |
||
263 | * |
||
264 | * @throws InvalidArgumentException |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | public function getAdminType($entity) |
||
283 | |||
284 | /** |
||
285 | * @param string $type |
||
286 | * |
||
287 | * @return AbstractAdminListConfigurator |
||
288 | */ |
||
289 | public function setAdminType($type) |
||
295 | |||
296 | /** |
||
297 | * @param array $typeOptions |
||
298 | * |
||
299 | * @return AbstractAdminListConfigurator |
||
300 | */ |
||
301 | public function setAdminTypeOptions($typeOptions) |
||
307 | |||
308 | /** |
||
309 | * Return the default form admin type options |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | public function getAdminTypeOptions() |
||
317 | |||
318 | /** |
||
319 | * @param object|array $item |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function canEdit($item) |
||
327 | |||
328 | /** |
||
329 | * Configure if it's possible to delete the given $item |
||
330 | * |
||
331 | * @param object|array $item |
||
332 | * |
||
333 | * @return bool |
||
334 | */ |
||
335 | public function canDelete($item) |
||
339 | |||
340 | /** |
||
341 | * Configure if it's possible to add new items |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | public function canAdd() |
||
349 | |||
350 | public function canView($item) |
||
354 | |||
355 | /** |
||
356 | * Configure if it's possible to add new items |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | public function canExport() |
||
364 | |||
365 | /** |
||
366 | * @param string $name The field name |
||
367 | * @param string $header The header title |
||
368 | * @param string $sort Sortable column or not |
||
369 | * @param string $template The template |
||
370 | * @param FieldAlias $alias The alias |
||
371 | * |
||
372 | * @return AbstractAdminListConfigurator |
||
373 | */ |
||
374 | public function addField($name, $header, $sort, $template = null, FieldAlias $alias = null) |
||
380 | |||
381 | /** |
||
382 | * @param string $name The field name |
||
383 | * @param string $header The header title |
||
384 | * @param string $template The template |
||
385 | * @param FieldAlias $alias The alias |
||
386 | * |
||
387 | * @return AbstractAdminListConfigurator |
||
388 | */ |
||
389 | public function addExportField($name, $header, $template = null, FieldAlias $alias = null) |
||
395 | |||
396 | /** |
||
397 | * @param string $columnName The column name |
||
398 | * @param FilterTypeInterface $type The filter type |
||
399 | * @param string $filterName The name of the filter |
||
400 | * @param array $options Options |
||
401 | * |
||
402 | * @return AbstractAdminListConfigurator |
||
403 | */ |
||
404 | public function addFilter( |
||
414 | |||
415 | /** |
||
416 | * @return int |
||
417 | */ |
||
418 | public function getLimit() |
||
422 | |||
423 | /** |
||
424 | * @return array |
||
425 | */ |
||
426 | public function getSortFields() |
||
437 | |||
438 | /** |
||
439 | * @return Field[] |
||
440 | */ |
||
441 | public function getFields() |
||
445 | |||
446 | /** |
||
447 | * @return Field[] |
||
448 | */ |
||
449 | public function getExportFields() |
||
457 | |||
458 | /** |
||
459 | * @param string $label The label, only used when the template equals null |
||
460 | * @param callable $routeGenerator The generator used to generate the url of an item, when generating the item will |
||
461 | * be provided |
||
462 | * @param string $icon The icon, only used when the template equals null |
||
463 | * @param string $template The template, when not specified the label is shown |
||
464 | * |
||
465 | * @return AbstractAdminListConfigurator |
||
466 | */ |
||
467 | public function addSimpleItemAction($label, $routeGenerator, $icon, $template = null) |
||
471 | |||
472 | /** |
||
473 | * @param ItemActionInterface $itemAction |
||
474 | * |
||
475 | * @return AbstractAdminListConfigurator |
||
476 | */ |
||
477 | public function addItemAction(ItemActionInterface $itemAction) |
||
483 | |||
484 | /** |
||
485 | * @return bool |
||
486 | */ |
||
487 | public function hasItemActions() |
||
491 | |||
492 | /** |
||
493 | * @return ItemActionInterface[] |
||
494 | */ |
||
495 | public function getItemActions() |
||
499 | |||
500 | /** |
||
501 | * @param ListActionInterface $listAction |
||
502 | * |
||
503 | * @return AdminListConfiguratorInterface |
||
504 | */ |
||
505 | public function addListAction(ListActionInterface $listAction) |
||
511 | |||
512 | /** |
||
513 | * @return bool |
||
514 | */ |
||
515 | public function hasListActions() |
||
519 | |||
520 | /** |
||
521 | * @return ListActionInterface[] |
||
522 | */ |
||
523 | public function getListActions() |
||
527 | |||
528 | /** |
||
529 | * @param BulkActionInterface $bulkAction |
||
530 | * |
||
531 | * @return AdminListConfiguratorInterface |
||
532 | */ |
||
533 | public function addBulkAction(BulkActionInterface $bulkAction) |
||
539 | |||
540 | /** |
||
541 | * @return bool |
||
542 | */ |
||
543 | public function hasBulkActions() |
||
547 | |||
548 | /** |
||
549 | * @return BulkActionInterface[] |
||
550 | */ |
||
551 | public function getBulkActions() |
||
555 | |||
556 | /** |
||
557 | * @return string |
||
558 | */ |
||
559 | public function getListTemplate() |
||
563 | |||
564 | /** |
||
565 | * @param string $template |
||
566 | * |
||
567 | * @return AdminListConfiguratorInterface |
||
568 | */ |
||
569 | public function setListTemplate($template) |
||
575 | |||
576 | /** |
||
577 | * @param array|object $item The item |
||
578 | * @param string $columnName The column name |
||
579 | * |
||
580 | * @return mixed |
||
581 | */ |
||
582 | public function getValue($item, $columnName) |
||
602 | |||
603 | /** |
||
604 | * @param array|object $item The item |
||
605 | * @param string $columnName The column name |
||
606 | * |
||
607 | * @return string |
||
608 | */ |
||
609 | public function getStringValue($item, $columnName) |
||
638 | |||
639 | /** |
||
640 | * @return string |
||
641 | */ |
||
642 | public function getAddTemplate() |
||
646 | |||
647 | /** |
||
648 | * @param string $template |
||
649 | * |
||
650 | * @return AdminListConfiguratorInterface |
||
651 | */ |
||
652 | public function setAddTemplate($template) |
||
658 | |||
659 | /** |
||
660 | * @return string |
||
661 | */ |
||
662 | public function getViewTemplate() |
||
666 | |||
667 | /** |
||
668 | * @param string $template |
||
669 | * |
||
670 | * @return AdminListConfiguratorInterface |
||
671 | */ |
||
672 | public function setViewTemplate($template) |
||
678 | |||
679 | /** |
||
680 | * @return string |
||
681 | */ |
||
682 | public function getEditTemplate() |
||
686 | |||
687 | /** |
||
688 | * @param string $template |
||
689 | * |
||
690 | * @return AdminListConfiguratorInterface |
||
691 | */ |
||
692 | public function setEditTemplate($template) |
||
698 | |||
699 | /** |
||
700 | * @return string |
||
701 | */ |
||
702 | public function getDeleteTemplate() |
||
706 | |||
707 | /** |
||
708 | * @param string $template |
||
709 | * |
||
710 | * @return AdminListConfiguratorInterface |
||
711 | */ |
||
712 | public function setDeleteTemplate($template) |
||
718 | |||
719 | /** |
||
720 | * You can override this method to do some custom things you need to do when adding an entity |
||
721 | * |
||
722 | * @param object $entity |
||
723 | * |
||
724 | * @return mixed |
||
725 | */ |
||
726 | public function decorateNewEntity($entity) |
||
730 | |||
731 | /** |
||
732 | * @return FilterBuilder |
||
733 | */ |
||
734 | public function getFilterBuilder() |
||
742 | |||
743 | /** |
||
744 | * @param FilterBuilder $filterBuilder |
||
745 | * |
||
746 | * @return AbstractAdminListConfigurator |
||
747 | */ |
||
748 | public function setFilterBuilder(FilterBuilder $filterBuilder) |
||
754 | |||
755 | /** |
||
756 | * Bind current request. |
||
757 | * |
||
758 | * @param Request $request |
||
759 | */ |
||
760 | public function bindRequest(Request $request) |
||
800 | |||
801 | /** |
||
802 | * Return current page. |
||
803 | * |
||
804 | * @return int |
||
805 | */ |
||
806 | public function getPage() |
||
810 | |||
811 | /** |
||
812 | * Return current sorting column. |
||
813 | * |
||
814 | * @return string |
||
815 | */ |
||
816 | public function getOrderBy() |
||
820 | |||
821 | /** |
||
822 | * Return current sorting direction. |
||
823 | * |
||
824 | * @return string |
||
825 | */ |
||
826 | public function getOrderDirection() |
||
830 | |||
831 | /** |
||
832 | * @param string $suffix |
||
833 | * |
||
834 | * @return string |
||
835 | */ |
||
836 | public function getPathByConvention($suffix = null) |
||
846 | |||
847 | /** |
||
848 | * Get controller path. |
||
849 | * |
||
850 | * @return string |
||
851 | */ |
||
852 | public function getControllerPath() |
||
856 | |||
857 | /** |
||
858 | * Return extra parameters for use in list actions. |
||
859 | * |
||
860 | * @return array |
||
861 | */ |
||
862 | public function getExtraParameters() |
||
866 | } |
||
867 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.