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 |
||
| 19 | class DataTable { |
||
| 20 | |||
| 21 | /** @var Request */ |
||
| 22 | private $_request; |
||
| 23 | |||
| 24 | /** @var Builder */ |
||
| 25 | private $_queryBuilder; |
||
| 26 | |||
| 27 | /** @var array */ |
||
| 28 | private $_headerFormatters = []; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | private $_components = []; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | private $_classes; |
||
| 35 | |||
| 36 | /** @var array */ |
||
| 37 | private $_columns = []; |
||
| 38 | |||
| 39 | /** @var array */ |
||
| 40 | private $_relations = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * DataTable constructor. |
||
| 44 | * @param Request $request |
||
| 45 | */ |
||
| 46 | 39 | public function __construct(Request $request) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Set the base model whose data is displayed in the table. |
||
| 53 | * |
||
| 54 | * @param string $modelName |
||
| 55 | * @param array $columns |
||
| 56 | * @return $this |
||
| 57 | * @throws \RuntimeException |
||
| 58 | */ |
||
| 59 | 38 | public function model(string $modelName, array $columns = []): DataTable |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Returns an array of Column objects which may be bound to a formatter. |
||
| 79 | * |
||
| 80 | * @param array $columns |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | 37 | private function _fetchColumns(array $columns): array |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @return Builder |
||
| 101 | */ |
||
| 102 | 17 | public function query(): Builder |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Displayed columns |
||
| 109 | * |
||
| 110 | * @param array $columns |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | 2 | public function columns(array $columns): DataTable |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Add a component to the data table. |
||
| 122 | * For example a "Paginator" or a "Sorter". |
||
| 123 | * |
||
| 124 | * @param DataComponent $component |
||
| 125 | * |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | 18 | public function addComponent(DataComponent $component): DataTable |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Add a formatter for the column headers. |
||
| 138 | * |
||
| 139 | * @param HeaderFormatter $headerFormatter |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | 8 | public function formatHeaders(HeaderFormatter $headerFormatter): DataTable |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Add a formatter for a column. |
||
| 151 | * |
||
| 152 | * @param string $columnName |
||
| 153 | * @param ColumnFormatter $columnFormatter |
||
| 154 | * @return DataTable |
||
| 155 | */ |
||
| 156 | 2 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter): DataTable |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Add classes to the table. |
||
| 178 | * |
||
| 179 | * @param string $classes |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | 1 | public function classes(string $classes): DataTable |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Add a relation to the table. |
||
| 192 | * |
||
| 193 | * @param array $relations |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | 2 | public function with(array $relations): DataTable |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Renders the table. |
||
| 205 | * |
||
| 206 | * @param null|Closure $noDataView |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | * @throws \RuntimeException |
||
| 210 | */ |
||
| 211 | 31 | public function render(Closure $noDataView = null): string |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Get data which should be displayed in the table. |
||
| 225 | * |
||
| 226 | * @return Collection |
||
| 227 | * |
||
| 228 | * @throws \RuntimeException |
||
| 229 | */ |
||
| 230 | 31 | private function _getData(): Collection |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Starts the table. |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 27 | private function _open(): string |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Renders the column headers. |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 27 | private function _renderHeaders(): string |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Displays the table body. |
||
| 297 | * |
||
| 298 | * @param Collection $data |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | 27 | private function _renderBody(Collection $data): string |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Displays a single row. |
||
| 315 | * |
||
| 316 | * @param Model $rowModel |
||
| 317 | * |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 27 | private function _renderRow(Model $rowModel): string |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Get all the mutated attributes which are needed. |
||
| 345 | * |
||
| 346 | * @param Model $model |
||
| 347 | * @param array $columns |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | 27 | private function _getMutatedAttributes(Model $model, array $columns = []): array |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Get all column names. |
||
| 363 | * |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | 27 | private function _getColumnNames(): array |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @param Model $model |
||
| 386 | * @param Column $column |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | 2 | private function _getColumnValueFromRelation(Model $model, Column $column): string |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Closes the table. |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | 27 | private function _close(): string |
|
| 419 | } |