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 |
||
15 | class Model |
||
16 | { |
||
17 | /** |
||
18 | * Eloquent model instance of the grid model. |
||
19 | * |
||
20 | * @var EloquentModel |
||
21 | */ |
||
22 | protected $model; |
||
23 | |||
24 | /** |
||
25 | * Array of queries of the eloquent model. |
||
26 | * |
||
27 | * @var \Illuminate\Support\Collection |
||
28 | */ |
||
29 | protected $queries; |
||
30 | |||
31 | /** |
||
32 | * Sort parameters of the model. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $sort; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $data = []; |
||
42 | |||
43 | /* |
||
44 | * 20 items per page as default. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $perPage = 20; |
||
49 | |||
50 | /** |
||
51 | * If the model use pagination. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $usePaginate = true; |
||
56 | |||
57 | /** |
||
58 | * The query string variable used to store the per-page. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $perPageName = 'per_page'; |
||
63 | |||
64 | /** |
||
65 | * The query string variable used to store the sort. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $sortName = '_sort'; |
||
70 | |||
71 | /** |
||
72 | * Collection callback. |
||
73 | * |
||
74 | * @var \Closure |
||
75 | */ |
||
76 | protected $collectionCallback; |
||
77 | |||
78 | /** |
||
79 | * Create a new grid model instance. |
||
80 | * |
||
81 | * @param EloquentModel $model |
||
82 | */ |
||
83 | public function __construct(EloquentModel $model) |
||
91 | |||
92 | /** |
||
93 | * Don't snake case attributes. |
||
94 | * |
||
95 | * @param EloquentModel $model |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | protected static function doNotSnakeAttributes(EloquentModel $model) |
||
105 | |||
106 | /** |
||
107 | * Get the eloquent model of the grid model. |
||
108 | * |
||
109 | * @return EloquentModel |
||
110 | */ |
||
111 | public function eloquent() |
||
115 | |||
116 | /** |
||
117 | * Enable or disable pagination. |
||
118 | * |
||
119 | * @param bool $use |
||
120 | */ |
||
121 | public function usePaginate($use = true) |
||
125 | |||
126 | /** |
||
127 | * Get the query string variable used to store the per-page. |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getPerPageName() |
||
135 | |||
136 | /** |
||
137 | * Set the query string variable used to store the per-page. |
||
138 | * |
||
139 | * @param string $name |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function setPerPageName($name) |
||
149 | |||
150 | /** |
||
151 | * Get the query string variable used to store the sort. |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function getSortName() |
||
159 | |||
160 | /** |
||
161 | * Set the query string variable used to store the sort. |
||
162 | * |
||
163 | * @param string $name |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function setSortName($name) |
||
173 | |||
174 | /** |
||
175 | * Set collection callback. |
||
176 | * |
||
177 | * @param \Closure $callback |
||
178 | * |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function collection(\Closure $callback = null) |
||
187 | |||
188 | /** |
||
189 | * Build. |
||
190 | * |
||
191 | * @param bool $toArray |
||
192 | * |
||
193 | * @return array|Collection|mixed |
||
194 | */ |
||
195 | public function buildData($toArray = true) |
||
213 | |||
214 | /** |
||
215 | * @param callable $callback |
||
216 | * @param int $count |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function chunk($callback, $count = 100) |
||
236 | |||
237 | /** |
||
238 | * Add conditions to grid model. |
||
239 | * |
||
240 | * @param array $conditions |
||
241 | * |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function addConditions(array $conditions) |
||
252 | |||
253 | /** |
||
254 | * Get table of the model. |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function getTable() |
||
262 | |||
263 | /** |
||
264 | * @throws \Exception |
||
265 | * |
||
266 | * @return Collection |
||
267 | */ |
||
268 | protected function get() |
||
293 | |||
294 | /** |
||
295 | * If current page is greater than last page, then redirect to last page. |
||
296 | * |
||
297 | * @param LengthAwarePaginator $paginator |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | protected function handleInvalidPage(LengthAwarePaginator $paginator) |
||
311 | |||
312 | /** |
||
313 | * Set the grid paginate. |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | protected function setPaginate() |
||
339 | |||
340 | /** |
||
341 | * Resolve perPage for pagination. |
||
342 | * |
||
343 | * @param array|null $paginate |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | protected function resolvePerPage($paginate) |
||
365 | |||
366 | /** |
||
367 | * Find query by method name. |
||
368 | * |
||
369 | * @param $method |
||
370 | * |
||
371 | * @return static |
||
372 | */ |
||
373 | protected function findQueryByMethod($method) |
||
379 | |||
380 | /** |
||
381 | * Set the grid sort. |
||
382 | * |
||
383 | * @return void |
||
384 | */ |
||
385 | protected function setSort() |
||
407 | |||
408 | /** |
||
409 | * Set relation sort. |
||
410 | * |
||
411 | * @param string $column |
||
412 | * |
||
413 | * @return void |
||
414 | */ |
||
415 | protected function setRelationSort($column) |
||
440 | |||
441 | /** |
||
442 | * Reset orderBy query. |
||
443 | * |
||
444 | * @return void |
||
445 | */ |
||
446 | public function resetOrderBy() |
||
452 | |||
453 | /** |
||
454 | * Build join parameters for related model. |
||
455 | * |
||
456 | * `HasOne` and `BelongsTo` relation has different join parameters. |
||
457 | * |
||
458 | * @param Relation $relation |
||
459 | * |
||
460 | * @throws \Exception |
||
461 | * |
||
462 | * @return array |
||
463 | */ |
||
464 | protected function joinParameters(Relation $relation) |
||
488 | |||
489 | /** |
||
490 | * @param string $method |
||
491 | * @param array $arguments |
||
492 | * |
||
493 | * @return $this |
||
494 | */ |
||
495 | public function __call($method, $arguments) |
||
504 | |||
505 | /** |
||
506 | * @param $key |
||
507 | * |
||
508 | * @return mixed |
||
509 | */ |
||
510 | public function __get($key) |
||
518 | } |
||
519 |
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..