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 | |||
| 49 | const FILTER_EQ = 'eq'; // equal to .. |
||
| 50 | const FILTER_NE = 'ne'; // not equal to .. |
||
| 51 | const FILTER_GT = 'gt'; // greater than .. |
||
| 52 | const FILTER_GE = 'ge'; // greater than .. or equal |
||
| 53 | const FILTER_LT = 'lt'; // less than .. |
||
| 54 | const FILTER_LE = 'le'; // less than .. or equal |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Source\AbstractSource instance of Source |
||
| 58 | */ |
||
| 59 | protected $adapter; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Data instance of Data |
||
| 63 | */ |
||
| 64 | protected $data; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string unique identification of grid |
||
| 68 | */ |
||
| 69 | protected $uid; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string unique prefix of grid |
||
| 73 | */ |
||
| 74 | protected $prefix; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string location of Grid |
||
| 78 | */ |
||
| 79 | protected $module; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string location of Grid |
||
| 83 | */ |
||
| 84 | protected $controller; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array custom array params |
||
| 88 | */ |
||
| 89 | protected $params = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var integer start from first page |
||
| 93 | */ |
||
| 94 | protected $page = 1; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var integer limit per page |
||
| 98 | */ |
||
| 99 | protected $limit = 25; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var integer default value of page limit |
||
| 103 | * @see Grid::$limit |
||
| 104 | */ |
||
| 105 | protected $defaultLimit = 25; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * List of orders |
||
| 109 | * |
||
| 110 | * Example |
||
| 111 | * 'first' => 'ASC', |
||
| 112 | * 'last' => 'ASC' |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $orders = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var array default order |
||
| 120 | * @see Grid::$orders |
||
| 121 | */ |
||
| 122 | protected $defaultOrder; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var array list of allow orders |
||
| 126 | * @see Grid::$orders |
||
| 127 | */ |
||
| 128 | protected $allowOrders = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var array list of filters |
||
| 132 | */ |
||
| 133 | protected $filters = []; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * List of allow filters |
||
| 137 | * |
||
| 138 | * Example |
||
| 139 | * ['id', 'status' => ['active', 'disable']] |
||
| 140 | * |
||
| 141 | * @var array |
||
| 142 | * @see Grid::$filters |
||
| 143 | */ |
||
| 144 | protected $allowFilters = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * List of allow filter names |
||
| 148 | * |
||
| 149 | * @var array |
||
| 150 | * @see Grid::$filters |
||
| 151 | */ |
||
| 152 | protected $allowFilterNames = [ |
||
| 153 | self::FILTER_LIKE, |
||
| 154 | self::FILTER_ENUM, |
||
| 155 | self::FILTER_EQ, |
||
| 156 | self::FILTER_NE, |
||
| 157 | self::FILTER_GT, |
||
| 158 | self::FILTER_GE, |
||
| 159 | self::FILTER_LT, |
||
| 160 | self::FILTER_LE |
||
| 161 | ]; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * List of aliases for columns in DB |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | protected $aliases = []; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Grid constructor |
||
| 172 | * |
||
| 173 | * @param array $options |
||
| 174 | */ |
||
| 175 | 34 | public function __construct($options = null) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Initialize Grid |
||
| 197 | * |
||
| 198 | * @return Grid |
||
| 199 | */ |
||
| 200 | abstract public function init(); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Set source adapter |
||
| 204 | * |
||
| 205 | * @param Source\AbstractSource $adapter |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | 34 | public function setAdapter(Source\AbstractSource $adapter) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get source adapter |
||
| 215 | * |
||
| 216 | * @return Source\AbstractSource |
||
| 217 | * @throws GridException |
||
| 218 | */ |
||
| 219 | 15 | public function getAdapter() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Get unique Grid Id |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 34 | public function getUid() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Get prefix |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | 1 | public function getPrefix() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Set module |
||
| 249 | * |
||
| 250 | * @param string $module |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | 1 | public function setModule($module) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Get module |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | 13 | public function getModule() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Set controller |
||
| 270 | * |
||
| 271 | * @param string $controller |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | 1 | public function setController($controller) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Get controller |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | 13 | public function getController() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Process request |
||
| 291 | * |
||
| 292 | * Example of request url |
||
| 293 | * - http://domain.com/pages/grid/ |
||
| 294 | * - http://domain.com/pages/grid/page/2/ |
||
| 295 | * - http://domain.com/pages/grid/page/2/order-alias/desc/ |
||
| 296 | * - http://domain.com/pages/grid/page/2/order-created/desc/order-alias/asc/ |
||
| 297 | * |
||
| 298 | * with prefix for support more than one grid on page |
||
| 299 | * - http://domain.com/users/grid/users-page/2/users-order-created/desc/ |
||
| 300 | * - http://domain.com/users/grid/users-page/2/users-filter-status/active/ |
||
| 301 | * |
||
| 302 | * hash support |
||
| 303 | * - http://domain.com/pages/grid/#/page/2/order-created/desc/order-alias/asc/ |
||
| 304 | * |
||
| 305 | * @return Grid |
||
| 306 | */ |
||
| 307 | 34 | public function processRequest() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Process source |
||
| 358 | * |
||
| 359 | * @return self |
||
| 360 | * @throws GridException |
||
| 361 | */ |
||
| 362 | 15 | public function processSource() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Get data |
||
| 379 | * |
||
| 380 | * @return Data |
||
| 381 | */ |
||
| 382 | 15 | public function getData() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Get settings |
||
| 392 | * |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | 15 | public function getSettings() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Setup params |
||
| 408 | * |
||
| 409 | * @param $params |
||
| 410 | * @return void |
||
| 411 | */ |
||
| 412 | public function setParams($params) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Return params prepared for url builder |
||
| 419 | * |
||
| 420 | * @param array $rewrite |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | 13 | public function getParams(array $rewrite = []) |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Get Url |
||
| 470 | * |
||
| 471 | * @param array $params |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | 13 | public function getUrl($params) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Add column name for allow order |
||
| 489 | * |
||
| 490 | * @param string $column |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | 34 | public function addAllowOrder($column) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Set allow orders |
||
| 500 | * |
||
| 501 | * @param string[] $orders |
||
| 502 | * @return void |
||
| 503 | */ |
||
| 504 | 34 | public function setAllowOrders(array $orders = []) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Get allow orders |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | 8 | public function getAllowOrders() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Check order column |
||
| 524 | * |
||
| 525 | * @param string $column |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | 8 | protected function checkOrderColumn($column) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Check order name |
||
| 535 | * |
||
| 536 | * @param string $order |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | 6 | protected function checkOrderName($order) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Add order rule |
||
| 546 | * |
||
| 547 | * @param string $column |
||
| 548 | * @param string $order |
||
| 549 | * @return void |
||
| 550 | * @throws GridException |
||
| 551 | */ |
||
| 552 | 8 | public function addOrder($column, $order = Grid::ORDER_ASC) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Add order rules |
||
| 567 | * |
||
| 568 | * @param array $orders |
||
| 569 | * @return void |
||
| 570 | */ |
||
| 571 | 1 | public function addOrders(array $orders) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Set order |
||
| 580 | * |
||
| 581 | * @param string $column |
||
| 582 | * @param string $order ASC or DESC |
||
| 583 | * @return void |
||
| 584 | */ |
||
| 585 | 6 | public function setOrder($column, $order = Grid::ORDER_ASC) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Set orders |
||
| 593 | * |
||
| 594 | * @param array $orders |
||
| 595 | * @return void |
||
| 596 | */ |
||
| 597 | 1 | public function setOrders(array $orders) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Get orders |
||
| 605 | * |
||
| 606 | * @return array |
||
| 607 | */ |
||
| 608 | 25 | public function getOrders() |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Add column name to allow filter it |
||
| 626 | * |
||
| 627 | * @param string $column |
||
| 628 | * @return void |
||
| 629 | */ |
||
| 630 | 34 | public function addAllowFilter($column) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Set allowed filters |
||
| 637 | * |
||
| 638 | * @param string[] $filters |
||
| 639 | * @return void |
||
| 640 | */ |
||
| 641 | 34 | public function setAllowFilters(array $filters = []) |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Get allow filters |
||
| 651 | * |
||
| 652 | * @return array |
||
| 653 | */ |
||
| 654 | 12 | public function getAllowFilters() |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Check filter column |
||
| 661 | * |
||
| 662 | * @param string $column |
||
| 663 | * @return bool |
||
| 664 | */ |
||
| 665 | 12 | protected function checkFilterColumn($column) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Check filter |
||
| 678 | * |
||
| 679 | * @param string $filter |
||
| 680 | * @return bool |
||
| 681 | */ |
||
| 682 | 12 | protected function checkFilterName($filter) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Add filter |
||
| 689 | * |
||
| 690 | * @param string $column name |
||
| 691 | * @param string $filter |
||
| 692 | * @param string $value |
||
| 693 | * @return void |
||
| 694 | * @throws GridException |
||
| 695 | */ |
||
| 696 | 10 | public function addFilter($column, $filter, $value) |
|
| 709 | |||
| 710 | |||
| 711 | /** |
||
| 712 | * Get filter |
||
| 713 | * |
||
| 714 | * @param string $column |
||
| 715 | * @param string $filter |
||
| 716 | * @return mixed |
||
| 717 | */ |
||
| 718 | public function getFilter($column, $filter = null) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Get filters |
||
| 729 | * |
||
| 730 | * @return array |
||
| 731 | */ |
||
| 732 | 23 | public function getFilters() |
|
| 736 | |||
| 737 | /** |
||
| 738 | * Add alias for column name |
||
| 739 | * |
||
| 740 | * @param string $column |
||
| 741 | * @param string $alias |
||
| 742 | * @return void |
||
| 743 | */ |
||
| 744 | 32 | public function addAlias($column, $alias) |
|
| 748 | |||
| 749 | /** |
||
| 750 | * Get column name by alias |
||
| 751 | * |
||
| 752 | * @param string $alias |
||
| 753 | * @return string |
||
| 754 | */ |
||
| 755 | protected function reverseAlias($alias) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Get alias by column name |
||
| 762 | * |
||
| 763 | * @param string $column |
||
| 764 | * @return string |
||
| 765 | */ |
||
| 766 | 34 | protected function applyAlias($column) |
|
| 770 | |||
| 771 | /** |
||
| 772 | * Set page |
||
| 773 | * |
||
| 774 | * @param integer $page |
||
| 775 | * @return void |
||
| 776 | * @throws GridException |
||
| 777 | */ |
||
| 778 | 34 | public function setPage($page = 1) |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Get page |
||
| 788 | * |
||
| 789 | * @return integer |
||
| 790 | */ |
||
| 791 | 17 | public function getPage() |
|
| 795 | |||
| 796 | /** |
||
| 797 | * Set limit per page |
||
| 798 | * |
||
| 799 | * @param integer $limit |
||
| 800 | * @return void |
||
| 801 | * @throws GridException |
||
| 802 | */ |
||
| 803 | 34 | public function setLimit($limit) |
|
| 810 | |||
| 811 | /** |
||
| 812 | * Get limit per page |
||
| 813 | * |
||
| 814 | * @return integer |
||
| 815 | */ |
||
| 816 | 24 | public function getLimit() |
|
| 820 | |||
| 821 | /** |
||
| 822 | * Set default limit |
||
| 823 | * |
||
| 824 | * @param integer $limit |
||
| 825 | * @return void |
||
| 826 | * @throws GridException |
||
| 827 | */ |
||
| 828 | 34 | public function setDefaultLimit($limit) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Get default limit |
||
| 840 | * |
||
| 841 | * @return integer |
||
| 842 | */ |
||
| 843 | 1 | public function getDefaultLimit() |
|
| 847 | |||
| 848 | /** |
||
| 849 | * Set default order |
||
| 850 | * |
||
| 851 | * @param string $column |
||
| 852 | * @param string $order ASC or DESC |
||
| 853 | * @return void |
||
| 854 | * @throws GridException |
||
| 855 | */ |
||
| 856 | 5 | public function setDefaultOrder($column, $order = Grid::ORDER_ASC) |
|
| 862 | |||
| 863 | /** |
||
| 864 | * Get default order |
||
| 865 | * |
||
| 866 | * @return array |
||
| 867 | */ |
||
| 868 | 24 | public function getDefaultOrder() |
|
| 872 | } |
||
| 873 |