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 | public const ORDER_ASC = 'asc'; |
||
| 43 | public const ORDER_DESC = 'desc'; |
||
| 44 | |||
| 45 | public const FILTER_LIKE = 'like'; // like |
||
| 46 | public const FILTER_ENUM = 'enum'; // one from .., .., .. |
||
| 47 | |||
| 48 | public const FILTER_EQ = 'eq'; // equal to .. |
||
| 49 | public const FILTER_NE = 'ne'; // not equal to .. |
||
| 50 | public const FILTER_GT = 'gt'; // greater than .. |
||
| 51 | public const FILTER_GE = 'ge'; // greater than .. or equal |
||
| 52 | public const FILTER_LT = 'lt'; // less than .. |
||
| 53 | public 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 | * @throws \Bluz\Common\Exception\CommonException |
||
| 175 | */ |
||
| 176 | 32 | public function __construct($options = null) |
|
| 177 | { |
||
| 178 | // initial default helper path |
||
| 179 | 32 | $this->addHelperPath(__DIR__ . '/Helper/'); |
|
| 180 | |||
| 181 | 32 | if ($options) { |
|
| 182 | $this->setOptions($options); |
||
| 183 | } |
||
| 184 | |||
| 185 | 32 | if ($this->getUid()) { |
|
| 186 | 32 | $this->prefix = $this->getUid() . '-'; |
|
| 187 | } |
||
| 188 | |||
| 189 | 32 | $this->init(); |
|
| 190 | |||
| 191 | 32 | $this->processRequest(); |
|
| 192 | 32 | } |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Initialize Grid |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | abstract public function init(): void; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set source adapter |
||
| 203 | * |
||
| 204 | * @param Source\AbstractSource $adapter |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | 32 | public function setAdapter(Source\AbstractSource $adapter): void |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get source adapter |
||
| 215 | * |
||
| 216 | * @return Source\AbstractSource |
||
| 217 | * @throws GridException |
||
| 218 | */ |
||
| 219 | 15 | public function getAdapter(): Source\AbstractSource |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Get unique Grid Id |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 32 | public function getUid(): string |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Get prefix |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | 1 | public function getPrefix(): string |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Set module |
||
| 249 | * |
||
| 250 | * @param string $module |
||
| 251 | * |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | 1 | public function setModule(string $module): void |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Get module |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 13 | public function getModule(): ?string |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Set controller |
||
| 271 | * |
||
| 272 | * @param string $controller |
||
| 273 | * |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | 1 | public function setController(string $controller): void |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Get controller |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 13 | public function getController(): ?string |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Process request |
||
| 293 | * |
||
| 294 | * Example of request url |
||
| 295 | * - http://domain.com/pages/grid/ |
||
| 296 | * - http://domain.com/pages/grid/page/2/ |
||
| 297 | * - http://domain.com/pages/grid/page/2/order-alias/desc/ |
||
| 298 | * - http://domain.com/pages/grid/page/2/order-created/desc/order-alias/asc/ |
||
| 299 | * |
||
| 300 | * with prefix for support more than one grid on page |
||
| 301 | * - http://domain.com/users/grid/users-page/2/users-order-created/desc/ |
||
| 302 | * - http://domain.com/users/grid/users-page/2/users-filter-status/active/ |
||
| 303 | * |
||
| 304 | * hash support |
||
| 305 | * - http://domain.com/pages/grid/#/page/2/order-created/desc/order-alias/asc/ |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | * @throws GridException |
||
| 309 | */ |
||
| 310 | 32 | public function processRequest(): void |
|
| 311 | { |
||
| 312 | 32 | $this->module = Request::getModule(); |
|
| 313 | 32 | $this->controller = Request::getController(); |
|
| 314 | |||
| 315 | 32 | $page = (int)Request::getParam($this->prefix . 'page', 1); |
|
| 316 | 32 | $this->setPage($page); |
|
| 317 | |||
| 318 | 32 | $limit = (int)Request::getParam($this->prefix . 'limit', $this->limit); |
|
| 319 | 32 | $this->setLimit($limit); |
|
| 320 | |||
| 321 | 32 | foreach ($this->allowOrders as $column) { |
|
| 322 | 32 | $alias = $this->applyAlias($column); |
|
| 323 | 32 | $order = Request::getParam($this->prefix . 'order-' . $alias); |
|
| 324 | 32 | if (null !== $order) { |
|
| 325 | 32 | $this->addOrder($column, $order); |
|
| 326 | } |
||
| 327 | } |
||
| 328 | 32 | foreach ($this->allowFilters as $column) { |
|
| 329 | 32 | $alias = $this->applyAlias($column); |
|
| 330 | 32 | $filter = Request::getParam($this->prefix . 'filter-' . $alias); |
|
| 331 | |||
| 332 | 32 | if (null !== $filter) { |
|
| 333 | 1 | $filter = trim($filter, ' _-'); |
|
| 334 | 1 | if (strpos($filter, '-')) { |
|
| 335 | /** |
||
| 336 | * Example of filters |
||
| 337 | * - http://domain.com/users/grid/users-filter-roleId/gt-2 - roleId greater than 2 |
||
| 338 | * - http://domain.com/users/grid/users-filter-roleId/gt-1_lt-4 - 1 < roleId < 4 |
||
| 339 | * - http://domain.com/users/grid/users-filter-login/eq-admin - login == admin |
||
| 340 | * - http://domain.com/users/grid/users-filter-login/like-adm - login LIKE `adm` |
||
| 341 | * - http://domain.com/users/grid/users-filter-login/like-od- - login LIKE `od-` |
||
| 342 | */ |
||
| 343 | 1 | $filters = explode('_', $filter); |
|
| 344 | 1 | foreach ($filters as $rawFilter) { |
|
| 345 | 1 | [$filterName, $filterValue] = explode('-', $rawFilter, 2); |
|
| 346 | 1 | $this->addFilter($column, $filterName, $filterValue); |
|
| 347 | } |
||
| 348 | } else { |
||
| 349 | /** |
||
| 350 | * Example of filters |
||
| 351 | * - http://domain.com/users/grid/users-filter-roleId/2 |
||
| 352 | * - http://domain.com/users/grid/users-filter-login/admin |
||
| 353 | */ |
||
| 354 | 32 | $this->addFilter($column, self::FILTER_EQ, $filter); |
|
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | 32 | } |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Process source |
||
| 362 | * |
||
| 363 | * @return void |
||
| 364 | * @throws GridException |
||
| 365 | */ |
||
| 366 | 15 | public function processSource(): void |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Get data |
||
| 386 | * |
||
| 387 | * @return Data |
||
| 388 | * @throws \Bluz\Grid\GridException |
||
| 389 | */ |
||
| 390 | 15 | public function getData(): Data |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Setup params |
||
| 400 | * |
||
| 401 | * @param $params |
||
| 402 | * |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | public function setParams($params): void |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Return params prepared for url builder |
||
| 412 | * |
||
| 413 | * @param array $rewrite |
||
| 414 | * |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | 13 | public function getParams(array $rewrite = []): array |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Get Url |
||
| 465 | * |
||
| 466 | * @param array $params |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | 13 | public function getUrl($params): string |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Add column name for allow order |
||
| 485 | * |
||
| 486 | * @param string $column |
||
| 487 | * |
||
| 488 | * @return void |
||
| 489 | */ |
||
| 490 | 32 | public function addAllowOrder($column): void |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Set allow orders |
||
| 497 | * |
||
| 498 | * @param string[] $orders |
||
| 499 | * |
||
| 500 | * @return void |
||
| 501 | */ |
||
| 502 | 32 | public function setAllowOrders(array $orders = []): void |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Get allow orders |
||
| 512 | * |
||
| 513 | * @return array |
||
| 514 | */ |
||
| 515 | 3 | public function getAllowOrders(): array |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Check order column |
||
| 522 | * |
||
| 523 | * @param string $column |
||
| 524 | * |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | 3 | protected function checkOrderColumn($column): bool |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Check order name |
||
| 534 | * |
||
| 535 | * @param string $order |
||
| 536 | * |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | 2 | protected function checkOrderName($order): bool |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Add order rule |
||
| 546 | * |
||
| 547 | * @param string $column |
||
| 548 | * @param string $order |
||
| 549 | * |
||
| 550 | * @return void |
||
| 551 | * @throws GridException |
||
| 552 | */ |
||
| 553 | 3 | public function addOrder($column, $order = self::ORDER_ASC): void |
|
| 554 | { |
||
| 555 | 3 | if (!$this->checkOrderColumn($column)) { |
|
| 556 | throw new GridException("Order for column `$column` is not allowed"); |
||
| 557 | } |
||
| 558 | |||
| 559 | 3 | if (!$this->checkOrderName($order)) { |
|
| 560 | throw new GridException("Order name for column `$column` is incorrect"); |
||
| 561 | } |
||
| 562 | |||
| 563 | 3 | $this->orders[$column] = $order; |
|
| 564 | 3 | } |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Add order rules |
||
| 568 | * |
||
| 569 | * @param array $orders |
||
| 570 | * |
||
| 571 | * @return void |
||
| 572 | * @throws GridException |
||
| 573 | */ |
||
| 574 | 1 | public function addOrders(array $orders): void |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Set order |
||
| 583 | * |
||
| 584 | * @param string $column |
||
| 585 | * @param string $order ASC or DESC |
||
| 586 | * |
||
| 587 | * @return void |
||
| 588 | * @throws GridException |
||
| 589 | */ |
||
| 590 | 1 | public function setOrder($column, $order = self::ORDER_ASC): void |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Set orders |
||
| 598 | * |
||
| 599 | * @param array $orders |
||
| 600 | * |
||
| 601 | * @return void |
||
| 602 | * @throws GridException |
||
| 603 | */ |
||
| 604 | 1 | public function setOrders(array $orders): void |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Get orders |
||
| 612 | * |
||
| 613 | * @return array |
||
| 614 | */ |
||
| 615 | 25 | public function getOrders(): array |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Add column name to allow filter it |
||
| 626 | * |
||
| 627 | * @param string $column |
||
| 628 | * |
||
| 629 | * @return void |
||
| 630 | */ |
||
| 631 | 32 | public function addAllowFilter($column): void |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Set allowed filters |
||
| 638 | * |
||
| 639 | * @param string[] $filters |
||
| 640 | * |
||
| 641 | * @return void |
||
| 642 | */ |
||
| 643 | 32 | public function setAllowFilters(array $filters = []): void |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Get allow filters |
||
| 653 | * |
||
| 654 | * @return array |
||
| 655 | */ |
||
| 656 | 12 | public function getAllowFilters(): array |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Check filter column |
||
| 663 | * |
||
| 664 | * @param string $column |
||
| 665 | * |
||
| 666 | * @return bool |
||
| 667 | */ |
||
| 668 | 12 | protected function checkFilterColumn($column): bool |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Check filter |
||
| 676 | * |
||
| 677 | * @param string $filter |
||
| 678 | * |
||
| 679 | * @return bool |
||
| 680 | */ |
||
| 681 | 12 | protected function checkFilterName($filter): bool |
|
| 685 | |||
| 686 | /** |
||
| 687 | * Add filter |
||
| 688 | * |
||
| 689 | * @param string $column name |
||
| 690 | * @param string $filter |
||
| 691 | * @param string $value |
||
| 692 | * |
||
| 693 | * @return void |
||
| 694 | * @throws GridException |
||
| 695 | */ |
||
| 696 | 10 | public function addFilter($column, $filter, $value): void |
|
| 709 | |||
| 710 | |||
| 711 | /** |
||
| 712 | * Get filter |
||
| 713 | * |
||
| 714 | * @param string $column |
||
| 715 | * @param string $filter |
||
| 716 | * |
||
| 717 | * @return mixed |
||
| 718 | */ |
||
| 719 | public function getFilter($column, $filter = null) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Get filters |
||
| 729 | * |
||
| 730 | * @return array |
||
| 731 | */ |
||
| 732 | 21 | public function getFilters(): array |
|
| 736 | |||
| 737 | /** |
||
| 738 | * Add alias for column name |
||
| 739 | * |
||
| 740 | * @param string $column |
||
| 741 | * @param string $alias |
||
| 742 | * |
||
| 743 | * @return void |
||
| 744 | */ |
||
| 745 | 30 | public function addAlias($column, $alias): void |
|
| 749 | |||
| 750 | /** |
||
| 751 | * Get column name by alias |
||
| 752 | * |
||
| 753 | * @param string $alias |
||
| 754 | * |
||
| 755 | * @return string |
||
| 756 | */ |
||
| 757 | protected function reverseAlias($alias): string |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Get alias by column name |
||
| 764 | * |
||
| 765 | * @param string $column |
||
| 766 | * |
||
| 767 | * @return string |
||
| 768 | */ |
||
| 769 | 32 | public function applyAlias($column): string |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Set page |
||
| 776 | * |
||
| 777 | * @param integer $page |
||
| 778 | * |
||
| 779 | * @return void |
||
| 780 | * @throws GridException |
||
| 781 | */ |
||
| 782 | 32 | public function setPage(int $page = 1): void |
|
| 789 | |||
| 790 | /** |
||
| 791 | * Get page |
||
| 792 | * |
||
| 793 | * @return integer |
||
| 794 | */ |
||
| 795 | 17 | public function getPage(): int |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Set limit per page |
||
| 802 | * |
||
| 803 | * @param integer $limit |
||
| 804 | * |
||
| 805 | * @return void |
||
| 806 | * @throws GridException |
||
| 807 | */ |
||
| 808 | 32 | public function setLimit(int $limit): void |
|
| 815 | |||
| 816 | /** |
||
| 817 | * Get limit per page |
||
| 818 | * |
||
| 819 | * @return integer |
||
| 820 | */ |
||
| 821 | 24 | public function getLimit(): int |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Set default limit |
||
| 828 | * |
||
| 829 | * @param integer $limit |
||
| 830 | * |
||
| 831 | * @return void |
||
| 832 | * @throws GridException |
||
| 833 | */ |
||
| 834 | 32 | public function setDefaultLimit(int $limit): void |
|
| 843 | |||
| 844 | /** |
||
| 845 | * Get default limit |
||
| 846 | * |
||
| 847 | * @return integer |
||
| 848 | */ |
||
| 849 | 1 | public function getDefaultLimit(): int |
|
| 853 | |||
| 854 | /** |
||
| 855 | * Set default order |
||
| 856 | * |
||
| 857 | * @param string $column |
||
| 858 | * @param string $order ASC or DESC |
||
| 859 | * |
||
| 860 | * @return void |
||
| 861 | */ |
||
| 862 | 3 | public function setDefaultOrder($column, $order = self::ORDER_ASC): void |
|
| 866 | |||
| 867 | /** |
||
| 868 | * Get default order |
||
| 869 | * |
||
| 870 | * @return array |
||
| 871 | */ |
||
| 872 | 22 | public function getDefaultOrder(): ?array |
|
| 876 | } |
||
| 877 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.