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 | * Array of queries of the eloquent model. |
||
30 | * |
||
31 | * @var \Illuminate\Support\Collection |
||
32 | */ |
||
33 | protected $queries; |
||
34 | |||
35 | /** |
||
36 | * Sort parameters of the model. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $sort; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $data = []; |
||
46 | |||
47 | /* |
||
48 | * 20 items per page as default. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $perPage = 20; |
||
53 | |||
54 | /** |
||
55 | * If the model use pagination. |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $usePaginate = true; |
||
60 | |||
61 | /** |
||
62 | * The query string variable used to store the per-page. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $perPageName = 'per_page'; |
||
67 | |||
68 | /** |
||
69 | * The query string variable used to store the sort. |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $sortName = '_sort'; |
||
74 | |||
75 | /** |
||
76 | * Collection callback. |
||
77 | * |
||
78 | * @var \Closure |
||
79 | */ |
||
80 | protected $collectionCallback; |
||
81 | |||
82 | /** |
||
83 | * @var Grid |
||
84 | */ |
||
85 | protected $grid; |
||
86 | |||
87 | /** |
||
88 | * @var Relation |
||
89 | */ |
||
90 | protected $relation; |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $eagerLoads = []; |
||
96 | |||
97 | /** |
||
98 | * Create a new grid model instance. |
||
99 | * |
||
100 | * @param EloquentModel $model |
||
101 | */ |
||
102 | public function __construct(EloquentModel $model) |
||
110 | |||
111 | /** |
||
112 | * Don't snake case attributes. |
||
113 | * |
||
114 | * @param EloquentModel $model |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | protected static function doNotSnakeAttributes(EloquentModel $model) |
||
124 | |||
125 | /** |
||
126 | * Get the eloquent model of the grid model. |
||
127 | * |
||
128 | * @return EloquentModel |
||
129 | */ |
||
130 | public function eloquent() |
||
134 | |||
135 | /** |
||
136 | * Enable or disable pagination. |
||
137 | * |
||
138 | * @param bool $use |
||
139 | */ |
||
140 | public function usePaginate($use = true) |
||
144 | |||
145 | /** |
||
146 | * Get the query string variable used to store the per-page. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getPerPageName() |
||
154 | |||
155 | /** |
||
156 | * Set the query string variable used to store the per-page. |
||
157 | * |
||
158 | * @param string $name |
||
159 | * |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function setPerPageName($name) |
||
168 | |||
169 | /** |
||
170 | * Get the query string variable used to store the sort. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getSortName() |
||
178 | |||
179 | /** |
||
180 | * Set the query string variable used to store the sort. |
||
181 | * |
||
182 | * @param string $name |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function setSortName($name) |
||
192 | |||
193 | /** |
||
194 | * Set parent grid instance. |
||
195 | * |
||
196 | * @param Grid $grid |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function setGrid(Grid $grid) |
||
206 | |||
207 | /** |
||
208 | * Get parent gird instance. |
||
209 | * |
||
210 | * @return Grid |
||
211 | */ |
||
212 | public function getGrid() |
||
216 | |||
217 | /** |
||
218 | * @param Relation $relation |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function setRelation(Relation $relation) |
||
228 | |||
229 | /** |
||
230 | * @return Relation |
||
231 | */ |
||
232 | public function getRelation() |
||
236 | |||
237 | /** |
||
238 | * Get constraints. |
||
239 | * |
||
240 | * @return array|bool |
||
241 | */ |
||
242 | public function getConstraints() |
||
252 | |||
253 | /** |
||
254 | * Set collection callback. |
||
255 | * |
||
256 | * @param \Closure $callback |
||
257 | * |
||
258 | * @return $this |
||
259 | */ |
||
260 | public function collection(\Closure $callback = null) |
||
266 | |||
267 | /** |
||
268 | * Build. |
||
269 | * |
||
270 | * @param bool $toArray |
||
271 | * |
||
272 | * @return array|Collection|mixed |
||
273 | */ |
||
274 | public function buildData($toArray = true) |
||
292 | |||
293 | /** |
||
294 | * @param callable $callback |
||
295 | * @param int $count |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function chunk($callback, $count = 100) |
||
315 | |||
316 | /** |
||
317 | * Add conditions to grid model. |
||
318 | * |
||
319 | * @param array $conditions |
||
320 | * |
||
321 | * @return $this |
||
322 | */ |
||
323 | public function addConditions(array $conditions) |
||
331 | |||
332 | /** |
||
333 | * Get table of the model. |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getTable() |
||
341 | |||
342 | /** |
||
343 | * @throws \Exception |
||
344 | * |
||
345 | * @return Collection |
||
346 | */ |
||
347 | protected function get() |
||
376 | |||
377 | /** |
||
378 | * If current page is greater than last page, then redirect to last page. |
||
379 | * |
||
380 | * @param LengthAwarePaginator $paginator |
||
381 | * |
||
382 | * @return void |
||
383 | */ |
||
384 | protected function handleInvalidPage(LengthAwarePaginator $paginator) |
||
394 | |||
395 | /** |
||
396 | * Set the grid paginate. |
||
397 | * |
||
398 | * @return void |
||
399 | */ |
||
400 | protected function setPaginate() |
||
422 | |||
423 | /** |
||
424 | * Resolve perPage for pagination. |
||
425 | * |
||
426 | * @param array|null $paginate |
||
427 | * |
||
428 | * @return array |
||
429 | */ |
||
430 | protected function resolvePerPage($paginate) |
||
452 | |||
453 | /** |
||
454 | * Find query by method name. |
||
455 | * |
||
456 | * @param $method |
||
457 | * |
||
458 | * @return static |
||
459 | */ |
||
460 | protected function findQueryByMethod($method) |
||
466 | |||
467 | /** |
||
468 | * Set the grid sort. |
||
469 | * |
||
470 | * @return void |
||
471 | */ |
||
472 | protected function setSort() |
||
494 | |||
495 | /** |
||
496 | * Set relation sort. |
||
497 | * |
||
498 | * @param string $column |
||
499 | * |
||
500 | * @return void |
||
501 | */ |
||
502 | protected function setRelationSort($column) |
||
527 | |||
528 | /** |
||
529 | * Reset orderBy query. |
||
530 | * |
||
531 | * @return void |
||
532 | */ |
||
533 | public function resetOrderBy() |
||
539 | |||
540 | /** |
||
541 | * Build join parameters for related model. |
||
542 | * |
||
543 | * `HasOne` and `BelongsTo` relation has different join parameters. |
||
544 | * |
||
545 | * @param Relation $relation |
||
546 | * |
||
547 | * @throws \Exception |
||
548 | * |
||
549 | * @return array |
||
550 | */ |
||
551 | protected function joinParameters(Relation $relation) |
||
575 | |||
576 | /** |
||
577 | * @param string $method |
||
578 | * @param array $arguments |
||
579 | * |
||
580 | * @return $this |
||
581 | */ |
||
582 | public function __call($method, $arguments) |
||
591 | |||
592 | /** |
||
593 | * Set the relationships that should be eager loaded. |
||
594 | * |
||
595 | * @param mixed $relations |
||
596 | * |
||
597 | * @return $this|Model |
||
598 | */ |
||
599 | public function with($relations) |
||
627 | |||
628 | /** |
||
629 | * @param $key |
||
630 | * |
||
631 | * @return mixed |
||
632 | */ |
||
633 | public function __get($key) |
||
641 | } |
||
642 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.