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 = '@KunstmaanAdminList/Default/list.html.twig'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $addTemplate = '@KunstmaanAdminList/Default/add_or_edit.html.twig'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $editTemplate = '@KunstmaanAdminList/Default/add_or_edit.html.twig'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private $viewTemplate = '@KunstmaanAdminList/Default/view.html.twig'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | private $deleteTemplate = '@KunstmaanAdminList/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) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $type |
||
| 290 | * |
||
| 291 | * @return AbstractAdminListConfigurator |
||
| 292 | */ |
||
| 293 | 6 | public function setAdminType($type) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param array $typeOptions |
||
| 302 | * |
||
| 303 | * @return AbstractAdminListConfigurator |
||
| 304 | */ |
||
| 305 | 5 | public function setAdminTypeOptions($typeOptions) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Return the default form admin type options |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | 1 | public function getAdminTypeOptions() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @param object|array $item |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | 1 | public function canEdit($item) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Configure if it's possible to delete the given $item |
||
| 334 | * |
||
| 335 | * @param object|array $item |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | 1 | public function canDelete($item) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Configure if it's possible to add new items |
||
| 346 | * |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | 1 | public function canAdd() |
|
| 353 | |||
| 354 | 1 | public function canView($item) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Configure if it's possible to add new items |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | 1 | public function canExport() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @param string $name The field name |
||
| 371 | * @param string $header The header title |
||
| 372 | * @param bool $sort Sortable column or not |
||
| 373 | * @param string $template The template |
||
| 374 | * @param FieldAlias $alias The alias |
||
| 375 | * |
||
| 376 | * @return AbstractAdminListConfigurator |
||
| 377 | */ |
||
| 378 | 8 | public function addField($name, $header, $sort, $template = null, FieldAlias $alias = null) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $name The field name |
||
| 387 | * @param string $header The header title |
||
| 388 | * @param string $template The template |
||
| 389 | * @param FieldAlias $alias The alias |
||
| 390 | * |
||
| 391 | * @return AbstractAdminListConfigurator |
||
| 392 | */ |
||
| 393 | 1 | public function addExportField($name, $header, $template = null, FieldAlias $alias = null) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $columnName The column name |
||
| 402 | * @param FilterTypeInterface $type The filter type |
||
| 403 | * @param string $filterName The name of the filter |
||
| 404 | * @param array $options Options |
||
| 405 | * |
||
| 406 | * @return AbstractAdminListConfigurator |
||
| 407 | */ |
||
| 408 | 5 | public function addFilter( |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @return int |
||
| 421 | */ |
||
| 422 | 3 | public function getLimit() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | 1 | public function getSortFields() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * @return Field[] |
||
| 444 | */ |
||
| 445 | 9 | public function getFields() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * @return Field[] |
||
| 452 | */ |
||
| 453 | 2 | public function getExportFields() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $label The label, only used when the template equals null |
||
| 464 | * @param callable $routeGenerator The generator used to generate the url of an item, when generating the item will |
||
| 465 | * be provided |
||
| 466 | * @param string $icon The icon, only used when the template equals null |
||
| 467 | * @param string $template The template, when not specified the label is shown |
||
| 468 | * |
||
| 469 | * @return AbstractAdminListConfigurator |
||
| 470 | */ |
||
| 471 | 1 | public function addSimpleItemAction($label, $routeGenerator, $icon, $template = null) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @param ItemActionInterface $itemAction |
||
| 478 | * |
||
| 479 | * @return AbstractAdminListConfigurator |
||
| 480 | */ |
||
| 481 | 4 | public function addItemAction(ItemActionInterface $itemAction) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | 1 | public function hasItemActions() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @return ItemActionInterface[] |
||
| 498 | */ |
||
| 499 | 3 | public function getItemActions() |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param ListActionInterface $listAction |
||
| 506 | * |
||
| 507 | * @return AdminListConfiguratorInterface |
||
| 508 | */ |
||
| 509 | 1 | public function addListAction(ListActionInterface $listAction) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @return bool |
||
| 518 | */ |
||
| 519 | 1 | public function hasListActions() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * @return ListActionInterface[] |
||
| 526 | */ |
||
| 527 | 1 | public function getListActions() |
|
| 531 | |||
| 532 | /** |
||
| 533 | * @param BulkActionInterface $bulkAction |
||
| 534 | * |
||
| 535 | * @return AdminListConfiguratorInterface |
||
| 536 | */ |
||
| 537 | 1 | public function addBulkAction(BulkActionInterface $bulkAction) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * @return bool |
||
| 546 | */ |
||
| 547 | 1 | public function hasBulkActions() |
|
| 551 | |||
| 552 | /** |
||
| 553 | * @return BulkActionInterface[] |
||
| 554 | */ |
||
| 555 | 1 | public function getBulkActions() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | 2 | public function getListTemplate() |
|
| 567 | |||
| 568 | /** |
||
| 569 | * @param string $template |
||
| 570 | * |
||
| 571 | * @return AdminListConfiguratorInterface |
||
| 572 | */ |
||
| 573 | 1 | public function setListTemplate($template) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * @param array|object $item The item |
||
| 582 | * @param string $columnName The column name |
||
| 583 | * |
||
| 584 | * @return mixed |
||
| 585 | */ |
||
| 586 | 2 | public function getValue($item, $columnName) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * @param array|object $item The item |
||
| 609 | * @param string $columnName The column name |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | 1 | public function getStringValue($item, $columnName) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | 1 | public function getAddTemplate() |
|
| 650 | |||
| 651 | /** |
||
| 652 | * @param string $template |
||
| 653 | * |
||
| 654 | * @return AdminListConfiguratorInterface |
||
| 655 | */ |
||
| 656 | 1 | public function setAddTemplate($template) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * @return string |
||
| 665 | */ |
||
| 666 | 1 | public function getViewTemplate() |
|
| 670 | |||
| 671 | /** |
||
| 672 | * @param string $template |
||
| 673 | * |
||
| 674 | * @return AdminListConfiguratorInterface |
||
| 675 | */ |
||
| 676 | 1 | public function setViewTemplate($template) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * @return string |
||
| 685 | */ |
||
| 686 | 1 | public function getEditTemplate() |
|
| 690 | |||
| 691 | /** |
||
| 692 | * @param string $template |
||
| 693 | * |
||
| 694 | * @return AdminListConfiguratorInterface |
||
| 695 | */ |
||
| 696 | 1 | public function setEditTemplate($template) |
|
| 702 | |||
| 703 | /** |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | 1 | public function getDeleteTemplate() |
|
| 710 | |||
| 711 | /** |
||
| 712 | * @param string $template |
||
| 713 | * |
||
| 714 | * @return AdminListConfiguratorInterface |
||
| 715 | */ |
||
| 716 | 1 | public function setDeleteTemplate($template) |
|
| 722 | |||
| 723 | /** |
||
| 724 | * You can override this method to do some custom things you need to do when adding an entity |
||
| 725 | * |
||
| 726 | * @param object $entity |
||
| 727 | * |
||
| 728 | * @return mixed |
||
| 729 | */ |
||
| 730 | 1 | public function decorateNewEntity($entity) |
|
| 734 | |||
| 735 | /** |
||
| 736 | * @return FilterBuilder |
||
| 737 | */ |
||
| 738 | 10 | public function getFilterBuilder() |
|
| 746 | |||
| 747 | /** |
||
| 748 | * @param FilterBuilder $filterBuilder |
||
| 749 | * |
||
| 750 | * @return AbstractAdminListConfigurator |
||
| 751 | */ |
||
| 752 | 2 | public function setFilterBuilder(FilterBuilder $filterBuilder) |
|
| 758 | |||
| 759 | /** |
||
| 760 | * Bind current request. |
||
| 761 | * |
||
| 762 | * @param Request $request |
||
| 763 | */ |
||
| 764 | 4 | public function bindRequest(Request $request) |
|
| 804 | |||
| 805 | /** |
||
| 806 | * Return current page. |
||
| 807 | * |
||
| 808 | * @return int |
||
| 809 | */ |
||
| 810 | 3 | public function getPage() |
|
| 814 | |||
| 815 | /** |
||
| 816 | * Return current sorting column. |
||
| 817 | * |
||
| 818 | * @return string |
||
| 819 | */ |
||
| 820 | 1 | public function getOrderBy() |
|
| 824 | |||
| 825 | /** |
||
| 826 | * Return current sorting direction. |
||
| 827 | * |
||
| 828 | * @return string |
||
| 829 | */ |
||
| 830 | 1 | public function getOrderDirection() |
|
| 834 | |||
| 835 | /** |
||
| 836 | * @param string $suffix |
||
| 837 | * |
||
| 838 | * @return string |
||
| 839 | */ |
||
| 840 | 9 | public function getPathByConvention($suffix = null) |
|
| 850 | |||
| 851 | /** |
||
| 852 | * Get controller path. |
||
| 853 | * |
||
| 854 | * @return string |
||
| 855 | */ |
||
| 856 | 1 | public function getControllerPath() |
|
| 860 | |||
| 861 | /** |
||
| 862 | * Return extra parameters for use in list actions. |
||
| 863 | * |
||
| 864 | * @return array |
||
| 865 | */ |
||
| 866 | 9 | public function getExtraParameters() |
|
| 870 | } |
||
| 871 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.