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; |
||
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; |
||
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 | 8 | 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 | 1 | public function buildExportFields() |
|
170 | |||
171 | /** |
||
172 | * Build iterator (if needed) |
||
173 | */ |
||
174 | public function buildIterator() |
||
177 | |||
178 | /** |
||
179 | * Reset all built members |
||
180 | */ |
||
181 | 1 | 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 | 1 | public function getAddUrlFor(array $params = array()) |
|
214 | |||
215 | /** |
||
216 | * Get the url to export the listed items |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | 1 | public function getExportUrl() |
|
229 | |||
230 | /** |
||
231 | * Get the view url for the given $item |
||
232 | * |
||
233 | * @param object|array $item |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | 1 | public function getViewUrlFor($item) |
|
252 | |||
253 | /** |
||
254 | * Return the url to list all the items |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | 1 | public function getIndexUrl() |
|
267 | |||
268 | /** |
||
269 | * @param object $entity |
||
270 | * |
||
271 | * @throws InvalidArgumentException |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | 3 | public function getAdminType($entity) |
|
290 | |||
291 | /** |
||
292 | * @param string $type |
||
293 | * |
||
294 | * @return AbstractAdminListConfigurator |
||
295 | */ |
||
296 | 6 | public function setAdminType($type) |
|
302 | |||
303 | /** |
||
304 | * @param array $typeOptions |
||
305 | * |
||
306 | * @return AbstractAdminListConfigurator |
||
307 | */ |
||
308 | 5 | public function setAdminTypeOptions($typeOptions) |
|
314 | |||
315 | /** |
||
316 | * Return the default form admin type options |
||
317 | * |
||
318 | * @return array |
||
319 | */ |
||
320 | 1 | public function getAdminTypeOptions() |
|
324 | |||
325 | /** |
||
326 | * @param object|array $item |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | 1 | public function canEdit($item) |
|
334 | |||
335 | /** |
||
336 | * Configure if it's possible to delete the given $item |
||
337 | * |
||
338 | * @param object|array $item |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | 1 | public function canDelete($item) |
|
346 | |||
347 | /** |
||
348 | * Configure if it's possible to add new items |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | 1 | public function canAdd() |
|
356 | |||
357 | 1 | public function canView($item) |
|
361 | |||
362 | /** |
||
363 | * Configure if it's possible to add new items |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | 1 | public function canExport() |
|
371 | |||
372 | /** |
||
373 | * @param string $name The field name |
||
374 | * @param string $header The header title |
||
375 | * @param bool $sort Sortable column or not |
||
376 | * @param string $template The template |
||
377 | * @param FieldAlias $alias The alias |
||
378 | * |
||
379 | * @return AbstractAdminListConfigurator |
||
380 | */ |
||
381 | 8 | public function addField($name, $header, $sort, $template = null, FieldAlias $alias = null) |
|
387 | |||
388 | /** |
||
389 | * @param string $name The field name |
||
390 | * @param string $header The header title |
||
391 | * @param string $template The template |
||
392 | * @param FieldAlias $alias The alias |
||
393 | * |
||
394 | * @return AbstractAdminListConfigurator |
||
395 | */ |
||
396 | 1 | public function addExportField($name, $header, $template = null, FieldAlias $alias = null) |
|
402 | |||
403 | /** |
||
404 | * @param string $columnName The column name |
||
405 | * @param FilterTypeInterface $type The filter type |
||
406 | * @param string $filterName The name of the filter |
||
407 | * @param array $options Options |
||
408 | * |
||
409 | * @return AbstractAdminListConfigurator |
||
410 | */ |
||
411 | 5 | public function addFilter( |
|
421 | |||
422 | /** |
||
423 | * @return int |
||
424 | */ |
||
425 | 3 | public function getLimit() |
|
429 | |||
430 | /** |
||
431 | * @return array |
||
432 | */ |
||
433 | 1 | public function getSortFields() |
|
444 | |||
445 | /** |
||
446 | * @return Field[] |
||
447 | */ |
||
448 | 9 | public function getFields() |
|
452 | |||
453 | /** |
||
454 | * @return Field[] |
||
455 | */ |
||
456 | 2 | public function getExportFields() |
|
464 | |||
465 | /** |
||
466 | * @param string $label The label, only used when the template equals null |
||
467 | * @param callable $routeGenerator The generator used to generate the url of an item, when generating the item will |
||
468 | * be provided |
||
469 | * @param string $icon The icon, only used when the template equals null |
||
470 | * @param string $template The template, when not specified the label is shown |
||
471 | * |
||
472 | * @return AbstractAdminListConfigurator |
||
473 | */ |
||
474 | 1 | public function addSimpleItemAction($label, $routeGenerator, $icon, $template = null) |
|
478 | |||
479 | /** |
||
480 | * @param ItemActionInterface $itemAction |
||
481 | * |
||
482 | * @return AbstractAdminListConfigurator |
||
483 | */ |
||
484 | 4 | public function addItemAction(ItemActionInterface $itemAction) |
|
490 | |||
491 | /** |
||
492 | * @return bool |
||
493 | */ |
||
494 | 1 | public function hasItemActions() |
|
498 | |||
499 | /** |
||
500 | * @return ItemActionInterface[] |
||
501 | */ |
||
502 | 3 | public function getItemActions() |
|
506 | |||
507 | /** |
||
508 | * @param ListActionInterface $listAction |
||
509 | * |
||
510 | * @return AdminListConfiguratorInterface |
||
511 | */ |
||
512 | 1 | public function addListAction(ListActionInterface $listAction) |
|
518 | |||
519 | /** |
||
520 | * @return bool |
||
521 | */ |
||
522 | 1 | public function hasListActions() |
|
526 | |||
527 | /** |
||
528 | * @return ListActionInterface[] |
||
529 | */ |
||
530 | 1 | public function getListActions() |
|
534 | |||
535 | /** |
||
536 | * @param BulkActionInterface $bulkAction |
||
537 | * |
||
538 | * @return AdminListConfiguratorInterface |
||
539 | */ |
||
540 | 1 | public function addBulkAction(BulkActionInterface $bulkAction) |
|
546 | |||
547 | /** |
||
548 | * @return bool |
||
549 | */ |
||
550 | 1 | public function hasBulkActions() |
|
554 | |||
555 | /** |
||
556 | * @return BulkActionInterface[] |
||
557 | */ |
||
558 | 1 | public function getBulkActions() |
|
562 | |||
563 | /** |
||
564 | * @return string |
||
565 | */ |
||
566 | 2 | public function getListTemplate() |
|
570 | |||
571 | /** |
||
572 | * @param string $template |
||
573 | * |
||
574 | * @return AdminListConfiguratorInterface |
||
575 | */ |
||
576 | 1 | public function setListTemplate($template) |
|
582 | |||
583 | /** |
||
584 | * @param array|object $item The item |
||
585 | * @param string $columnName The column name |
||
586 | * |
||
587 | * @return mixed |
||
588 | */ |
||
589 | 2 | public function getValue($item, $columnName) |
|
609 | |||
610 | /** |
||
611 | * @param array|object $item The item |
||
612 | * @param string $columnName The column name |
||
613 | * |
||
614 | * @return string |
||
615 | */ |
||
616 | 1 | public function getStringValue($item, $columnName) |
|
645 | |||
646 | /** |
||
647 | * @return string |
||
648 | */ |
||
649 | 1 | public function getAddTemplate() |
|
653 | |||
654 | /** |
||
655 | * @param string $template |
||
656 | * |
||
657 | * @return AdminListConfiguratorInterface |
||
658 | */ |
||
659 | 1 | public function setAddTemplate($template) |
|
665 | |||
666 | /** |
||
667 | * @return string |
||
668 | */ |
||
669 | 1 | public function getViewTemplate() |
|
673 | |||
674 | /** |
||
675 | * @param string $template |
||
676 | * |
||
677 | * @return AdminListConfiguratorInterface |
||
678 | */ |
||
679 | 1 | public function setViewTemplate($template) |
|
685 | |||
686 | /** |
||
687 | * @return string |
||
688 | */ |
||
689 | 1 | public function getEditTemplate() |
|
693 | |||
694 | /** |
||
695 | * @param string $template |
||
696 | * |
||
697 | * @return AdminListConfiguratorInterface |
||
698 | */ |
||
699 | 1 | public function setEditTemplate($template) |
|
705 | |||
706 | /** |
||
707 | * @return string |
||
708 | */ |
||
709 | 1 | public function getDeleteTemplate() |
|
713 | |||
714 | /** |
||
715 | * @param string $template |
||
716 | * |
||
717 | * @return AdminListConfiguratorInterface |
||
718 | */ |
||
719 | 1 | public function setDeleteTemplate($template) |
|
725 | |||
726 | /** |
||
727 | * You can override this method to do some custom things you need to do when adding an entity |
||
728 | * |
||
729 | * @param object $entity |
||
730 | * |
||
731 | * @return mixed |
||
732 | */ |
||
733 | 1 | public function decorateNewEntity($entity) |
|
737 | |||
738 | /** |
||
739 | * @return FilterBuilder |
||
740 | */ |
||
741 | 10 | public function getFilterBuilder() |
|
749 | |||
750 | /** |
||
751 | * @param FilterBuilder $filterBuilder |
||
752 | * |
||
753 | * @return AbstractAdminListConfigurator |
||
754 | */ |
||
755 | 2 | public function setFilterBuilder(FilterBuilder $filterBuilder) |
|
761 | |||
762 | /** |
||
763 | * Bind current request. |
||
764 | * |
||
765 | * @param Request $request |
||
766 | */ |
||
767 | 4 | public function bindRequest(Request $request) |
|
807 | |||
808 | /** |
||
809 | * Return current page. |
||
810 | * |
||
811 | * @return int |
||
812 | */ |
||
813 | 3 | public function getPage() |
|
817 | |||
818 | /** |
||
819 | * Return current sorting column. |
||
820 | * |
||
821 | * @return string |
||
822 | */ |
||
823 | 1 | public function getOrderBy() |
|
827 | |||
828 | /** |
||
829 | * Return current sorting direction. |
||
830 | * |
||
831 | * @return string |
||
832 | */ |
||
833 | 1 | public function getOrderDirection() |
|
837 | |||
838 | /** |
||
839 | * @param string $suffix |
||
840 | * |
||
841 | * @return string |
||
842 | */ |
||
843 | 9 | public function getPathByConvention($suffix = null) |
|
853 | |||
854 | /** |
||
855 | * Get controller path. |
||
856 | * |
||
857 | * @return string |
||
858 | */ |
||
859 | 1 | public function getControllerPath() |
|
863 | |||
864 | /** |
||
865 | * Return extra parameters for use in list actions. |
||
866 | * |
||
867 | * @return array |
||
868 | */ |
||
869 | 9 | public function getExtraParameters() |
|
873 | } |
||
874 |
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.