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 |
||
| 37 | abstract class Grid |
||
| 38 | { |
||
| 39 | use Options; |
||
| 40 | use Helper; |
||
| 41 | |||
| 42 | const ORDER_ASC = 'asc'; |
||
| 43 | const ORDER_DESC = 'desc'; |
||
| 44 | |||
| 45 | const FILTER_LIKE = 'like'; // like |
||
| 46 | const FILTER_ENUM = 'enum'; // one from .., .., .. |
||
| 47 | |||
| 48 | const FILTER_EQ = 'eq'; // equal to .. |
||
| 49 | const FILTER_NE = 'ne'; // not equal to .. |
||
| 50 | const FILTER_GT = 'gt'; // greater than .. |
||
| 51 | const FILTER_GE = 'ge'; // greater than .. or equal |
||
| 52 | const FILTER_LT = 'lt'; // less than .. |
||
| 53 | const FILTER_LE = 'le'; // less than .. or equal |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var Source\AbstractSource instance of Source |
||
| 57 | */ |
||
| 58 | protected $adapter; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var Data instance of Data |
||
| 62 | */ |
||
| 63 | protected $data; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string unique identification of grid |
||
| 67 | */ |
||
| 68 | protected $uid; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string unique prefix of grid |
||
| 72 | */ |
||
| 73 | protected $prefix = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string location of Grid |
||
| 77 | */ |
||
| 78 | protected $module; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string location of Grid |
||
| 82 | */ |
||
| 83 | protected $controller; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array custom array params |
||
| 87 | */ |
||
| 88 | protected $params = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var integer start from first page |
||
| 92 | */ |
||
| 93 | protected $page = 1; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var integer limit per page |
||
| 97 | */ |
||
| 98 | protected $limit = 25; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var integer default value of page limit |
||
| 102 | * @see Grid::$limit |
||
| 103 | */ |
||
| 104 | protected $defaultLimit = 25; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * List of orders |
||
| 108 | * |
||
| 109 | * Example |
||
| 110 | * 'first' => 'ASC', |
||
| 111 | * 'last' => 'ASC' |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | protected $orders = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array default order |
||
| 119 | * @see Grid::$orders |
||
| 120 | */ |
||
| 121 | protected $defaultOrder; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var array list of allow orders |
||
| 125 | * @see Grid::$orders |
||
| 126 | */ |
||
| 127 | protected $allowOrders = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array list of filters |
||
| 131 | */ |
||
| 132 | protected $filters = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * List of allow filters |
||
| 136 | * |
||
| 137 | * Example |
||
| 138 | * ['id', 'status' => ['active', 'disable']] |
||
| 139 | * |
||
| 140 | * @var array |
||
| 141 | * @see Grid::$filters |
||
| 142 | */ |
||
| 143 | protected $allowFilters = []; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * List of allow filter names |
||
| 147 | * |
||
| 148 | * @var array |
||
| 149 | * @see Grid::$filters |
||
| 150 | */ |
||
| 151 | protected $allowFilterNames = [ |
||
| 152 | self::FILTER_LIKE, |
||
| 153 | self::FILTER_ENUM, |
||
| 154 | self::FILTER_EQ, |
||
| 155 | self::FILTER_NE, |
||
| 156 | self::FILTER_GT, |
||
| 157 | self::FILTER_GE, |
||
| 158 | self::FILTER_LT, |
||
| 159 | self::FILTER_LE |
||
| 160 | ]; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * List of aliases for columns in DB |
||
| 164 | * |
||
| 165 | * @var array |
||
| 166 | */ |
||
| 167 | protected $aliases = []; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Grid constructor |
||
| 171 | * |
||
| 172 | * @param array $options |
||
| 173 | */ |
||
| 174 | 34 | public function __construct($options = null) |
|
| 175 | { |
||
| 176 | 34 | if ($options) { |
|
| 177 | $this->setOptions($options); |
||
| 178 | } |
||
| 179 | |||
| 180 | 34 | if ($this->getUid()) { |
|
| 181 | 34 | $this->prefix = $this->getUid() . '-'; |
|
| 182 | } |
||
| 183 | |||
| 184 | 34 | $this->init(); |
|
| 185 | |||
| 186 | 34 | $this->processRequest(); |
|
| 187 | |||
| 188 | // initial default helper path |
||
| 189 | 34 | $this->addHelperPath(__DIR__ . '/Helper/'); |
|
| 190 | 34 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Initialize Grid |
||
| 194 | * |
||
| 195 | * @return Grid |
||
| 196 | */ |
||
| 197 | abstract public function init(); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set source adapter |
||
| 201 | * |
||
| 202 | * @param Source\AbstractSource $adapter |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | 34 | public function setAdapter(Source\AbstractSource $adapter) |
|
| 207 | { |
||
| 208 | 34 | $this->adapter = $adapter; |
|
| 209 | 34 | } |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get source adapter |
||
| 213 | * |
||
| 214 | * @return Source\AbstractSource |
||
| 215 | * @throws GridException |
||
| 216 | */ |
||
| 217 | 15 | public function getAdapter() |
|
| 218 | { |
||
| 219 | 15 | if (null === $this->adapter) { |
|
| 220 | throw new GridException('Grid adapter is not initialized'); |
||
| 221 | } |
||
| 222 | 15 | return $this->adapter; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get unique Grid Id |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | 34 | public function getUid() |
|
| 231 | { |
||
| 232 | 34 | return $this->uid; |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get prefix |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 1 | public function getPrefix() |
|
| 241 | { |
||
| 242 | 1 | return $this->prefix; |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Set module |
||
| 247 | * |
||
| 248 | * @param string $module |
||
| 249 | * |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | 1 | public function setModule($module) |
|
| 253 | { |
||
| 254 | 1 | $this->module = $module; |
|
| 255 | 1 | } |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get module |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 13 | public function getModule() |
|
| 263 | { |
||
| 264 | 13 | return $this->module; |
|
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Set controller |
||
| 269 | * |
||
| 270 | * @param string $controller |
||
| 271 | * |
||
| 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 | * |
||
| 411 | * @return void |
||
| 412 | */ |
||
| 413 | public function setParams($params) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Return params prepared for url builder |
||
| 420 | * |
||
| 421 | * @param array $rewrite |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | 13 | public function getParams(array $rewrite = []) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Get Url |
||
| 472 | * |
||
| 473 | * @param array $params |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | 13 | public function getUrl($params) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Add column name for allow order |
||
| 492 | * |
||
| 493 | * @param string $column |
||
| 494 | * |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | 34 | public function addAllowOrder($column) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Set allow orders |
||
| 504 | * |
||
| 505 | * @param string[] $orders |
||
| 506 | * |
||
| 507 | * @return void |
||
| 508 | */ |
||
| 509 | 34 | public function setAllowOrders(array $orders = []) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Get allow orders |
||
| 519 | * |
||
| 520 | * @return array |
||
| 521 | */ |
||
| 522 | 8 | public function getAllowOrders() |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Check order column |
||
| 529 | * |
||
| 530 | * @param string $column |
||
| 531 | * |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | 8 | protected function checkOrderColumn($column) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Check order name |
||
| 541 | * |
||
| 542 | * @param string $order |
||
| 543 | * |
||
| 544 | * @return bool |
||
| 545 | */ |
||
| 546 | 6 | protected function checkOrderName($order) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Add order rule |
||
| 553 | * |
||
| 554 | * @param string $column |
||
| 555 | * @param string $order |
||
| 556 | * |
||
| 557 | * @return void |
||
| 558 | * @throws GridException |
||
| 559 | */ |
||
| 560 | 8 | public function addOrder($column, $order = Grid::ORDER_ASC) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Add order rules |
||
| 575 | * |
||
| 576 | * @param array $orders |
||
| 577 | * |
||
| 578 | * @return void |
||
| 579 | */ |
||
| 580 | 1 | public function addOrders(array $orders) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Set order |
||
| 589 | * |
||
| 590 | * @param string $column |
||
| 591 | * @param string $order ASC or DESC |
||
| 592 | * |
||
| 593 | * @return void |
||
| 594 | */ |
||
| 595 | 6 | public function setOrder($column, $order = Grid::ORDER_ASC) |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Set orders |
||
| 603 | * |
||
| 604 | * @param array $orders |
||
| 605 | * |
||
| 606 | * @return void |
||
| 607 | */ |
||
| 608 | 1 | public function setOrders(array $orders) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Get orders |
||
| 616 | * |
||
| 617 | * @return array |
||
| 618 | */ |
||
| 619 | 25 | public function getOrders() |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Add column name to allow filter it |
||
| 637 | * |
||
| 638 | * @param string $column |
||
| 639 | * |
||
| 640 | * @return void |
||
| 641 | */ |
||
| 642 | 34 | public function addAllowFilter($column) |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Set allowed filters |
||
| 649 | * |
||
| 650 | * @param string[] $filters |
||
| 651 | * |
||
| 652 | * @return void |
||
| 653 | */ |
||
| 654 | 34 | public function setAllowFilters(array $filters = []) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Get allow filters |
||
| 664 | * |
||
| 665 | * @return array |
||
| 666 | */ |
||
| 667 | 12 | public function getAllowFilters() |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Check filter column |
||
| 674 | * |
||
| 675 | * @param string $column |
||
| 676 | * |
||
| 677 | * @return bool |
||
| 678 | */ |
||
| 679 | 12 | protected function checkFilterColumn($column) |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Check filter |
||
| 691 | * |
||
| 692 | * @param string $filter |
||
| 693 | * |
||
| 694 | * @return bool |
||
| 695 | */ |
||
| 696 | 12 | protected function checkFilterName($filter) |
|
| 700 | |||
| 701 | /** |
||
| 702 | * Add filter |
||
| 703 | * |
||
| 704 | * @param string $column name |
||
| 705 | * @param string $filter |
||
| 706 | * @param string $value |
||
| 707 | * |
||
| 708 | * @return void |
||
| 709 | * @throws GridException |
||
| 710 | */ |
||
| 711 | 10 | public function addFilter($column, $filter, $value) |
|
| 724 | |||
| 725 | |||
| 726 | /** |
||
| 727 | * Get filter |
||
| 728 | * |
||
| 729 | * @param string $column |
||
| 730 | * @param string $filter |
||
| 731 | * |
||
| 732 | * @return mixed |
||
| 733 | */ |
||
| 734 | public function getFilter($column, $filter = null) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Get filters |
||
| 744 | * |
||
| 745 | * @return array |
||
| 746 | */ |
||
| 747 | 23 | public function getFilters() |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Add alias for column name |
||
| 754 | * |
||
| 755 | * @param string $column |
||
| 756 | * @param string $alias |
||
| 757 | * |
||
| 758 | * @return void |
||
| 759 | */ |
||
| 760 | 32 | public function addAlias($column, $alias) |
|
| 764 | |||
| 765 | /** |
||
| 766 | * Get column name by alias |
||
| 767 | * |
||
| 768 | * @param string $alias |
||
| 769 | * |
||
| 770 | * @return string |
||
| 771 | */ |
||
| 772 | protected function reverseAlias($alias) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Get alias by column name |
||
| 779 | * |
||
| 780 | * @param string $column |
||
| 781 | * |
||
| 782 | * @return string |
||
| 783 | */ |
||
| 784 | 34 | protected function applyAlias($column) |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Set page |
||
| 791 | * |
||
| 792 | * @param integer $page |
||
| 793 | * |
||
| 794 | * @return void |
||
| 795 | * @throws GridException |
||
| 796 | */ |
||
| 797 | 34 | public function setPage(int $page = 1) |
|
| 804 | |||
| 805 | /** |
||
| 806 | * Get page |
||
| 807 | * |
||
| 808 | * @return integer |
||
| 809 | */ |
||
| 810 | 17 | public function getPage(): int |
|
| 814 | |||
| 815 | /** |
||
| 816 | * Set limit per page |
||
| 817 | * |
||
| 818 | * @param integer $limit |
||
| 819 | * |
||
| 820 | * @return void |
||
| 821 | * @throws GridException |
||
| 822 | */ |
||
| 823 | 34 | public function setLimit(int $limit) |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Get limit per page |
||
| 833 | * |
||
| 834 | * @return integer |
||
| 835 | */ |
||
| 836 | 24 | public function getLimit(): int |
|
| 840 | |||
| 841 | /** |
||
| 842 | * Set default limit |
||
| 843 | * |
||
| 844 | * @param integer $limit |
||
| 845 | * |
||
| 846 | * @return void |
||
| 847 | * @throws GridException |
||
| 848 | */ |
||
| 849 | 34 | public function setDefaultLimit(int $limit) |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Get default limit |
||
| 861 | * |
||
| 862 | * @return integer |
||
| 863 | */ |
||
| 864 | 1 | public function getDefaultLimit(): int |
|
| 868 | |||
| 869 | /** |
||
| 870 | * Set default order |
||
| 871 | * |
||
| 872 | * @param string $column |
||
| 873 | * @param string $order ASC or DESC |
||
| 874 | * |
||
| 875 | * @return void |
||
| 876 | * @throws GridException |
||
| 877 | */ |
||
| 878 | 5 | public function setDefaultOrder($column, $order = Grid::ORDER_ASC) |
|
| 884 | |||
| 885 | /** |
||
| 886 | * Get default order |
||
| 887 | * |
||
| 888 | * @return array |
||
| 889 | */ |
||
| 890 | 24 | public function getDefaultOrder() |
|
| 894 | } |
||
| 895 |