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 |
||
| 11 | class Grid |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $template = 'laravel-grid::grid'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var GridHelper |
||
| 20 | */ |
||
| 21 | protected $gridHelper; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var SourceInterface |
||
| 25 | */ |
||
| 26 | protected $source; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Column[] |
||
| 30 | */ |
||
| 31 | protected $columns = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $perPage = 15; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $emptyMessage = 'Brak danych do wyświetlenia.'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Rows |
||
| 45 | */ |
||
| 46 | protected $rows; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Total number of records. |
||
| 50 | * |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $total = 0; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var RowAction[] |
||
| 57 | */ |
||
| 58 | protected $rowActions = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $defaultOrder = [ |
||
| 64 | 'column' => 'id', |
||
| 65 | 'direction' => 'desc' |
||
| 66 | ]; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Order |
||
| 70 | */ |
||
| 71 | protected $order; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $enablePagination = true; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var callable |
||
| 80 | */ |
||
| 81 | protected $afterCallback; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $viewData = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param GridHelper $gridHelper |
||
| 90 | */ |
||
| 91 | public function __construct(GridHelper $gridHelper) |
||
| 97 | |||
| 98 | public function buildGrid() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return GridHelper |
||
| 105 | */ |
||
| 106 | public function getGridHelper() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param SourceInterface $source |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | public function setSource(SourceInterface $source) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $name |
||
| 124 | * @param array $options |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | public function addColumn($name, array $options = []) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return Column[] |
||
| 143 | */ |
||
| 144 | public function getColumns() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param Order $order |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | public function setDefaultOrder(Order $order) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return Order |
||
| 162 | */ |
||
| 163 | public function getOrder() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param int $perPage |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function setPerPage($perPage) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return int |
||
| 181 | */ |
||
| 182 | public function getPerPage() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getEmptyMessage() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $emptyMessage |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function setEmptyMessage($emptyMessage) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param RowAction $rowAction |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function addRowAction(RowAction $rowAction) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param bool $flag |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function setEnablePagination($flag) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function isPaginationEnabled() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param callable $callback |
||
| 240 | * @deprecated |
||
| 241 | */ |
||
| 242 | public function each(callable $callback) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param callable $callback |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | public function after(callable $callback) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param Component $component |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function addComponent(Component $component) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param array $viewData |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function setViewData($viewData) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return \Illuminate\View\View |
||
| 283 | */ |
||
| 284 | public function render() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Is table filterable? |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public function isFilterable() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return Rows |
||
| 323 | */ |
||
| 324 | public function getRows() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param Rows $rows |
||
| 380 | * @return LengthAwarePaginator |
||
| 381 | */ |
||
| 382 | protected function getPaginator(Rows $rows) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return mixed |
||
| 391 | */ |
||
| 392 | protected function execute() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return int |
||
| 407 | */ |
||
| 408 | protected function resolveCurrentPage() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | protected function resolveCurrentPath() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | protected function getValidatorRules() |
||
| 439 | |||
| 440 | protected function makeDefaultOrder() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $name |
||
| 449 | * @param array $options |
||
| 450 | * @return Column |
||
| 451 | */ |
||
| 452 | protected function makeColumn($name, array $options = []) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param string $name |
||
| 461 | * @param array $options |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | protected function setupColumnOptions($name, array $options) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | public function __toString() |
||
| 478 | } |
||
| 479 |