Complex classes like Change 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 Change, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Change extends Base |
||
21 | { |
||
22 | /** |
||
23 | * The query param key used when previewing |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | const QUERY_KEY = 'view-change'; |
||
28 | |||
29 | /** |
||
30 | * The attributes that should be cast to native types. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $casts = [ |
||
35 | 'changed' => 'array', |
||
36 | ]; |
||
37 | |||
38 | protected $fillable = [ |
||
39 | 'admin', |
||
40 | 'key', |
||
41 | 'changeable_id', |
||
42 | 'changed', |
||
43 | 'deleted', |
||
44 | |||
45 | 'action', |
||
46 | 'model', |
||
47 | 'model_title', |
||
48 | 'date', |
||
49 | 'title', |
||
50 | 'meta', |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Get the admin associated with the change |
||
55 | * |
||
56 | * @return Illuminate\Database\Eloquent\Relations\Relation |
||
57 | */ |
||
58 | public function admin() |
||
62 | |||
63 | /** |
||
64 | * The polymorphic relation back to the parent model |
||
65 | * |
||
66 | * @var mixed |
||
67 | */ |
||
68 | public function loggable() |
||
72 | |||
73 | /** |
||
74 | * Get the related model, including trashed instances |
||
75 | * |
||
76 | * @return Model |
||
77 | */ |
||
78 | public function changedModel() |
||
82 | |||
83 | /** |
||
84 | * Default ordering by descending time, designed to be overridden |
||
85 | * |
||
86 | * @param Illuminate\Database\Query\Builder $query |
||
87 | * @return Illuminate\Database\Query\Builder |
||
88 | */ |
||
89 | public function scopeOrdered($query) |
||
93 | |||
94 | /** |
||
95 | * Don't log changes |
||
96 | * |
||
97 | * @param string $action |
||
98 | * @return boolean |
||
99 | */ |
||
100 | public function shouldLogChange($action) |
||
104 | |||
105 | /** |
||
106 | * A convenience method for saving a change instance |
||
107 | * |
||
108 | * @param Model $model The model being touched |
||
109 | * @param string $action Generally a CRUD verb: "created", "updated", "deleted" |
||
110 | * @param User $admin The admin acting on the record |
||
111 | * @return static|void |
||
112 | */ |
||
113 | public static function log(Model $model, $action, User $admin = null) |
||
138 | |||
139 | /** |
||
140 | * Don't log changes when the only thing that changed was the published |
||
141 | * state or updated timestamp. We check if there are any attributes |
||
142 | * besides these that changed. |
||
143 | * |
||
144 | * @param Model $model The model being touched |
||
145 | * @param string $action |
||
146 | * @return boolean |
||
147 | */ |
||
148 | static private function shouldWriteChange(Model $model, $action) |
||
157 | |||
158 | /** |
||
159 | * Get the changes attributes |
||
160 | * |
||
161 | * @param Model $model The model being touched |
||
162 | * @param string $action |
||
163 | * @return array|null |
||
164 | */ |
||
165 | static private function getChanged(Model $model, $action) |
||
173 | |||
174 | /** |
||
175 | * Create a change entry |
||
176 | * |
||
177 | * @param Model $model Th |
||
178 | * @param string $action |
||
179 | * @param User $admin |
||
180 | */ |
||
181 | static protected function createLog( |
||
198 | |||
199 | /** |
||
200 | * Get the title of the model |
||
201 | * |
||
202 | * @param Model $model |
||
203 | * @return string |
||
204 | */ |
||
205 | static protected function getModelTitle(Model $model) |
||
210 | |||
211 | /** |
||
212 | * Get the admin id |
||
213 | * |
||
214 | * @param User $admin |
||
215 | * @return integer |
||
216 | */ |
||
217 | static protected function getAdminId(User $admin = null) |
||
224 | |||
225 | /** |
||
226 | * Log changes to publishing state. The initial publish should be logged |
||
227 | * but not an initil unpublished state. |
||
228 | * |
||
229 | * @param Model $model |
||
230 | * @param string $action |
||
231 | * @param User $admin |
||
232 | * @return void |
||
233 | */ |
||
234 | static public function logPublishingChange( |
||
247 | |||
248 | /** |
||
249 | * Return a list of all the actions currently being used as a hash for use |
||
250 | * in a select menu |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | public static function getActions() |
||
263 | |||
264 | /** |
||
265 | * Return a list of all the admins as a hash for use in a select menu |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | public static function getAdmins() |
||
273 | |||
274 | /** |
||
275 | * Format the the activity like a sentence |
||
276 | * |
||
277 | * @return string HTML |
||
278 | */ |
||
279 | public function getAdminTitleHtmlAttribute() |
||
291 | |||
292 | /** |
||
293 | * Get the admin name and link |
||
294 | * |
||
295 | * @return string HTML |
||
296 | */ |
||
297 | public function getAdminLinkAttribute() |
||
309 | |||
310 | /** |
||
311 | * Format the activity as a colored label |
||
312 | * |
||
313 | * @return string HTML |
||
314 | */ |
||
315 | public function getActionLabelAttribute() |
||
332 | |||
333 | /** |
||
334 | * Format the model name by translating it through the controllers's defined |
||
335 | * title |
||
336 | * |
||
337 | * @return string HTML |
||
338 | */ |
||
339 | public function getModelNameHtmlAttribute() |
||
361 | |||
362 | /** |
||
363 | * Get the title of the model. Perhaps in the future there will be more smarts |
||
364 | * here, like generating a link to the edit view |
||
365 | * |
||
366 | * @return string HTML |
||
367 | */ |
||
368 | public function getLinkedTitleAttribute() |
||
378 | |||
379 | /** |
||
380 | * Get the date of the change |
||
381 | * |
||
382 | * @return string HTML |
||
383 | */ |
||
384 | public function getDateAttribute() |
||
398 | |||
399 | /** |
||
400 | * Get the human readable date |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | public function getHumanDateAttribute() |
||
417 | |||
418 | /** |
||
419 | * Customize the action links |
||
420 | * |
||
421 | * @param array $data The data passed to a listing view |
||
422 | * @return array |
||
423 | */ |
||
424 | public function makeAdminActions($data) |
||
434 | |||
435 | /** |
||
436 | * Make the preview filter icon |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | public function getFilterActionAttribute() |
||
451 | |||
452 | /** |
||
453 | * Make a link to filter the result set |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | public function filterUrl($query) |
||
461 | |||
462 | /** |
||
463 | * Make the changes icon |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | public function getChangesActionAttribute() |
||
489 | |||
490 | /** |
||
491 | * Make link to preview a version as long as the model has a URI and the |
||
492 | * action wasn't a delete action. |
||
493 | * |
||
494 | * @return string |
||
495 | */ |
||
496 | public function getPreviewActionAttribute() |
||
513 | |||
514 | /** |
||
515 | * Make the preview URL for a the model |
||
516 | * |
||
517 | * @return string |
||
518 | */ |
||
519 | public function getPreviewUrlAttribute() |
||
529 | |||
530 | /** |
||
531 | * Get just the attributes that should be displayed in the admin modal. |
||
532 | * |
||
533 | * @return array |
||
534 | */ |
||
535 | public function attributesForModal() |
||
553 | } |
||
554 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.