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 DataGrid 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 DataGrid, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class DataGrid extends Nette\Application\UI\Control |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | * @todo Tell about this on github |
||
| 21 | */ |
||
| 22 | public static $icon_prefix = 'fa fa-'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | * @persistent |
||
| 27 | */ |
||
| 28 | public $page = 1; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int |
||
| 32 | * @persistent |
||
| 33 | */ |
||
| 34 | public $per_page; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | * @persistent |
||
| 39 | */ |
||
| 40 | public $sort = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | * @persistent |
||
| 45 | */ |
||
| 46 | public $filter = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Callable[] |
||
| 50 | */ |
||
| 51 | public $onRender = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $items_per_page_list = [10, 20, 50]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $template_file; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var Column\IColumn[] |
||
| 65 | */ |
||
| 66 | protected $columns = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Column\Action[] |
||
| 70 | */ |
||
| 71 | protected $actions = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var GroupAction\GroupActionCollection |
||
| 75 | */ |
||
| 76 | protected $group_action_collection; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Filter\Filter[] |
||
| 80 | */ |
||
| 81 | protected $filters = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var Export\Export[] |
||
| 85 | */ |
||
| 86 | protected $exports = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var DataModel |
||
| 90 | */ |
||
| 91 | protected $dataModel; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var DataFilter |
||
| 95 | */ |
||
| 96 | protected $dataFilter; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $primary_key = 'id'; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $do_paginate = TRUE; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | protected $csv_export = TRUE; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $csv_export_filtered = TRUE; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var bool |
||
| 120 | */ |
||
| 121 | protected $sortable = FALSE; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | protected $original_template; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | protected $redraw_item; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var mixed |
||
| 135 | */ |
||
| 136 | protected $translator; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var bool |
||
| 140 | */ |
||
| 141 | protected $force_filter_active; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var callable |
||
| 145 | */ |
||
| 146 | protected $tree_view_children_callback; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | protected $tree_view_has_children_column; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var bool |
||
| 155 | */ |
||
| 156 | protected $outer_filter_rendering = FALSE; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var bool |
||
| 160 | */ |
||
| 161 | private $remember_state = TRUE; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var bool |
||
| 165 | */ |
||
| 166 | private $refresh_url = TRUE; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var Nette\Http\SessionSection |
||
| 170 | */ |
||
| 171 | private $grid_session; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var array |
||
| 175 | */ |
||
| 176 | private $items_detail = []; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var array |
||
| 180 | */ |
||
| 181 | private $row_conditions = [ |
||
| 182 | 'group_action' => FALSE, |
||
| 183 | 'action' => [] |
||
| 184 | ]; |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * @param Nette\ComponentModel\IContainer|NULL $parent |
||
| 189 | * @param string $name |
||
| 190 | */ |
||
| 191 | public function __construct(Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * {inheritDoc} |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function attached($presenter) |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Find some unique session key name |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getSessionSectionName() |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * Render template |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | public function render() |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Return current paginator class |
||
| 307 | * @return NULL|Components\DataGridPaginator\DataGridPaginator |
||
| 308 | */ |
||
| 309 | public function getPaginator() |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $primary_key |
||
| 321 | */ |
||
| 322 | public function setPrimaryKey($primary_key) |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Set Grid data source |
||
| 332 | * @param DataSource\IDataSource|array|\DibiFluent $source |
||
| 333 | * @return DataGrid |
||
| 334 | */ |
||
| 335 | public function setDataSource($source) |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Is filter active? |
||
| 375 | * @return boolean |
||
| 376 | */ |
||
| 377 | public function isFilterActive() |
||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * Tell that filter is active from whatever reasons |
||
| 387 | * return self |
||
| 388 | */ |
||
| 389 | public function setFilterActive() |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * If we want to sent some initial filter |
||
| 399 | * @param array $filter |
||
| 400 | */ |
||
| 401 | public function setFilter(array $filter) |
||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * Set options of select "items_per_page" |
||
| 411 | * @param array $items_per_page_list |
||
| 412 | */ |
||
| 413 | public function setItemsPerPageList(array $items_per_page_list) |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * Set custom template file to render |
||
| 423 | * @param string $template_file |
||
| 424 | */ |
||
| 425 | public function setTemplateFile($template_file) |
||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * Get DataGrid template file |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | public function getTemplateFile() |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Get DataGrid original template file |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getOriginalTemplateFile() |
||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * Order Grid to "be paginated" |
||
| 455 | * @param bool $do |
||
| 456 | */ |
||
| 457 | public function setPagination($do) |
||
| 463 | |||
| 464 | |||
| 465 | /** |
||
| 466 | * Tell whether Grid is paginated |
||
| 467 | * @return bool |
||
| 468 | */ |
||
| 469 | public function isPaginated() |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * Set grido to be sortable |
||
| 477 | * @param bool $sortable |
||
| 478 | */ |
||
| 479 | public function setSortable($sortable = TRUE) |
||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * Tell whether DataGrid is sortable |
||
| 493 | * @return bool |
||
| 494 | */ |
||
| 495 | public function isSortable() |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * Is tree view set? |
||
| 503 | * @return boolean |
||
| 504 | */ |
||
| 505 | public function isTreeView() |
||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * Setting tree view |
||
| 513 | * @param callable $get_children_callback |
||
| 514 | * @param string $tree_view_has_children_column |
||
| 515 | */ |
||
| 516 | public function setTreeView($get_children_callback, $tree_view_has_children_column = 'has_children') |
||
| 541 | |||
| 542 | |||
| 543 | /******************************************************************************** |
||
| 544 | * Columns * |
||
| 545 | ********************************************************************************/ |
||
| 546 | |||
| 547 | |||
| 548 | /** |
||
| 549 | * Add text column with no other formating |
||
| 550 | * @param string $key |
||
| 551 | * @param string $name |
||
| 552 | * @param string|null $column |
||
| 553 | * @return Column\Column |
||
| 554 | */ |
||
| 555 | public function addColumnText($key, $name, $column = NULL) |
||
| 562 | |||
| 563 | |||
| 564 | /** |
||
| 565 | * Add column with link |
||
| 566 | * @param string $key |
||
| 567 | * @param string $name |
||
| 568 | * @param string|null $column |
||
| 569 | * @return Column\Column |
||
| 570 | */ |
||
| 571 | public function addColumnLink($key, $name, $href = NULL, $column = NULL, array $params = NULL) |
||
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * Add column with possible number formating |
||
| 587 | * @param string $key |
||
| 588 | * @param string $name |
||
| 589 | * @param string|null $column |
||
| 590 | * @return Column\Column |
||
| 591 | */ |
||
| 592 | public function addColumnNumber($key, $name, $column = NULL) |
||
| 599 | |||
| 600 | |||
| 601 | /** |
||
| 602 | * Add column with date formating |
||
| 603 | * @param string $key |
||
| 604 | * @param string $name |
||
| 605 | * @param string|null $column |
||
| 606 | * @return Column\Column |
||
| 607 | */ |
||
| 608 | public function addColumnDateTime($key, $name, $column = NULL) |
||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * Return existing column |
||
| 619 | * @param string $key |
||
| 620 | * @return Column\Column |
||
| 621 | * @throws DataGridException |
||
| 622 | */ |
||
| 623 | public function getColumn($key) |
||
| 631 | |||
| 632 | |||
| 633 | /** |
||
| 634 | * Remove column |
||
| 635 | * @param string $key |
||
| 636 | * @return void |
||
| 637 | */ |
||
| 638 | public function removeColumn($key) |
||
| 642 | |||
| 643 | |||
| 644 | /** |
||
| 645 | * Check whether given key already exists in $this->columns |
||
| 646 | * @param string $key |
||
| 647 | * @throws DataGridException |
||
| 648 | */ |
||
| 649 | protected function addColumnCheck($key) |
||
| 655 | |||
| 656 | |||
| 657 | /******************************************************************************** |
||
| 658 | * Actions * |
||
| 659 | ********************************************************************************/ |
||
| 660 | |||
| 661 | |||
| 662 | /** |
||
| 663 | * Create action |
||
| 664 | * @param string $key |
||
| 665 | * @param string $name |
||
| 666 | * @param string $href |
||
| 667 | * @param array|null $params |
||
| 668 | */ |
||
| 669 | public function addAction($key, $name = '', $href = NULL, array $params = NULL) |
||
| 680 | |||
| 681 | |||
| 682 | /** |
||
| 683 | * Get existing action |
||
| 684 | * @param string $key |
||
| 685 | * @return Column\Action |
||
| 686 | * @throws DataGridException |
||
| 687 | */ |
||
| 688 | public function getAction($key) |
||
| 696 | |||
| 697 | |||
| 698 | /** |
||
| 699 | * Remove action |
||
| 700 | * @param string $key |
||
| 701 | * @return void |
||
| 702 | */ |
||
| 703 | public function removeAction($key) |
||
| 707 | |||
| 708 | |||
| 709 | /** |
||
| 710 | * Check whether given key already exists in $this->filters |
||
| 711 | * @param string $key |
||
| 712 | * @throws DataGridException |
||
| 713 | */ |
||
| 714 | protected function addActionCheck($key) |
||
| 720 | |||
| 721 | |||
| 722 | /******************************************************************************** |
||
| 723 | * Filters * |
||
| 724 | ********************************************************************************/ |
||
| 725 | |||
| 726 | |||
| 727 | /** |
||
| 728 | * Add filter fot text search |
||
| 729 | * @param string $key |
||
| 730 | * @param string $name |
||
| 731 | * @param array|string $columns |
||
| 732 | * @throws DataGridException |
||
| 733 | */ |
||
| 734 | public function addFilterText($key, $name, $columns = NULL) |
||
| 746 | |||
| 747 | |||
| 748 | /** |
||
| 749 | * Add select box filter |
||
| 750 | * @param string $key |
||
| 751 | * @param string $name |
||
| 752 | * @param array $options |
||
| 753 | * @param string $column |
||
| 754 | * @throws DataGridException |
||
| 755 | */ |
||
| 756 | View Code Duplication | public function addFilterSelect($key, $name, $options, $column = NULL) |
|
| 768 | |||
| 769 | |||
| 770 | /** |
||
| 771 | * Add datepicker filter |
||
| 772 | * @param string $key |
||
| 773 | * @param string $name |
||
| 774 | * @param string $column |
||
| 775 | * @throws DataGridException |
||
| 776 | */ |
||
| 777 | public function addFilterDate($key, $name, $column = NULL) |
||
| 789 | |||
| 790 | |||
| 791 | /** |
||
| 792 | * Add range filter (from - to) |
||
| 793 | * @param string $key |
||
| 794 | * @param string $name |
||
| 795 | * @param string $column |
||
| 796 | * @throws DataGridException |
||
| 797 | */ |
||
| 798 | View Code Duplication | public function addFilterRange($key, $name, $column = NULL, $name_second = '-') |
|
| 810 | |||
| 811 | |||
| 812 | /** |
||
| 813 | * Add datepicker filter (from - to) |
||
| 814 | * @param string $key |
||
| 815 | * @param string $name |
||
| 816 | * @param string $column |
||
| 817 | * @throws DataGridException |
||
| 818 | */ |
||
| 819 | View Code Duplication | public function addFilterDateRange($key, $name, $column = NULL, $name_second = '-') |
|
| 831 | |||
| 832 | |||
| 833 | /** |
||
| 834 | * Check whether given key already exists in $this->filters |
||
| 835 | * @param string $key |
||
| 836 | * @throws DataGridException |
||
| 837 | */ |
||
| 838 | protected function addFilterCheck($key) |
||
| 844 | |||
| 845 | |||
| 846 | /** |
||
| 847 | * Fill array of Filter\Filter[] with values from $this->filter persistent parameter |
||
| 848 | * Fill array of Column\Column[] with values from $this->sort persistent parameter |
||
| 849 | * @return Filter\Filter[] $this->filters === Filter\Filter[] |
||
| 850 | */ |
||
| 851 | public function assableFilters() |
||
| 879 | |||
| 880 | |||
| 881 | /** |
||
| 882 | * Try to restore session stuff |
||
| 883 | * @return void |
||
| 884 | */ |
||
| 885 | public function findSessionFilters() |
||
| 913 | |||
| 914 | |||
| 915 | /** |
||
| 916 | * Remove filter |
||
| 917 | * @param string $key |
||
| 918 | * @return void |
||
| 919 | */ |
||
| 920 | public function removeFilter($key) |
||
| 924 | |||
| 925 | |||
| 926 | /******************************************************************************** |
||
| 927 | * Exports * |
||
| 928 | ********************************************************************************/ |
||
| 929 | |||
| 930 | |||
| 931 | /** |
||
| 932 | * Add export of type callback |
||
| 933 | * @param string $text |
||
| 934 | * @param callable $callback |
||
| 935 | * @param boolean $filtered |
||
| 936 | */ |
||
| 937 | public function addExportCallback($text, $callback, $filtered = FALSE) |
||
| 945 | |||
| 946 | |||
| 947 | /** |
||
| 948 | * Add already implemented csv export |
||
| 949 | * @param string $text |
||
| 950 | * @param string $csv_file_name |
||
| 951 | */ |
||
| 952 | public function addExportCsv($text, $csv_file_name) |
||
| 956 | |||
| 957 | |||
| 958 | /** |
||
| 959 | * Add already implemented csv export, but for filtered data |
||
| 960 | * @param string $text |
||
| 961 | * @param string $csv_file_name |
||
| 962 | */ |
||
| 963 | public function addExportCsvFiltered($text, $csv_file_name) |
||
| 967 | |||
| 968 | |||
| 969 | /** |
||
| 970 | * Add export to array |
||
| 971 | * @param Export\Export $export |
||
| 972 | */ |
||
| 973 | protected function addToExports(Export\Export $export) |
||
| 981 | |||
| 982 | |||
| 983 | /******************************************************************************** |
||
| 984 | * Group actions * |
||
| 985 | ********************************************************************************/ |
||
| 986 | |||
| 987 | |||
| 988 | /** |
||
| 989 | * Add group actino |
||
| 990 | * @param string $title |
||
| 991 | * @param array $options |
||
| 992 | */ |
||
| 993 | public function addGroupAction($title, $options = []) |
||
| 997 | |||
| 998 | |||
| 999 | /** |
||
| 1000 | * Get collection of all group actions |
||
| 1001 | * @return GroupAction\GroupActionCollection |
||
| 1002 | */ |
||
| 1003 | public function getGroupActionCollection() |
||
| 1011 | |||
| 1012 | |||
| 1013 | /******************************************************************************** |
||
| 1014 | * Signals * |
||
| 1015 | ********************************************************************************/ |
||
| 1016 | |||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Handler for changind page (just refresh site with page as persistent paramter set) |
||
| 1020 | * @param int $page |
||
| 1021 | * @return void |
||
| 1022 | */ |
||
| 1023 | public function handlePage($page) |
||
| 1033 | |||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Handler for sorting |
||
| 1037 | * @return void |
||
| 1038 | */ |
||
| 1039 | public function handleSort(array $sort) |
||
| 1049 | |||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * handler for reseting the filter |
||
| 1053 | * @return void |
||
| 1054 | */ |
||
| 1055 | public function handleResetFilter() |
||
| 1077 | |||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * Handler for export |
||
| 1081 | * @param int $id Key for particular export class in array $this->exports |
||
| 1082 | * @return void |
||
| 1083 | */ |
||
| 1084 | public function handleExport($id) |
||
| 1114 | |||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Handler for getting children of parent item (e.g. category) |
||
| 1118 | * @param int $parent |
||
| 1119 | * @return void |
||
| 1120 | */ |
||
| 1121 | View Code Duplication | public function handleGetChildren($parent) |
|
| 1136 | |||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * Handler for getting item detail |
||
| 1140 | * @param mixed $id |
||
| 1141 | * @return void |
||
| 1142 | */ |
||
| 1143 | View Code Duplication | public function handleGetItemDetail($id) |
|
| 1155 | |||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Handler for inline editing |
||
| 1159 | * @param mixed $id |
||
| 1160 | * @param mixed $key |
||
| 1161 | * @return void |
||
| 1162 | */ |
||
| 1163 | public function handleEdit($id, $key) |
||
| 1170 | |||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Redraw $this |
||
| 1174 | * @return void |
||
| 1175 | */ |
||
| 1176 | public function reload($snippets = []) |
||
| 1197 | |||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Redraw just one row via ajax |
||
| 1201 | * @param int $id |
||
| 1202 | * @param mixed $primary_where_column |
||
| 1203 | * @return void |
||
| 1204 | */ |
||
| 1205 | public function redrawItem($id, $primary_where_column = NULL) |
||
| 1212 | |||
| 1213 | |||
| 1214 | /******************************************************************************** |
||
| 1215 | * Components * |
||
| 1216 | ********************************************************************************/ |
||
| 1217 | |||
| 1218 | |||
| 1219 | /** |
||
| 1220 | * Paginator factory |
||
| 1221 | * @return Components\DataGridPaginator\DataGridPaginator |
||
| 1222 | */ |
||
| 1223 | public function createComponentPaginator() |
||
| 1236 | |||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * PerPage form factory |
||
| 1240 | * @return Form |
||
| 1241 | */ |
||
| 1242 | public function createComponentPerPage() |
||
| 1268 | |||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * FilterAndGroupAction form factory |
||
| 1272 | * @return Form |
||
| 1273 | */ |
||
| 1274 | public function createComponentFilter() |
||
| 1304 | |||
| 1305 | |||
| 1306 | /** |
||
| 1307 | * Set $this->filter values after filter form submitted |
||
| 1308 | * @param Form $form |
||
| 1309 | * @return void |
||
| 1310 | */ |
||
| 1311 | public function filterSucceeded(Form $form) |
||
| 1337 | |||
| 1338 | |||
| 1339 | /******************************************************************************** |
||
| 1340 | * Support functions * |
||
| 1341 | ********************************************************************************/ |
||
| 1342 | |||
| 1343 | |||
| 1344 | public function resetExportsLinks() |
||
| 1350 | |||
| 1351 | |||
| 1352 | /** |
||
| 1353 | * Get parameter per_page |
||
| 1354 | * @return int |
||
| 1355 | */ |
||
| 1356 | public function getPerPage() |
||
| 1366 | |||
| 1367 | |||
| 1368 | /** |
||
| 1369 | * Get associative array of items_per_page_list |
||
| 1370 | * @return array |
||
| 1371 | */ |
||
| 1372 | public function getItemsPerPageList() |
||
| 1384 | |||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * Get primary key of datagrid data source |
||
| 1388 | * @return string |
||
| 1389 | */ |
||
| 1390 | public function getPrimaryKey() |
||
| 1394 | |||
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Get set of set columns |
||
| 1398 | * @return Column\IColumn[] |
||
| 1399 | */ |
||
| 1400 | public function getColumns() |
||
| 1404 | |||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Has datagrid some group actions? |
||
| 1408 | * @return boolean |
||
| 1409 | */ |
||
| 1410 | public function hasGroupActions() |
||
| 1414 | |||
| 1415 | |||
| 1416 | /** |
||
| 1417 | * Get translator for datagrid |
||
| 1418 | * @return Nette\Localization\ITranslator |
||
| 1419 | */ |
||
| 1420 | public function getTranslator() |
||
| 1428 | |||
| 1429 | |||
| 1430 | /** |
||
| 1431 | * Set datagrid translator |
||
| 1432 | * @param Nette\Localization\ITranslator $translator |
||
| 1433 | */ |
||
| 1434 | public function setTranslator(Nette\Localization\ITranslator $translator) |
||
| 1440 | |||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Should be datagrid filters rendered separately? |
||
| 1444 | * @param boolean $out |
||
| 1445 | */ |
||
| 1446 | public function setOuterFilterRendering($out = TRUE) |
||
| 1450 | |||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * Are datagrid filters rendered separately? |
||
| 1454 | * @return boolean |
||
| 1455 | */ |
||
| 1456 | public function hasOuterFilterRendering() |
||
| 1460 | |||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Set order of datagrid columns |
||
| 1464 | * @param array $order |
||
| 1465 | */ |
||
| 1466 | public function setColumnsOrder($order) |
||
| 1482 | |||
| 1483 | |||
| 1484 | /** |
||
| 1485 | * Should datagrid remember its filters/pagination/etc using session? |
||
| 1486 | * @param bool $remember |
||
| 1487 | */ |
||
| 1488 | public function setRememberState($remember = TRUE) |
||
| 1492 | |||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Should datagrid refresh url using history API? |
||
| 1496 | * @param bool $refresh |
||
| 1497 | */ |
||
| 1498 | public function setRefreshUrl($refresh = TRUE) |
||
| 1502 | |||
| 1503 | |||
| 1504 | /** |
||
| 1505 | * Get session data if functionality is enabled |
||
| 1506 | * @param string $key |
||
| 1507 | * @return mixed |
||
| 1508 | */ |
||
| 1509 | public function getSessionData($key = NULL) |
||
| 1517 | |||
| 1518 | |||
| 1519 | /** |
||
| 1520 | * Save session data - just if it is enabled |
||
| 1521 | * @param string $key |
||
| 1522 | * @param mixed $value |
||
| 1523 | * @return void |
||
| 1524 | */ |
||
| 1525 | public function saveSessionData($key, $value) |
||
| 1532 | |||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Delete session data |
||
| 1536 | * @return void |
||
| 1537 | */ |
||
| 1538 | public function deleteSesssionData($key) |
||
| 1542 | |||
| 1543 | |||
| 1544 | /** |
||
| 1545 | * Get items detail parameters |
||
| 1546 | * @return array |
||
| 1547 | */ |
||
| 1548 | public function getItemsDetail() |
||
| 1552 | |||
| 1553 | |||
| 1554 | /** |
||
| 1555 | * Items can have thair detail - toggled |
||
| 1556 | * @param mixed $detail callable|string|bool |
||
| 1557 | */ |
||
| 1558 | public function setItemsDetail($detail = TRUE, $primary_where_column = NULL) |
||
| 1597 | |||
| 1598 | |||
| 1599 | /** |
||
| 1600 | * Get cont of columns |
||
| 1601 | * @return int |
||
| 1602 | */ |
||
| 1603 | public function getColumnsCount() |
||
| 1617 | |||
| 1618 | |||
| 1619 | public function allowRowsGroupAction(callable $condition) |
||
| 1623 | |||
| 1624 | |||
| 1625 | public function allowRowsAction($key, callable $condition) |
||
| 1629 | |||
| 1630 | |||
| 1631 | public function getRowCondition($name, $key = NULL) |
||
| 1645 | |||
| 1646 | } |
||
| 1647 | |||
| 1652 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: