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 | 33 | public function __construct($options = null) |
|
| 160 | { |
||
| 161 | 33 | if ($options) { |
|
| 162 | $this->setOptions($options); |
||
| 163 | } |
||
| 164 | |||
| 165 | 33 | if ($this->uid) { |
|
| 166 | 33 | $this->prefix = $this->getUid() . '-'; |
|
| 167 | } else { |
||
| 168 | $this->prefix = ''; |
||
| 169 | } |
||
| 170 | |||
| 171 | 33 | $this->init(); |
|
| 172 | |||
| 173 | 33 | $this->processRequest(); |
|
| 174 | |||
| 175 | // initial default helper path |
||
| 176 | 33 | $this->addHelperPath(dirname(__FILE__) . '/Helper/'); |
|
| 177 | 33 | } |
|
| 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() |
|
| 292 | { |
||
| 293 | 33 | $this->module = Request::getModule(); |
|
| 294 | 33 | $this->controller = Request::getController(); |
|
| 295 | |||
| 296 | 33 | $page = Request::getParam($this->prefix . 'page', 1); |
|
| 297 | 33 | $this->setPage($page); |
|
| 298 | |||
| 299 | 33 | $limit = Request::getParam($this->prefix . 'limit', $this->limit); |
|
| 300 | 33 | $this->setLimit($limit); |
|
| 301 | |||
| 302 | 33 | foreach ($this->allowOrders as $column) { |
|
| 303 | 33 | $order = Request::getParam($this->prefix . 'order-' . $column); |
|
| 304 | 33 | if ($order) { |
|
| 305 | 33 | $this->addOrder($column, $order); |
|
| 306 | } |
||
| 307 | } |
||
| 308 | 33 | foreach ($this->allowFilters as $column) { |
|
| 309 | 33 | $filter = Request::getParam($this->prefix . 'filter-' . $column); |
|
| 310 | |||
| 311 | 33 | if ($filter) { |
|
| 312 | 1 | if (strpos($filter, '-')) { |
|
| 313 | 1 | $filter = trim($filter, ' -'); |
|
| 314 | |||
| 315 | 1 | while ($pos = strpos($filter, '-')) { |
|
| 316 | 1 | $filterType = substr($filter, 0, $pos); |
|
| 317 | 1 | $filter = substr($filter, $pos + 1); |
|
| 318 | |||
| 319 | 1 | if (strpos($filter, '-')) { |
|
| 320 | $filterValue = substr($filter, 0, strpos($filter, '-')); |
||
| 321 | $filter = substr($filter, strpos($filter, '-') + 1); |
||
| 322 | } else { |
||
| 323 | 1 | $filterValue = $filter; |
|
| 324 | } |
||
| 325 | |||
| 326 | 1 | $this->addFilter($column, $filterType, $filterValue); |
|
| 327 | } |
||
| 328 | } else { |
||
| 329 | 33 | $this->addFilter($column, self::FILTER_EQ, $filter); |
|
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | 33 | return $this; |
|
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Process source |
||
| 338 | * |
||
| 339 | * @return self |
||
| 340 | * @throws GridException |
||
| 341 | */ |
||
| 342 | 15 | public function processSource() |
|
| 343 | { |
||
| 344 | 15 | if (null === $this->adapter) { |
|
| 345 | throw new GridException("Grid Adapter is not initiated, please update method init() and try again"); |
||
| 346 | } |
||
| 347 | |||
| 348 | try { |
||
| 349 | 15 | $this->data = $this->getAdapter()->process($this->getSettings()); |
|
| 350 | } catch (\Exception $e) { |
||
| 351 | throw new GridException("Grid Adapter can't process request: ". $e->getMessage()); |
||
| 352 | } |
||
| 353 | |||
| 354 | 15 | return $this; |
|
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get data |
||
| 359 | * |
||
| 360 | * @return Data |
||
| 361 | */ |
||
| 362 | 15 | public function getData() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Get settings |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | 15 | public function getSettings() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Setup params |
||
| 387 | * |
||
| 388 | * @param $params |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | public function setParams($params) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Return params prepared for url builder |
||
| 398 | * |
||
| 399 | * @param array $rewrite |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | 11 | public function getParams(array $rewrite = []) |
|
| 447 | |||
| 448 | /** |
||
| 449 | 1 | * Get Url |
|
| 450 | * |
||
| 451 | * @param array $params |
||
| 452 | 11 | * @return string |
|
| 453 | */ |
||
| 454 | public function getUrl($params) |
||
| 466 | |||
| 467 | 11 | /** |
|
| 468 | 11 | * Set allow orders |
|
| 469 | 11 | * |
|
| 470 | * @param string[] $orders |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | public function setAllowOrders(array $orders = []) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get allow orders |
||
| 480 | 33 | * |
|
| 481 | * @return array |
||
| 482 | 33 | */ |
|
| 483 | 33 | public function getAllowOrders() |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Add order rule |
||
| 490 | 2 | * |
|
| 491 | * @param string $column |
||
| 492 | 2 | * @param string $order |
|
| 493 | * @return void |
||
| 494 | * @throws GridException |
||
| 495 | */ |
||
| 496 | public function addOrder($column, $order = Grid::ORDER_ASC) |
||
| 511 | |||
| 512 | 1 | /** |
|
| 513 | * Add order rules |
||
| 514 | 6 | * |
|
| 515 | * @param array $orders |
||
| 516 | 6 | * @return void |
|
| 517 | 6 | */ |
|
| 518 | public function addOrders(array $orders) |
||
| 524 | |||
| 525 | 1 | /** |
|
| 526 | * Set order |
||
| 527 | 1 | * |
|
| 528 | 1 | * @param string $column |
|
| 529 | * @param string $order ASC or DESC |
||
| 530 | 1 | * @return void |
|
| 531 | */ |
||
| 532 | public function setOrder($column, $order = Grid::ORDER_ASC) |
||
| 537 | |||
| 538 | /** |
||
| 539 | 6 | * Set orders |
|
| 540 | * |
||
| 541 | 6 | * @param array $orders |
|
| 542 | 6 | * @return void |
|
| 543 | 4 | */ |
|
| 544 | public function setOrders(array $orders) |
||
| 549 | |||
| 550 | /** |
||
| 551 | 1 | * Get orders |
|
| 552 | * |
||
| 553 | 1 | * @return array |
|
| 554 | 1 | */ |
|
| 555 | 1 | public function getOrders() |
|
| 570 | 24 | ||
| 571 | /** |
||
| 572 | * Set allowed filters |
||
| 573 | * |
||
| 574 | * @param string[] $filters |
||
| 575 | 24 | * @return void |
|
| 576 | */ |
||
| 577 | public function setAllowFilters(array $filters = []) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Get allow filters |
||
| 584 | 33 | * |
|
| 585 | * @return array |
||
| 586 | 33 | */ |
|
| 587 | 33 | public function getAllowFilters() |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Check filter |
||
| 594 | 3 | * |
|
| 595 | * @param string $filter |
||
| 596 | 3 | * @return bool |
|
| 597 | */ |
||
| 598 | public function checkFilter($filter) |
||
| 614 | 5 | ||
| 615 | 10 | /** |
|
| 616 | * Add filter |
||
| 617 | 8 | * |
|
| 618 | * @param string $column |
||
| 619 | 2 | * @param string $filter |
|
| 620 | * @param string $value |
||
| 621 | * @return void |
||
| 622 | * @throws GridException |
||
| 623 | */ |
||
| 624 | public function addFilter($column, $filter, $value) |
||
| 645 | 8 | ||
| 646 | |||
| 647 | 8 | /** |
|
| 648 | 8 | * Get filter |
|
| 649 | * |
||
| 650 | 8 | * @param string $column |
|
| 651 | 8 | * @param string $filter |
|
| 652 | * @return mixed |
||
| 653 | */ |
||
| 654 | public function getFilter($column, $filter = null) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Get filters |
||
| 673 | * |
||
| 674 | * @return array |
||
| 675 | */ |
||
| 676 | public function getFilters() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Add alias |
||
| 683 | 23 | * |
|
| 684 | * @param string $key |
||
| 685 | 23 | * @param string $value |
|
| 686 | * @return void |
||
| 687 | */ |
||
| 688 | public function addAlias($key, $value) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Set aliases |
||
| 695 | * |
||
| 696 | * @param array $aliases |
||
| 697 | * @return void |
||
| 698 | */ |
||
| 699 | public function setAliases($aliases) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Apply Alias |
||
| 706 | * |
||
| 707 | * @param string $key |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | protected function applyAlias($key) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Set page |
||
| 717 | 11 | * |
|
| 718 | * @param integer $page |
||
| 719 | 11 | * @return void |
|
| 720 | * @throws GridException |
||
| 721 | */ |
||
| 722 | public function setPage($page = 1) |
||
| 729 | 33 | ||
| 730 | /** |
||
| 731 | 33 | * Get page |
|
| 732 | 1 | * |
|
| 733 | * @return integer |
||
| 734 | 33 | */ |
|
| 735 | 33 | public function getPage() |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Set limit per page |
||
| 742 | 17 | * |
|
| 743 | * @param integer $limit |
||
| 744 | 17 | * @return void |
|
| 745 | * @throws GridException |
||
| 746 | */ |
||
| 747 | public function setLimit($limit) |
||
| 754 | 33 | ||
| 755 | /** |
||
| 756 | 33 | * Get limit per page |
|
| 757 | 1 | * |
|
| 758 | * @return integer |
||
| 759 | 33 | */ |
|
| 760 | 33 | public function getLimit() |
|
| 764 | |||
| 765 | /** |
||
| 766 | * Set default limit |
||
| 767 | 16 | * |
|
| 768 | * @param integer $limit |
||
| 769 | 16 | * @return void |
|
| 770 | * @throws GridException |
||
| 771 | */ |
||
| 772 | public function setDefaultLimit($limit) |
||
| 781 | 33 | ||
| 782 | 1 | /** |
|
| 783 | * Get default limit |
||
| 784 | 33 | * |
|
| 785 | * @return integer |
||
| 786 | 33 | */ |
|
| 787 | 33 | public function getDefaultLimit() |
|
| 791 | |||
| 792 | /** |
||
| 793 | * Set default order |
||
| 794 | 1 | * |
|
| 795 | * @param string $column |
||
| 796 | 1 | * @param string $order ASC or DESC |
|
| 797 | * @return void |
||
| 798 | * @throws GridException |
||
| 799 | */ |
||
| 800 | public function setDefaultOrder($column, $order = Grid::ORDER_ASC) |
||
| 806 | |||
| 807 | 5 | /** |
|
| 808 | * Get default order |
||
| 809 | 5 | * |
|
| 810 | * @return array |
||
| 811 | 3 | */ |
|
| 812 | 3 | public function getDefaultOrder() |
|
| 816 | } |
||
| 817 |
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: