Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Model |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Eloquent model instance of the grid model. |
||
| 23 | * |
||
| 24 | * @var EloquentModel |
||
| 25 | */ |
||
| 26 | protected $model; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var EloquentModel |
||
| 30 | */ |
||
| 31 | protected $originalModel; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Array of queries of the eloquent model. |
||
| 35 | * |
||
| 36 | * @var \Illuminate\Support\Collection |
||
| 37 | */ |
||
| 38 | protected $queries; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Sort parameters of the model. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $sort; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $data = []; |
||
| 51 | |||
| 52 | /* |
||
| 53 | * 20 items per page as default. |
||
| 54 | * |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | protected $perPage = 20; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * If the model use pagination. |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $usePaginate = true; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The query string variable used to store the per-page. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $perPageName = 'per_page'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The query string variable used to store the sort. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $sortName = '_sort'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Collection callback. |
||
| 82 | * |
||
| 83 | * @var \Closure |
||
| 84 | */ |
||
| 85 | protected $collectionCallback; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var Grid |
||
| 89 | */ |
||
| 90 | protected $grid; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Relation |
||
| 94 | */ |
||
| 95 | protected $relation; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $eagerLoads = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Create a new grid model instance. |
||
| 104 | * |
||
| 105 | * @param EloquentModel $model |
||
| 106 | */ |
||
| 107 | public function __construct(EloquentModel $model) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Don't snake case attributes. |
||
| 120 | * |
||
| 121 | * @param EloquentModel $model |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | protected static function doNotSnakeAttributes(EloquentModel $model) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return EloquentModel |
||
| 134 | */ |
||
| 135 | public function getOriginalModel() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get the eloquent model of the grid model. |
||
| 142 | * |
||
| 143 | * @return EloquentModel |
||
| 144 | */ |
||
| 145 | public function eloquent() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Enable or disable pagination. |
||
| 152 | * |
||
| 153 | * @param bool $use |
||
| 154 | */ |
||
| 155 | public function usePaginate($use = true) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the query string variable used to store the per-page. |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function getPerPageName() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the query string variable used to store the per-page. |
||
| 172 | * |
||
| 173 | * @param string $name |
||
| 174 | * |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function setPerPageName($name) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get per-page number. |
||
| 186 | * |
||
| 187 | * @return int |
||
| 188 | */ |
||
| 189 | public function getPerPage() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get the query string variable used to store the sort. |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getSortName() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Set the query string variable used to store the sort. |
||
| 206 | * |
||
| 207 | * @param string $name |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function setSortName($name) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Set parent grid instance. |
||
| 220 | * |
||
| 221 | * @param Grid $grid |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public function setGrid(Grid $grid) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get parent gird instance. |
||
| 234 | * |
||
| 235 | * @return Grid |
||
| 236 | */ |
||
| 237 | public function getGrid() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param Relation $relation |
||
| 244 | * |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | public function setRelation(Relation $relation) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return Relation |
||
| 256 | */ |
||
| 257 | public function getRelation() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get constraints. |
||
| 264 | * |
||
| 265 | * @return array|bool |
||
| 266 | */ |
||
| 267 | public function getConstraints() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set collection callback. |
||
| 280 | * |
||
| 281 | * @param \Closure $callback |
||
| 282 | * |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function collection(\Closure $callback = null) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Build. |
||
| 294 | * |
||
| 295 | * @param bool $toArray |
||
| 296 | * |
||
| 297 | * @return array|Collection|mixed |
||
| 298 | */ |
||
| 299 | public function buildData($toArray = true) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param callable $callback |
||
| 320 | * @param int $count |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | public function chunk($callback, $count = 100) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Add conditions to grid model. |
||
| 343 | * |
||
| 344 | * @param array $conditions |
||
| 345 | * |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function addConditions(array $conditions) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get table of the model. |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function getTable() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @throws \Exception |
||
| 369 | * |
||
| 370 | * @return Collection |
||
| 371 | */ |
||
| 372 | protected function get() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return \Illuminate\Database\Eloquent\Builder|EloquentModel |
||
| 404 | */ |
||
| 405 | public function getQueryBuilder() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * If current page is greater than last page, then redirect to last page. |
||
| 426 | * |
||
| 427 | * @param LengthAwarePaginator $paginator |
||
| 428 | * |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | protected function handleInvalidPage(LengthAwarePaginator $paginator) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Set the grid paginate. |
||
| 444 | * |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | protected function setPaginate() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Resolve perPage for pagination. |
||
| 472 | * |
||
| 473 | * @param array|null $paginate |
||
| 474 | * |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | protected function resolvePerPage($paginate) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Find query by method name. |
||
| 502 | * |
||
| 503 | * @param $method |
||
| 504 | * |
||
| 505 | * @return static |
||
| 506 | */ |
||
| 507 | protected function findQueryByMethod($method) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Set the grid sort. |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | protected function setSort() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Set relation sort. |
||
| 555 | * |
||
| 556 | * @param string $column |
||
| 557 | * |
||
| 558 | * @return void |
||
| 559 | */ |
||
| 560 | protected function setRelationSort($column) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Reset orderBy query. |
||
| 593 | * |
||
| 594 | * @return void |
||
| 595 | */ |
||
| 596 | public function resetOrderBy() |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Build join parameters for related model. |
||
| 605 | * |
||
| 606 | * `HasOne` and `BelongsTo` relation has different join parameters. |
||
| 607 | * |
||
| 608 | * @param Relation $relation |
||
| 609 | * |
||
| 610 | * @throws \Exception |
||
| 611 | * |
||
| 612 | * @return array |
||
| 613 | */ |
||
| 614 | protected function joinParameters(Relation $relation) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * @param string $method |
||
| 643 | * @param array $arguments |
||
| 644 | * |
||
| 645 | * @return $this |
||
| 646 | */ |
||
| 647 | public function __call($method, $arguments) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Set the relationships that should be eager loaded. |
||
| 659 | * |
||
| 660 | * @param mixed $relations |
||
| 661 | * |
||
| 662 | * @return $this|Model |
||
| 663 | */ |
||
| 664 | public function with($relations) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @param $key |
||
| 695 | * |
||
| 696 | * @return mixed |
||
| 697 | */ |
||
| 698 | public function __get($key) |
||
| 706 | } |
||
| 707 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..