Complex classes like Grid 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 Grid, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class Grid |
||
| 39 | { |
||
| 40 | use Options; |
||
| 41 | use Helper; |
||
| 42 | |||
| 43 | const ORDER_ASC = 'asc'; |
||
| 44 | const ORDER_DESC = 'desc'; |
||
| 45 | |||
| 46 | const FILTER_LIKE = 'like'; // like |
||
| 47 | const FILTER_ENUM = 'enum'; // one from .., .., .. |
||
| 48 | const FILTER_NUM = 'num'; // ==, !=, >, >=, <, <= |
||
| 49 | |||
| 50 | const FILTER_EQ = 'eq'; // equal to .. |
||
| 51 | const FILTER_NE = 'ne'; // not equal to .. |
||
| 52 | const FILTER_GT = 'gt'; // greater than .. |
||
| 53 | const FILTER_GE = 'ge'; // greater than .. or equal |
||
| 54 | const FILTER_LT = 'lt'; // less than .. |
||
| 55 | const FILTER_LE = 'le'; // less than .. or equal |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Source\AbstractSource instance of Source |
||
| 59 | */ |
||
| 60 | protected $adapter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var Data instance of Data |
||
| 64 | */ |
||
| 65 | protected $data; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string unique identification of grid |
||
| 69 | */ |
||
| 70 | protected $uid; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string unique prefix of grid |
||
| 74 | */ |
||
| 75 | protected $prefix; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string location of Grid |
||
| 79 | */ |
||
| 80 | protected $module; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string location of Grid |
||
| 84 | */ |
||
| 85 | protected $controller; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array custom array params |
||
| 89 | */ |
||
| 90 | protected $params = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var integer start from first page |
||
| 94 | */ |
||
| 95 | protected $page = 1; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var integer limit per page |
||
| 99 | */ |
||
| 100 | protected $limit = 25; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var integer default value of page limit |
||
| 104 | * @see Grid::$limit |
||
| 105 | */ |
||
| 106 | protected $defaultLimit = 25; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * List of orders |
||
| 110 | * |
||
| 111 | * Example |
||
| 112 | * 'first' => 'ASC', |
||
| 113 | * 'last' => 'ASC' |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | protected $orders = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array default order |
||
| 121 | * @see Grid::$orders |
||
| 122 | */ |
||
| 123 | protected $defaultOrder; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var array list of allow orders |
||
| 127 | * @see Grid::$orders |
||
| 128 | */ |
||
| 129 | protected $allowOrders = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var array list of filters |
||
| 133 | */ |
||
| 134 | protected $filters = []; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * List of allow filters |
||
| 138 | * |
||
| 139 | * Example |
||
| 140 | * ['id', 'status' => ['active', 'disable']] |
||
| 141 | * |
||
| 142 | * @var array |
||
| 143 | * @see Grid::$filters |
||
| 144 | */ |
||
| 145 | protected $allowFilters = []; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * List of aliases for columns in DB |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $aliases = []; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Grid constructor |
||
| 156 | * |
||
| 157 | * @param array $options |
||
| 158 | */ |
||
| 159 | 34 | public function __construct($options = null) |
|
| 160 | { |
||
| 161 | 34 | if ($options) { |
|
| 162 | $this->setOptions($options); |
||
| 163 | } |
||
| 164 | |||
| 165 | 34 | if ($this->getUid()) { |
|
| 166 | 34 | $this->prefix = $this->getUid() . '-'; |
|
| 167 | } else { |
||
| 168 | $this->prefix = ''; |
||
| 169 | } |
||
| 170 | |||
| 171 | 34 | $this->init(); |
|
| 172 | |||
| 173 | 34 | $this->processRequest(); |
|
| 174 | |||
| 175 | // initial default helper path |
||
| 176 | 34 | $this->addHelperPath(dirname(__FILE__) . '/Helper/'); |
|
| 177 | 34 | } |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Initialize Grid |
||
| 181 | * |
||
| 182 | * @return Grid |
||
| 183 | */ |
||
| 184 | abstract public function init(); |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set source adapter |
||
| 188 | * |
||
| 189 | * @param Source\AbstractSource $adapter |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | 34 | public function setAdapter(Source\AbstractSource $adapter) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Get source adapter |
||
| 199 | * |
||
| 200 | * @return Source\AbstractSource |
||
| 201 | * @throws GridException |
||
| 202 | */ |
||
| 203 | 15 | public function getAdapter() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get unique Grid Id |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 34 | public function getUid() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get prefix |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | 1 | public function getPrefix() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Set module |
||
| 233 | * |
||
| 234 | * @param string $module |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | 1 | public function setModule($module) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Get module |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | 13 | public function getModule() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Set controller |
||
| 254 | * |
||
| 255 | * @param string $controller |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | 1 | public function setController($controller) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Get controller |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | 13 | public function getController() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Process request |
||
| 275 | * |
||
| 276 | * Example of request url |
||
| 277 | * - http://domain.com/pages/grid/ |
||
| 278 | * - http://domain.com/pages/grid/page/2/ |
||
| 279 | * - http://domain.com/pages/grid/page/2/order-alias/desc/ |
||
| 280 | * - http://domain.com/pages/grid/page/2/order-created/desc/order-alias/asc/ |
||
| 281 | * |
||
| 282 | * with prefix for support more than one grid on page |
||
| 283 | * - http://domain.com/users/grid/users-page/2/users-order-created/desc/ |
||
| 284 | * - http://domain.com/users/grid/users-page/2/users-filter-status/active/ |
||
| 285 | * |
||
| 286 | * hash support |
||
| 287 | * - http://domain.com/pages/grid/#/page/2/order-created/desc/order-alias/asc/ |
||
| 288 | * |
||
| 289 | * @return Grid |
||
| 290 | */ |
||
| 291 | 34 | public function processRequest() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Process source |
||
| 342 | * |
||
| 343 | * @return self |
||
| 344 | * @throws GridException |
||
| 345 | */ |
||
| 346 | 15 | public function processSource() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Get data |
||
| 363 | * |
||
| 364 | * @return Data |
||
| 365 | */ |
||
| 366 | 15 | public function getData() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Get settings |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | 15 | public function getSettings() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Setup params |
||
| 392 | * |
||
| 393 | * @param $params |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | public function setParams($params) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Return params prepared for url builder |
||
| 403 | * |
||
| 404 | * @param array $rewrite |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | 13 | public function getParams(array $rewrite = []) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get Url |
||
| 454 | * |
||
| 455 | * @param array $params |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | 13 | public function getUrl($params) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Add column name for allow order |
||
| 473 | * |
||
| 474 | * @param string $column |
||
| 475 | * @return void |
||
| 476 | */ |
||
| 477 | 34 | public function addAllowOrder($column) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Set allow orders |
||
| 484 | * |
||
| 485 | * @param string[] $orders |
||
| 486 | * @return void |
||
| 487 | */ |
||
| 488 | 34 | public function setAllowOrders(array $orders = []) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get allow orders |
||
| 498 | * |
||
| 499 | * @return array |
||
| 500 | */ |
||
| 501 | 8 | public function getAllowOrders() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Check order column |
||
| 508 | * |
||
| 509 | * @param string $column |
||
| 510 | * @return bool |
||
| 511 | */ |
||
| 512 | 8 | protected function checkOrderColumn($column) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Check order name |
||
| 519 | * |
||
| 520 | * @param string $order |
||
| 521 | * @return bool |
||
| 522 | */ |
||
| 523 | 6 | protected function checkOrderName($order) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Add order rule |
||
| 530 | * |
||
| 531 | * @param string $column |
||
| 532 | * @param string $order |
||
| 533 | * @return void |
||
| 534 | * @throws GridException |
||
| 535 | */ |
||
| 536 | 8 | public function addOrder($column, $order = Grid::ORDER_ASC) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Add order rules |
||
| 551 | * |
||
| 552 | * @param array $orders |
||
| 553 | * @return void |
||
| 554 | */ |
||
| 555 | 1 | public function addOrders(array $orders) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Set order |
||
| 564 | * |
||
| 565 | * @param string $column |
||
| 566 | * @param string $order ASC or DESC |
||
| 567 | * @return void |
||
| 568 | */ |
||
| 569 | 6 | public function setOrder($column, $order = Grid::ORDER_ASC) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Set orders |
||
| 577 | * |
||
| 578 | * @param array $orders |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | 1 | public function setOrders(array $orders) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Get orders |
||
| 589 | * |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | 25 | public function getOrders() |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Add column name to allow filter it |
||
| 610 | * |
||
| 611 | * @param string $column |
||
| 612 | * @return void |
||
| 613 | */ |
||
| 614 | 34 | public function addAllowFilter($column) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Set allowed filters |
||
| 621 | * |
||
| 622 | * @param string[] $filters |
||
| 623 | * @return void |
||
| 624 | */ |
||
| 625 | 34 | public function setAllowFilters(array $filters = []) |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Get allow filters |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | 12 | public function getAllowFilters() |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Check filter column |
||
| 645 | * |
||
| 646 | * @param string $column |
||
| 647 | * @return bool |
||
| 648 | */ |
||
| 649 | 12 | protected function checkFilterColumn($column) |
|
| 659 | |||
| 660 | /** |
||
| 661 | * Check filter |
||
| 662 | * |
||
| 663 | * @param string $filter |
||
| 664 | * @return bool |
||
| 665 | */ |
||
| 666 | 12 | protected function checkFilterName($filter) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * Add filter |
||
| 685 | * |
||
| 686 | * @param string $column name |
||
| 687 | * @param string $filter |
||
| 688 | * @param string $value |
||
| 689 | * @return void |
||
| 690 | * @throws GridException |
||
| 691 | */ |
||
| 692 | 10 | public function addFilter($column, $filter, $value) |
|
| 705 | |||
| 706 | |||
| 707 | /** |
||
| 708 | * Get filter |
||
| 709 | * |
||
| 710 | * @param string $column |
||
| 711 | * @param string $filter |
||
| 712 | * @return mixed |
||
| 713 | */ |
||
| 714 | public function getFilter($column, $filter = null) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Get filters |
||
| 725 | * |
||
| 726 | * @return array |
||
| 727 | */ |
||
| 728 | 23 | public function getFilters() |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Add alias for column name |
||
| 735 | * |
||
| 736 | * @param string $column |
||
| 737 | * @param string $alias |
||
| 738 | * @return void |
||
| 739 | */ |
||
| 740 | 32 | public function addAlias($column, $alias) |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Get column name by alias |
||
| 747 | * |
||
| 748 | * @param string $alias |
||
| 749 | * @return string |
||
| 750 | */ |
||
| 751 | protected function reverseAlias($alias) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Get alias by column name |
||
| 758 | * |
||
| 759 | * @param string $column |
||
| 760 | * @return string |
||
| 761 | */ |
||
| 762 | 34 | protected function applyAlias($column) |
|
| 766 | |||
| 767 | /** |
||
| 768 | * Set page |
||
| 769 | * |
||
| 770 | * @param integer $page |
||
| 771 | * @return void |
||
| 772 | * @throws GridException |
||
| 773 | */ |
||
| 774 | 34 | public function setPage($page = 1) |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Get page |
||
| 784 | * |
||
| 785 | * @return integer |
||
| 786 | */ |
||
| 787 | 17 | public function getPage() |
|
| 791 | |||
| 792 | /** |
||
| 793 | * Set limit per page |
||
| 794 | * |
||
| 795 | * @param integer $limit |
||
| 796 | * @return void |
||
| 797 | * @throws GridException |
||
| 798 | */ |
||
| 799 | 34 | public function setLimit($limit) |
|
| 806 | |||
| 807 | /** |
||
| 808 | * Get limit per page |
||
| 809 | * |
||
| 810 | * @return integer |
||
| 811 | */ |
||
| 812 | 24 | public function getLimit() |
|
| 816 | |||
| 817 | /** |
||
| 818 | * Set default limit |
||
| 819 | * |
||
| 820 | * @param integer $limit |
||
| 821 | * @return void |
||
| 822 | * @throws GridException |
||
| 823 | */ |
||
| 824 | 34 | public function setDefaultLimit($limit) |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Get default limit |
||
| 836 | * |
||
| 837 | * @return integer |
||
| 838 | */ |
||
| 839 | 1 | public function getDefaultLimit() |
|
| 843 | |||
| 844 | /** |
||
| 845 | * Set default order |
||
| 846 | * |
||
| 847 | * @param string $column |
||
| 848 | * @param string $order ASC or DESC |
||
| 849 | * @return void |
||
| 850 | * @throws GridException |
||
| 851 | */ |
||
| 852 | 5 | public function setDefaultOrder($column, $order = Grid::ORDER_ASC) |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Get default order |
||
| 861 | * |
||
| 862 | * @return array |
||
| 863 | */ |
||
| 864 | 24 | public function getDefaultOrder() |
|
| 868 | } |
||
| 869 |