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 |
||
| 14 | class Grid |
||
| 15 | { |
||
| 16 | protected $template = 'laravel-grid::grid'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Request |
||
| 20 | */ |
||
| 21 | protected $request; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var ValidationFactory |
||
| 25 | */ |
||
| 26 | protected $validator; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var HtmlBuilder |
||
| 30 | */ |
||
| 31 | protected $htmlBuilder; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var FormBuilder |
||
| 35 | */ |
||
| 36 | protected $formBuilder; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var SourceInterface |
||
| 40 | */ |
||
| 41 | protected $source; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var Column[] |
||
| 45 | */ |
||
| 46 | protected $columns = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | protected $perPage = 15; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $emptyMessage = 'Brak danych do wyświetlenia.'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Rows |
||
| 60 | */ |
||
| 61 | protected $rows; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Total number of records. |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | protected $total = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var RowAction[] |
||
| 72 | */ |
||
| 73 | protected $rowActions = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $defaultOrder = [ |
||
| 79 | 'column' => 'id', |
||
| 80 | 'direction' => 'desc' |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var Order |
||
| 85 | */ |
||
| 86 | protected $order; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $enablePagination = true; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var callable |
||
| 95 | */ |
||
| 96 | protected $eachCallback; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $data = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param Request $request |
||
| 105 | * @param ValidationFactory $validator |
||
| 106 | * @param HtmlBuilder $htmlBuilder |
||
| 107 | * @param FormBuilder $formBuilder |
||
| 108 | */ |
||
| 109 | public function __construct( |
||
| 122 | |||
| 123 | public function buildGrid() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return Request |
||
| 130 | */ |
||
| 131 | public function getRequest() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return HtmlBuilder |
||
| 138 | */ |
||
| 139 | public function getHtmlBuilder() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return FormBuilder |
||
| 146 | */ |
||
| 147 | public function getFormBuilder() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param SourceInterface $source |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function setSource(SourceInterface $source) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $name |
||
| 165 | * @param array $options |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function addColumn($name, array $options = []) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return Column[] |
||
| 184 | */ |
||
| 185 | public function getColumns() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param Order $order |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function setDefaultOrder(Order $order) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return Order |
||
| 203 | */ |
||
| 204 | public function getOrder() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param int $perPage |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setPerPage($perPage) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return int |
||
| 222 | */ |
||
| 223 | public function getPerPage() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function getEmptyMessage() |
||
| 232 | { |
||
| 233 | return $this->emptyMessage; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $emptyMessage |
||
| 238 | */ |
||
| 239 | public function setEmptyMessage($emptyMessage) |
||
| 240 | { |
||
| 241 | $this->emptyMessage = $emptyMessage; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param RowAction $rowAction |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function addRowAction(RowAction $rowAction) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param bool $flag |
||
| 258 | * @return $this |
||
| 259 | */ |
||
| 260 | public function setEnablePagination($flag) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function isPaginationEnabled() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param callable $callback |
||
| 278 | */ |
||
| 279 | public function each(callable $callback) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param array $data |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function setData($data) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function render() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Is table filterable? |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | public function isFilterable() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return Rows |
||
| 337 | */ |
||
| 338 | public function getRows() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param Rows $rows |
||
| 391 | * @return LengthAwarePaginator |
||
| 392 | */ |
||
| 393 | protected function getPaginator(Rows $rows) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return mixed |
||
| 402 | */ |
||
| 403 | protected function execute() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @return int |
||
| 416 | */ |
||
| 417 | protected function resolveCurrentPage() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | protected function resolveCurrentPath() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @return \Illuminate\Contracts\Validation\Validator |
||
| 432 | */ |
||
| 433 | protected function getValidatorInstance() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | protected function getValidatorRules() |
||
| 456 | |||
| 457 | protected function makeDefaultOrder() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $name |
||
| 466 | * @param array $options |
||
| 467 | * @return Column |
||
| 468 | */ |
||
| 469 | protected function makeColumn($name, array $options = []) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param string $name |
||
| 478 | * @param array $options |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | protected function setupColumnOptions($name, array $options) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | public function __toString() |
||
| 495 | } |
||
| 496 |