Complex classes like CRUD 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 CRUD, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class CRUD |
||
| 13 | { |
||
| 14 | const BASE_URL_DELIMITER = '/~/'; |
||
| 15 | |||
| 16 | private $fields = []; |
||
| 17 | private $actions; |
||
| 18 | private $model = ''; |
||
| 19 | private $repo; |
||
| 20 | private $preferences; |
||
| 21 | private $tableIdentifier; |
||
| 22 | private $rawPerPage; |
||
| 23 | private $columns = []; |
||
| 24 | private $formClass = 'col-sm-12 col-md-12 col-lg-12'; |
||
| 25 | private $toolbar = []; |
||
| 26 | private $locales = []; |
||
| 27 | private $batchCheckboxesEnabled = false; |
||
| 28 | private $sortableWeightField = null; |
||
| 29 | private $softDeleteEnabled = false; |
||
| 30 | |||
| 31 | 18 | public function __construct(ModelRepository $repo, PreferencesRepository $preferences, ActionsContainer $actions) |
|
| 32 | { |
||
| 33 | 18 | $this->repo = $repo; |
|
| 34 | 18 | $this->preferences = $preferences; |
|
| 35 | 18 | $this->actions = $actions; |
|
| 36 | 18 | } |
|
| 37 | |||
| 38 | 17 | public function formClass(string $class = null) |
|
| 39 | { |
||
| 40 | 17 | if (!is_null($class)) { |
|
| 41 | 17 | $this->formClass = $class; |
|
| 42 | } |
||
| 43 | |||
| 44 | 17 | return $this->formClass; |
|
| 45 | } |
||
| 46 | |||
| 47 | public function getRawPerPage() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string|AbstractField $column |
||
| 54 | */ |
||
| 55 | public function addColumn($column) |
||
| 59 | |||
| 60 | 13 | public function getColumns() |
|
| 61 | { |
||
| 62 | 13 | return $this->columns; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get columns as field objects, or fields if there are no columns. |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function getColumnsAsFields() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get list of columns that initialized as field object. |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | 13 | public function getColumnsWithoutRelatedField() |
|
| 92 | { |
||
| 93 | 13 | $columns = []; |
|
| 94 | 13 | foreach ($this->getColumns() as $column) { |
|
| 95 | if (is_object($column)) { |
||
| 96 | $columns[] = $column; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | 13 | return $columns; |
|
| 101 | } |
||
| 102 | |||
| 103 | 13 | public function getAllFieldObjects() |
|
| 104 | { |
||
| 105 | 13 | $fieldsAndColumns = array_merge( |
|
| 106 | 13 | $this->getFieldsWithoutMarkup(), |
|
| 107 | 13 | $this->getColumnsWithoutRelatedField() |
|
| 108 | ); |
||
| 109 | |||
| 110 | 13 | return $fieldsAndColumns; |
|
| 111 | } |
||
| 112 | |||
| 113 | 1 | public function getFieldByName($name) |
|
|
|
|||
| 114 | { |
||
| 115 | 1 | $fields = $this->getFieldsWithoutMarkup(); |
|
| 116 | 1 | foreach ($fields as $field) { |
|
| 117 | 1 | if ($field->name() === $name) { |
|
| 118 | 1 | return $field; |
|
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return null; |
||
| 123 | } |
||
| 124 | |||
| 125 | 13 | public function addField(AbstractField $field) |
|
| 126 | { |
||
| 127 | 13 | $this->fields[] = $field; |
|
| 128 | 13 | } |
|
| 129 | |||
| 130 | 13 | public function getFields() |
|
| 131 | { |
||
| 132 | 13 | return $this->fields; |
|
| 133 | } |
||
| 134 | |||
| 135 | 13 | public function getFieldsWithoutMarkup() |
|
| 136 | { |
||
| 137 | 13 | $fields = []; |
|
| 138 | 13 | foreach ($this->getFields() as $field) { |
|
| 139 | 13 | $this->extractField($field, $fields); |
|
| 140 | } |
||
| 141 | |||
| 142 | 13 | return $fields; |
|
| 143 | } |
||
| 144 | |||
| 145 | 13 | private function extractField(AbstractField $field, &$fields) |
|
| 146 | { |
||
| 147 | 13 | if (!$field->isMarkupRow()) { |
|
| 148 | 13 | $fields[] = $field; |
|
| 149 | 13 | return; |
|
| 150 | } |
||
| 151 | |||
| 152 | foreach ($field->getFields() as $field) { |
||
| 153 | $this->extractField($field, $fields); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getTabs($skipHiddenType = null) |
||
| 171 | |||
| 172 | private function moveDefaultTabToEnd(&$tabs) |
||
| 183 | |||
| 184 | 13 | public function setModel($model) |
|
| 185 | { |
||
| 186 | 13 | $this->model = $model; |
|
| 187 | |||
| 188 | 13 | return $this; |
|
| 189 | } |
||
| 190 | |||
| 191 | 13 | public function repo() |
|
| 192 | { |
||
| 193 | 13 | return $this->repo->setCrud($this); |
|
| 194 | } |
||
| 195 | |||
| 196 | 4 | public function preferences() |
|
| 197 | { |
||
| 198 | 4 | return $this->preferences; |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param int|array $perPage |
||
| 203 | */ |
||
| 204 | public function paginate($perPage) |
||
| 220 | |||
| 221 | public function order(string $column, string $direction) |
||
| 227 | |||
| 228 | 13 | public function filter(\Closure $callback) |
|
| 229 | { |
||
| 230 | 13 | $this->repo()->filter($callback); |
|
| 231 | |||
| 232 | 13 | return $this; |
|
| 233 | } |
||
| 234 | |||
| 235 | 13 | public function getModel() |
|
| 236 | { |
||
| 237 | 13 | return $this->model; |
|
| 238 | } |
||
| 239 | |||
| 240 | public function hasAnyFieldFilter() |
||
| 250 | |||
| 251 | public function editUrl($id) |
||
| 255 | |||
| 256 | public function createUrl() |
||
| 260 | |||
| 261 | public function deleteUrl($id) |
||
| 265 | |||
| 266 | public function restoreUrl($id) |
||
| 270 | |||
| 271 | public function forceDeleteUrl($id) |
||
| 275 | |||
| 276 | public function toolbarUrl($identifier) |
||
| 280 | |||
| 281 | 5 | public function listUrl() |
|
| 282 | { |
||
| 283 | 5 | return $this->baseUrl(); |
|
| 284 | } |
||
| 285 | |||
| 286 | public function perPageUrl($perPage) |
||
| 290 | |||
| 291 | public function searchUrl() |
||
| 295 | |||
| 296 | public function relationSearchUrl() |
||
| 300 | |||
| 301 | 13 | public function inlineUrl() |
|
| 302 | { |
||
| 303 | 13 | return sprintf('%s%sinline', $this->baseUrl(), self::BASE_URL_DELIMITER); |
|
| 304 | } |
||
| 305 | |||
| 306 | public function orderUrl($column, $direction) |
||
| 310 | |||
| 311 | public function reorderUrl() |
||
| 315 | |||
| 316 | public function reorderMoveItemUrl($id) |
||
| 320 | |||
| 321 | 14 | public function baseUrl() |
|
| 322 | { |
||
| 323 | 14 | $chunks = explode(self::BASE_URL_DELIMITER, request()->url()); |
|
| 324 | |||
| 325 | 14 | return rtrim($chunks[0], '/'); |
|
| 326 | } |
||
| 327 | |||
| 328 | 17 | public function tableIdentifier($ident = null) |
|
| 329 | { |
||
| 330 | 17 | if (is_null($ident)) { |
|
| 331 | 4 | return $this->tableIdentifier; |
|
| 332 | } |
||
| 333 | |||
| 334 | 17 | $this->tableIdentifier = $ident; |
|
| 335 | 17 | } |
|
| 336 | |||
| 337 | 1 | public function saveSearchFilterParams(array $params) |
|
| 338 | { |
||
| 339 | 1 | $this->preferences()->saveSearchFilterParams($this->tableIdentifier(), $params); |
|
| 340 | 1 | } |
|
| 341 | |||
| 342 | 1 | public function getOrderFilterParam($column) |
|
| 343 | { |
||
| 344 | 1 | return $this->preferences()->getOrderFilterParam($this->tableIdentifier(), $column); |
|
| 345 | } |
||
| 346 | |||
| 347 | 1 | public function saveOrderFilterParam($column, $direction) |
|
| 348 | { |
||
| 349 | 1 | $this->preferences()->saveOrderFilterParam($this->tableIdentifier(), $column, $direction); |
|
| 350 | 1 | } |
|
| 351 | |||
| 352 | 1 | public function getPerPageParam() |
|
| 353 | { |
||
| 354 | 1 | return $this->preferences()->getPerPageParam($this->tableIdentifier()); |
|
| 355 | } |
||
| 356 | |||
| 357 | 1 | public function setPerPageParam($perPage) |
|
| 358 | { |
||
| 359 | 1 | $this->preferences()->setPerPageParam($this->tableIdentifier(), $perPage); |
|
| 360 | 1 | } |
|
| 361 | |||
| 362 | public function getCurrentLocale() |
||
| 366 | |||
| 367 | public function saveCurrentLocale($locale) |
||
| 371 | |||
| 372 | 2 | public function getTool($ident) |
|
| 373 | { |
||
| 374 | 2 | if (array_key_exists($ident, $this->toolbar)) { |
|
| 375 | 2 | return $this->toolbar[$ident]; |
|
| 376 | } |
||
| 377 | |||
| 378 | throw new \RuntimeException('Not allowed toolbar'); |
||
| 379 | } |
||
| 380 | |||
| 381 | 2 | public function addTool(ToolInterface $tool) |
|
| 382 | { |
||
| 383 | 2 | $this->toolbar[$tool->identifier()] = $tool; |
|
| 384 | 2 | } |
|
| 385 | |||
| 386 | 2 | public function getTools() |
|
| 387 | { |
||
| 388 | 2 | return $this->toolbar; |
|
| 389 | } |
||
| 390 | |||
| 391 | public function getActiveHeaderToolbarTools() |
||
| 403 | |||
| 404 | public function getActiveBodyToolbarToolsOnTop() |
||
| 408 | |||
| 409 | public function getActiveBodyToolbarToolsOnBottom() |
||
| 413 | |||
| 414 | public function getActiveBodyToolbarTools($position) |
||
| 427 | |||
| 428 | public function getTabErrorsCount($tabTitle, $errors) |
||
| 440 | |||
| 441 | public function locales(array $locales) |
||
| 448 | |||
| 449 | public function getLocales() |
||
| 453 | |||
| 454 | // TODO: move helper to trait or smth |
||
| 455 | private function isAssociativeArray(array $array) |
||
| 463 | |||
| 464 | private function normalizeLocales(array &$locales) |
||
| 473 | |||
| 474 | 17 | public function actions() |
|
| 475 | { |
||
| 476 | 17 | return $this->actions; |
|
| 477 | } |
||
| 478 | |||
| 479 | 1 | public function enableBatchCheckboxes(bool $enabled = true) |
|
| 480 | { |
||
| 481 | 1 | $this->batchCheckboxesEnabled = $enabled; |
|
| 482 | 1 | } |
|
| 483 | |||
| 484 | public function isBatchCheckboxesEnabled() |
||
| 488 | |||
| 489 | public function enableSortableByWeight(string $field) |
||
| 493 | |||
| 494 | public function isSortableByWeight() |
||
| 498 | |||
| 499 | 1 | public function isSortableByWeightActive() |
|
| 500 | { |
||
| 501 | 1 | return $this->preferences()->isSortableByWeightActive($this->tableIdentifier()); |
|
| 502 | } |
||
| 503 | |||
| 504 | public function getSortableWeightFieldName() |
||
| 508 | |||
| 509 | public function setSortableOrderState(bool $active) |
||
| 513 | |||
| 514 | 13 | public function enableSoftDelete(bool $enabled = true) |
|
| 515 | { |
||
| 516 | 13 | $this->softDeleteEnabled = $enabled; |
|
| 517 | 13 | } |
|
| 518 | |||
| 519 | 3 | public function isSoftDeleteEnabled(): bool |
|
| 520 | { |
||
| 521 | 3 | return $this->softDeleteEnabled; |
|
| 522 | } |
||
| 523 | |||
| 524 | private function makeDefaultColumnField($column) |
||
| 528 | } |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.