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 |
||
| 20 | class DataTable { |
||
| 21 | |||
| 22 | /** @var Request */ |
||
| 23 | private $_request; |
||
| 24 | |||
| 25 | /** @var Builder */ |
||
| 26 | private $_queryBuilder; |
||
| 27 | |||
| 28 | /** @var array */ |
||
| 29 | private $_headerFormatters = []; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | private $_components = []; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | private $_classes; |
||
| 36 | |||
| 37 | /** @var array */ |
||
| 38 | private $_columns = []; |
||
| 39 | |||
| 40 | /** @var array */ |
||
| 41 | private $_relations = []; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | private $_noDataHtml = '<div>no data</div>'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * DataTable constructor. |
||
| 48 | * @param Request $request |
||
| 49 | */ |
||
| 50 | 43 | public function __construct(Request $request) |
|
| 51 | { |
||
| 52 | 43 | $this->_request = $request; |
|
| 53 | 43 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Set the base model whose data is displayed in the table. |
||
| 57 | * |
||
| 58 | * @param string $modelName |
||
| 59 | * @param array $columns |
||
| 60 | * @return $this |
||
| 61 | * @throws \RuntimeException |
||
| 62 | */ |
||
| 63 | 42 | public function model(string $modelName, array $columns = []): DataTable |
|
| 64 | { |
||
| 65 | 42 | if (!\class_exists($modelName) || !\is_subclass_of($modelName, Model::class)) |
|
| 66 | { |
||
| 67 | 2 | throw new RuntimeException('Class "' . $modelName . '" does not exist or is not an active record!'); |
|
| 68 | } |
||
| 69 | |||
| 70 | 40 | $this->_queryBuilder = (new $modelName)->newQuery(); |
|
| 71 | 40 | $this->_columns = $this->_fetchColumns($columns); |
|
| 72 | |||
| 73 | 40 | return $this; |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns an array of Column objects which may be bound to a formatter. |
||
| 78 | * |
||
| 79 | * @param array $columns |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | 41 | private function _fetchColumns(array $columns): array |
|
| 83 | { |
||
| 84 | 41 | $columnModels = []; |
|
| 85 | 41 | foreach ($columns as $column => $formatter) |
|
| 86 | { |
||
| 87 | 37 | if (\is_int($column)) |
|
| 88 | { |
||
| 89 | 35 | $column = $formatter; |
|
| 90 | 35 | $formatter = null; |
|
| 91 | } |
||
| 92 | 37 | $columnModels[] = new Column($column, $formatter); |
|
| 93 | } |
||
| 94 | |||
| 95 | 41 | return $columnModels; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return Builder |
||
| 100 | */ |
||
| 101 | 19 | public function query(): Builder |
|
| 102 | { |
||
| 103 | 19 | return $this->_queryBuilder; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Displayed columns |
||
| 108 | * |
||
| 109 | * @param array $columns |
||
| 110 | * @return $this |
||
| 111 | */ |
||
| 112 | 2 | public function columns(array $columns): DataTable |
|
| 113 | { |
||
| 114 | 2 | $this->_columns += $this->_fetchColumns($columns); |
|
| 115 | |||
| 116 | 2 | return $this; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Add a component to the data table. |
||
| 121 | * For example a "Paginator" or a "Sorter". |
||
| 122 | * |
||
| 123 | * @param DataComponent $component |
||
| 124 | * |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | 20 | public function addComponent(DataComponent $component): DataTable |
|
| 128 | { |
||
| 129 | 20 | $component->init($this->_queryBuilder, $this->_request); |
|
| 130 | 20 | $this->_components[] = $component; |
|
| 131 | |||
| 132 | 20 | return $this; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add a formatter for the column headers. |
||
| 137 | * |
||
| 138 | * @param HeaderFormatter $headerFormatter |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | 8 | public function formatHeaders(HeaderFormatter $headerFormatter): DataTable |
|
| 142 | { |
||
| 143 | 8 | $this->_headerFormatters[] = $headerFormatter; |
|
| 144 | |||
| 145 | 8 | return $this; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Add a formatter for a column. |
||
| 150 | * |
||
| 151 | * @param string $columnName |
||
| 152 | * @param ColumnFormatter $columnFormatter |
||
| 153 | * @return DataTable |
||
| 154 | */ |
||
| 155 | 2 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter): DataTable |
|
| 156 | { |
||
| 157 | /** @var Column $column */ |
||
| 158 | 2 | $column = \array_first( |
|
| 159 | 2 | $this->_columns, |
|
| 160 | function ($index, $column) use ($columnName) |
||
| 161 | { |
||
| 162 | /** @var Column $column */ |
||
| 163 | 2 | return $column->getName() === $columnName; |
|
| 164 | 2 | } |
|
| 165 | ); |
||
| 166 | |||
| 167 | 2 | if ($column !== null) |
|
| 168 | { |
||
| 169 | 2 | $column->setFormatter($columnFormatter); |
|
| 170 | } |
||
| 171 | |||
| 172 | 2 | return $this; |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Add classes to the table. |
||
| 177 | * |
||
| 178 | * @param string $classes |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | 1 | public function classes(string $classes): DataTable |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Add a relation to the table. |
||
| 191 | * |
||
| 192 | * @param array $relations |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | 3 | public function with(array $relations): DataTable |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Set the HTML which should be displayed when the dataset is empty. |
||
| 204 | * |
||
| 205 | * @param string $html |
||
| 206 | * @return DataTable |
||
| 207 | */ |
||
| 208 | 1 | public function noDataHtml(string $html): DataTable |
|
| 209 | { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Set a view which should be displayed when the dataset is empty. |
||
| 217 | * |
||
| 218 | * @param string $viewName |
||
| 219 | * @return DataTable |
||
| 220 | */ |
||
| 221 | 1 | public function noDataView(string $viewName): DataTable |
|
| 222 | { |
||
| 223 | 1 | $this->_noDataHtml = \view($viewName)->render(); |
|
| 224 | |||
| 225 | 1 | return $this; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Renders the table. |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | * @throws \RuntimeException |
||
| 233 | */ |
||
| 234 | 33 | public function render(): string |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Get data which should be displayed in the table. |
||
| 250 | * |
||
| 251 | * @return Collection |
||
| 252 | * |
||
| 253 | * @throws \RuntimeException |
||
| 254 | */ |
||
| 255 | 33 | private function _getData(): Collection |
|
| 272 | |||
| 273 | 32 | private function _addRelations() |
|
| 280 | |||
| 281 | 28 | private function _initColumns() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Starts the table. |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | 28 | private function _open(): string |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Renders the column headers. |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 28 | private function _renderHeaders(): string |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | 28 | private function _fetchHeaders(): array |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Displays the table body. |
||
| 335 | * |
||
| 336 | * @param Collection $data |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | 28 | private function _renderBody(Collection $data): string |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Displays a single row. |
||
| 353 | * |
||
| 354 | * @param Model $rowModel |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | 28 | private function _renderRow(Model $rowModel): string |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Get all the mutated attributes which are needed. |
||
| 375 | * |
||
| 376 | * @param Model $model |
||
| 377 | * @param array $columns |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | 28 | private function _getMutatedAttributes(Model $model, array $columns = []): array |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Get all column names. |
||
| 393 | * |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | 28 | private function _getColumnNames(): array |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Get only the columns which are attributes from the base model. |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | 28 | private function _getColumnsWithoutRelations(): array |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param Model $model |
||
| 426 | * @param Column $column |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | 3 | private function _getColumnValueFromRelation(Model $model, Column $column): string |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Closes the table. |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 28 | private function _close(): string |
|
| 451 | } |