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 $eachCallback; |
||
| 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 | */ |
||
| 198 | public function setEmptyMessage($emptyMessage) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param RowAction $rowAction |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function addRowAction(RowAction $rowAction) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param bool $flag |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function setEnablePagination($flag) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function isPaginationEnabled() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param callable $callback |
||
| 237 | */ |
||
| 238 | public function each(callable $callback) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param Component $component |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | public function addComponent(Component $component) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param array $viewData |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function setViewData($viewData) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return \Illuminate\View\View; |
||
|
|
|||
| 268 | */ |
||
| 269 | public function render() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Is table filterable? |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function isFilterable() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return Rows |
||
| 308 | */ |
||
| 309 | public function getRows() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param Rows $rows |
||
| 362 | * @return LengthAwarePaginator |
||
| 363 | */ |
||
| 364 | protected function getPaginator(Rows $rows) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | protected function execute() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return int |
||
| 387 | */ |
||
| 388 | protected function resolveCurrentPage() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | protected function resolveCurrentPath() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | protected function getValidatorRules() |
||
| 419 | |||
| 420 | protected function makeDefaultOrder() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param string $name |
||
| 429 | * @param array $options |
||
| 430 | * @return Column |
||
| 431 | */ |
||
| 432 | protected function makeColumn($name, array $options = []) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $name |
||
| 441 | * @param array $options |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | protected function setupColumnOptions($name, array $options) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public function __toString() |
||
| 458 | } |
||
| 459 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.