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 | * Get the eloquent model of the grid model. |
||
134 | * |
||
135 | * @return EloquentModel |
||
136 | */ |
||
137 | public function eloquent() |
||
141 | |||
142 | /** |
||
143 | * Enable or disable pagination. |
||
144 | * |
||
145 | * @param bool $use |
||
146 | */ |
||
147 | public function usePaginate($use = true) |
||
151 | |||
152 | /** |
||
153 | * Get the query string variable used to store the per-page. |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getPerPageName() |
||
161 | |||
162 | /** |
||
163 | * Set the query string variable used to store the per-page. |
||
164 | * |
||
165 | * @param string $name |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function setPerPageName($name) |
||
175 | |||
176 | /** |
||
177 | * Get the query string variable used to store the sort. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getSortName() |
||
185 | |||
186 | /** |
||
187 | * Set the query string variable used to store the sort. |
||
188 | * |
||
189 | * @param string $name |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function setSortName($name) |
||
199 | |||
200 | /** |
||
201 | * Set parent grid instance. |
||
202 | * |
||
203 | * @param Grid $grid |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function setGrid(Grid $grid) |
||
213 | |||
214 | /** |
||
215 | * Get parent gird instance. |
||
216 | * |
||
217 | * @return Grid |
||
218 | */ |
||
219 | public function getGrid() |
||
223 | |||
224 | /** |
||
225 | * @param Relation $relation |
||
226 | * |
||
227 | * @return $this |
||
228 | */ |
||
229 | public function setRelation(Relation $relation) |
||
235 | |||
236 | /** |
||
237 | * @return Relation |
||
238 | */ |
||
239 | public function getRelation() |
||
243 | |||
244 | /** |
||
245 | * Get constraints. |
||
246 | * |
||
247 | * @return array|bool |
||
248 | */ |
||
249 | public function getConstraints() |
||
259 | |||
260 | /** |
||
261 | * Set collection callback. |
||
262 | * |
||
263 | * @param \Closure $callback |
||
264 | * |
||
265 | * @return $this |
||
266 | */ |
||
267 | public function collection(\Closure $callback = null) |
||
273 | |||
274 | /** |
||
275 | * Build. |
||
276 | * |
||
277 | * @param bool $toArray |
||
278 | * |
||
279 | * @return array|Collection|mixed |
||
280 | */ |
||
281 | public function buildData($toArray = true) |
||
299 | |||
300 | /** |
||
301 | * @param callable $callback |
||
302 | * @param int $count |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function chunk($callback, $count = 100) |
||
322 | |||
323 | /** |
||
324 | * Add conditions to grid model. |
||
325 | * |
||
326 | * @param array $conditions |
||
327 | * |
||
328 | * @return $this |
||
329 | */ |
||
330 | public function addConditions(array $conditions) |
||
338 | |||
339 | /** |
||
340 | * Get table of the model. |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | public function getTable() |
||
348 | |||
349 | /** |
||
350 | * @throws \Exception |
||
351 | * |
||
352 | * @return Collection |
||
353 | */ |
||
354 | protected function get() |
||
384 | |||
385 | /** |
||
386 | * @return \Illuminate\Database\Eloquent\Builder|EloquentModel |
||
387 | */ |
||
388 | public function getQueryBuilder() |
||
406 | |||
407 | /** |
||
408 | * If current page is greater than last page, then redirect to last page. |
||
409 | * |
||
410 | * @param LengthAwarePaginator $paginator |
||
411 | * |
||
412 | * @return void |
||
413 | */ |
||
414 | protected function handleInvalidPage(LengthAwarePaginator $paginator) |
||
424 | |||
425 | /** |
||
426 | * Set the grid paginate. |
||
427 | * |
||
428 | * @return void |
||
429 | */ |
||
430 | protected function setPaginate() |
||
452 | |||
453 | /** |
||
454 | * Resolve perPage for pagination. |
||
455 | * |
||
456 | * @param array|null $paginate |
||
457 | * |
||
458 | * @return array |
||
459 | */ |
||
460 | protected function resolvePerPage($paginate) |
||
482 | |||
483 | /** |
||
484 | * Find query by method name. |
||
485 | * |
||
486 | * @param $method |
||
487 | * |
||
488 | * @return static |
||
489 | */ |
||
490 | protected function findQueryByMethod($method) |
||
496 | |||
497 | /** |
||
498 | * Set the grid sort. |
||
499 | * |
||
500 | * @return void |
||
501 | */ |
||
502 | protected function setSort() |
||
524 | |||
525 | /** |
||
526 | * Set relation sort. |
||
527 | * |
||
528 | * @param string $column |
||
529 | * |
||
530 | * @return void |
||
531 | */ |
||
532 | protected function setRelationSort($column) |
||
562 | |||
563 | /** |
||
564 | * Reset orderBy query. |
||
565 | * |
||
566 | * @return void |
||
567 | */ |
||
568 | public function resetOrderBy() |
||
574 | |||
575 | /** |
||
576 | * Build join parameters for related model. |
||
577 | * |
||
578 | * `HasOne` and `BelongsTo` relation has different join parameters. |
||
579 | * |
||
580 | * @param Relation $relation |
||
581 | * |
||
582 | * @throws \Exception |
||
583 | * |
||
584 | * @return array |
||
585 | */ |
||
586 | protected function joinParameters(Relation $relation) |
||
610 | |||
611 | /** |
||
612 | * @param string $method |
||
613 | * @param array $arguments |
||
614 | * |
||
615 | * @return $this |
||
616 | */ |
||
617 | public function __call($method, $arguments) |
||
626 | |||
627 | /** |
||
628 | * Set the relationships that should be eager loaded. |
||
629 | * |
||
630 | * @param mixed $relations |
||
631 | * |
||
632 | * @return $this|Model |
||
633 | */ |
||
634 | public function with($relations) |
||
662 | |||
663 | /** |
||
664 | * @param $key |
||
665 | * |
||
666 | * @return mixed |
||
667 | */ |
||
668 | public function __get($key) |
||
676 | } |
||
677 |
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..