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 = array(); |
||
| 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 = array(); |
||
| 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 = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var array list of filters |
||
| 133 | */ |
||
| 134 | protected $filters = array(); |
||
| 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 = array(); |
||
| 146 | |||
| 147 | /** |
||
| 148 | * List of aliases for columns in DB |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $aliases = array(); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Grid constructor |
||
| 156 | * |
||
| 157 | * @param array $options |
||
| 158 | */ |
||
| 159 | 33 | public function __construct($options = null) |
|
| 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 | 33 | 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 | 33 | 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 | 11 | 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 | 11 | 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 | 33 | public function processRequest() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Process source |
||
| 339 | * |
||
| 340 | * @return self |
||
| 341 | * @throws GridException |
||
| 342 | */ |
||
| 343 | 15 | public function processSource() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Get data |
||
| 360 | * |
||
| 361 | * @return Data |
||
| 362 | */ |
||
| 363 | 15 | public function getData() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Get settings |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | 15 | public function getSettings() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Setup params |
||
| 388 | * |
||
| 389 | * @param $params |
||
| 390 | * @return void |
||
| 391 | */ |
||
| 392 | public function setParams($params) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Return params prepared for url builder |
||
| 399 | * |
||
| 400 | * @param array $rewrite |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | 11 | public function getParams(array $rewrite = []) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Get Url |
||
| 458 | * |
||
| 459 | * @param array $params |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | 11 | public function getUrl($params) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Set allow orders |
||
| 477 | * |
||
| 478 | * @param string[] $orders |
||
| 479 | * @return void |
||
| 480 | */ |
||
| 481 | 33 | public function setAllowOrders(array $orders = []) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Get allow orders |
||
| 488 | * |
||
| 489 | * @return array |
||
| 490 | */ |
||
| 491 | 2 | public function getAllowOrders() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Add order rule |
||
| 498 | * |
||
| 499 | * @param string $column |
||
| 500 | * @param string $order |
||
| 501 | * @return void |
||
| 502 | * @throws GridException |
||
| 503 | */ |
||
| 504 | 8 | public function addOrder($column, $order = Grid::ORDER_ASC) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Add order rules |
||
| 522 | * |
||
| 523 | * @param array $orders |
||
| 524 | * @return void |
||
| 525 | */ |
||
| 526 | 1 | public function addOrders(array $orders) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Set order |
||
| 535 | * |
||
| 536 | * @param string $column |
||
| 537 | * @param string $order ASC or DESC |
||
| 538 | * @return void |
||
| 539 | */ |
||
| 540 | 6 | public function setOrder($column, $order = Grid::ORDER_ASC) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Set orders |
||
| 548 | * |
||
| 549 | * @param array $orders |
||
| 550 | * @return void |
||
| 551 | */ |
||
| 552 | 1 | public function setOrders(array $orders) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Get orders |
||
| 560 | * |
||
| 561 | * @return array |
||
| 562 | */ |
||
| 563 | 24 | public function getOrders() |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Set allowed filters |
||
| 581 | * |
||
| 582 | * @param string[] $filters |
||
| 583 | * @return void |
||
| 584 | */ |
||
| 585 | 33 | public function setAllowFilters(array $filters = array()) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Get allow filters |
||
| 592 | * |
||
| 593 | * @return array |
||
| 594 | */ |
||
| 595 | 3 | public function getAllowFilters() |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Check filter |
||
| 602 | * |
||
| 603 | * @param string $filter |
||
| 604 | * @return bool |
||
| 605 | */ |
||
| 606 | 10 | public function checkFilter($filter) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Add filter |
||
| 625 | * |
||
| 626 | * @param string $column |
||
| 627 | * @param string $filter |
||
| 628 | * @param string $value |
||
| 629 | * @return void |
||
| 630 | * @throws GridException |
||
| 631 | */ |
||
| 632 | 10 | public function addFilter($column, $filter, $value) |
|
| 653 | |||
| 654 | |||
| 655 | /** |
||
| 656 | * Get filter |
||
| 657 | * |
||
| 658 | * @param string $column |
||
| 659 | * @param string $filter |
||
| 660 | * @return mixed |
||
| 661 | */ |
||
| 662 | public function getFilter($column, $filter = null) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Get filters |
||
| 681 | * |
||
| 682 | * @return array |
||
| 683 | */ |
||
| 684 | 23 | public function getFilters() |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Add alias |
||
| 691 | * |
||
| 692 | * @param string $key |
||
| 693 | * @param string $value |
||
| 694 | * @return void |
||
| 695 | */ |
||
| 696 | public function addAlias($key, $value) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Set aliases |
||
| 703 | * |
||
| 704 | * @param array $aliases |
||
| 705 | * @return void |
||
| 706 | */ |
||
| 707 | public function setAliases($aliases) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Apply Alias |
||
| 714 | * |
||
| 715 | * @param string $key |
||
| 716 | * @return string |
||
| 717 | */ |
||
| 718 | 11 | protected function applyAlias($key) |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Set page |
||
| 725 | * |
||
| 726 | * @param integer $page |
||
| 727 | * @return void |
||
| 728 | * @throws GridException |
||
| 729 | */ |
||
| 730 | 33 | public function setPage($page = 1) |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Get page |
||
| 740 | * |
||
| 741 | * @return integer |
||
| 742 | */ |
||
| 743 | 17 | public function getPage() |
|
| 747 | |||
| 748 | /** |
||
| 749 | * Set limit per page |
||
| 750 | * |
||
| 751 | * @param integer $limit |
||
| 752 | * @return void |
||
| 753 | * @throws GridException |
||
| 754 | */ |
||
| 755 | 33 | public function setLimit($limit) |
|
| 762 | |||
| 763 | /** |
||
| 764 | * Get limit per page |
||
| 765 | * |
||
| 766 | * @return integer |
||
| 767 | */ |
||
| 768 | 16 | public function getLimit() |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Set default limit |
||
| 775 | * |
||
| 776 | * @param integer $limit |
||
| 777 | * @return void |
||
| 778 | * @throws GridException |
||
| 779 | */ |
||
| 780 | 33 | public function setDefaultLimit($limit) |
|
| 789 | |||
| 790 | /** |
||
| 791 | * Get default limit |
||
| 792 | * |
||
| 793 | * @return integer |
||
| 794 | */ |
||
| 795 | 1 | public function getDefaultLimit() |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Set default order |
||
| 802 | * |
||
| 803 | * @param string $column |
||
| 804 | * @param string $order ASC or DESC |
||
| 805 | * @return void |
||
| 806 | * @throws GridException |
||
| 807 | */ |
||
| 808 | 5 | public function setDefaultOrder($column, $order = Grid::ORDER_ASC) |
|
| 814 | |||
| 815 | /** |
||
| 816 | * Get default order |
||
| 817 | * |
||
| 818 | * @return array |
||
| 819 | */ |
||
| 820 | 23 | public function getDefaultOrder() |
|
| 824 | } |
||
| 825 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: