Complex classes like DataTable 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 DataTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class DataTable { |
||
| 19 | |||
| 20 | /** @var Request */ |
||
| 21 | private $_request; |
||
| 22 | |||
| 23 | /** @var Builder */ |
||
| 24 | private $_queryBuilder; |
||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | private $_headerFormatters = []; |
||
| 28 | |||
| 29 | /** @var array */ |
||
| 30 | private $_components = []; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | private $_classes; |
||
| 34 | |||
| 35 | /** @var array */ |
||
| 36 | private $_columns = []; |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | private $_relations = []; |
||
| 40 | |||
| 41 | /** @var string */ |
||
| 42 | private $_noDataHtml = '<div>no data</div>'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * DataTable constructor. |
||
| 46 | * @param Request $request |
||
| 47 | */ |
||
| 48 | 46 | public function __construct(Request $request) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Set the base model whose data is displayed in the table. |
||
| 55 | * |
||
| 56 | * @param string $modelName |
||
| 57 | * @param array $columns |
||
| 58 | * @return $this |
||
| 59 | * @throws \RuntimeException |
||
| 60 | */ |
||
| 61 | 45 | public function model(string $modelName, array $columns = []): DataTable |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Returns an array of Column objects which may be bound to a formatter. |
||
| 76 | * |
||
| 77 | * @param array $columns |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | 44 | private function _fetchColumns(array $columns): array |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param $column |
||
| 94 | * @param $formatter |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 38 | private function _setColumnFormatter($column, $formatter): array |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @return Builder |
||
| 110 | */ |
||
| 111 | 19 | public function query(): Builder |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Displayed columns |
||
| 118 | * |
||
| 119 | * @param array $columns |
||
| 120 | * @return $this |
||
| 121 | */ |
||
| 122 | 2 | public function columns(array $columns): DataTable |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Add a component to the data table. |
||
| 131 | * For example a "Paginator" or a "Sorter". |
||
| 132 | * |
||
| 133 | * @param DataComponent $component |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | 23 | public function addComponent(DataComponent $component): DataTable |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Add a formatter for the column headers. |
||
| 147 | * |
||
| 148 | * @param HeaderFormatter $headerFormatter |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | 8 | public function formatHeaders(HeaderFormatter $headerFormatter): DataTable |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Add a formatter for a column. |
||
| 160 | * |
||
| 161 | * @param string $columnName |
||
| 162 | * @param ColumnFormatter $columnFormatter |
||
| 163 | * @return DataTable |
||
| 164 | */ |
||
| 165 | 2 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter): DataTable |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Add classes to the table. |
||
| 187 | * |
||
| 188 | * @param string $classes |
||
| 189 | * |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | 1 | public function classes(string $classes): DataTable |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Add a relation to the table. |
||
| 201 | * |
||
| 202 | * @param array $relations |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | 3 | public function with(array $relations): DataTable |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Set the HTML which should be displayed when the dataset is empty. |
||
| 214 | * |
||
| 215 | * @param string $html |
||
| 216 | * @return DataTable |
||
| 217 | */ |
||
| 218 | 1 | public function noDataHtml(string $html): DataTable |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Set a view which should be displayed when the dataset is empty. |
||
| 227 | * |
||
| 228 | * @param string $viewName |
||
| 229 | * @return DataTable |
||
| 230 | */ |
||
| 231 | 1 | public function noDataView(string $viewName): DataTable |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Renders the table. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | * @throws \RuntimeException |
||
| 243 | */ |
||
| 244 | 34 | public function render(): string |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Get data which should be displayed in the table. |
||
| 260 | * |
||
| 261 | * @return Collection |
||
| 262 | * |
||
| 263 | * @throws \RuntimeException |
||
| 264 | */ |
||
| 265 | 34 | private function _getData(): Collection |
|
| 282 | |||
| 283 | 33 | private function _addRelations() |
|
| 290 | |||
| 291 | 29 | private function _initColumns() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Starts the table. |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 29 | private function _open(): string |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Renders the column headers. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | 29 | private function _renderHeaders(): string |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | 29 | private function _fetchHeaders(): array |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Displays the table body. |
||
| 345 | * |
||
| 346 | * @param Collection $data |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 29 | private function _renderBody(Collection $data): string |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Displays a single row. |
||
| 363 | * |
||
| 364 | * @param Model $rowModel |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 29 | private function _renderRow(Model $rowModel): string |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get all the mutated attributes which are needed. |
||
| 385 | * |
||
| 386 | * @param Model $model |
||
| 387 | * @param array $columns |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | 29 | private function _getMutatedAttributes(Model $model, array $columns = []): array |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Get all column names. |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | 29 | private function _getColumnNames(): array |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Get only the columns which are attributes from the base model. |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | 29 | private function _getColumnsWithoutRelations(): array |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @param Model $model |
||
| 436 | * @param Column $column |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | 3 | private function _getColumnValueFromRelation(Model $model, Column $column): string |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Closes the table. |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | 29 | private function _close(): string |
|
| 461 | } |