Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class User extends Authenticatable |
||
15 | { |
||
16 | use Notifiable; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $table = 'users'; |
||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | public $timestamps = true; |
||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $fillable = ['username', 'emp_num', 'first_name', 'nickname', 'last_name', |
||
30 | 'email', 'phone', 'status', 'clearance', 'elig_date', 'inv', 'inv_close', 'destroyed_date', |
||
31 | 'supervisor_id', 'access_level', 'password', 'separated_date', 'cont_eval', 'cont_eval_date', ]; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $hidden = ['username', 'password', 'remember_token']; |
||
36 | |||
37 | /* |
||
38 | * @var array $logAttributes defines will log the changed attributes |
||
39 | */ |
||
40 | use LogsActivity; |
||
41 | protected static $logAttributes = ['username', 'emp_num', 'first_name', 'nickname', |
||
42 | 'last_name', 'email', 'phone', 'jpas_name', 'status', 'clearance', |
||
43 | 'elig_date', 'inv', 'inv_close', 'destroyed_date', 'role', 'supervisor_id', 'access_level', |
||
44 | 'last_logon', 'ip', 'cont_eval', 'cont_eval_date', ]; |
||
45 | |||
46 | /** |
||
47 | * make destroyed_date a Carbon instance. |
||
48 | */ |
||
49 | protected $dates = ['destroyed_date']; |
||
50 | |||
51 | public function supervisor() |
||
55 | |||
56 | public function subordinates() |
||
60 | |||
61 | public function attachments() |
||
65 | |||
66 | public function notes() |
||
70 | |||
71 | public function travels() |
||
75 | |||
76 | public function visits() |
||
80 | |||
81 | public function assignedTrainings() |
||
85 | |||
86 | public function trainingUsers() |
||
90 | |||
91 | public function accessTokens() |
||
95 | |||
96 | /** |
||
97 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
98 | */ |
||
99 | public function trainings() |
||
103 | |||
104 | /** |
||
105 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
106 | */ |
||
107 | public function groups() |
||
111 | |||
112 | /** |
||
113 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
114 | */ |
||
115 | public function duties() |
||
119 | |||
120 | /** |
||
121 | * If we have a nickname, return 'lastname, nickname' otherwise return 'lastname, firstname'. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getUserFullNameAttribute() |
||
143 | |||
144 | /** |
||
145 | * @param $query |
||
146 | * @param $input |
||
147 | */ |
||
148 | public function scopeSearchUsers($query, $input) |
||
154 | |||
155 | /** |
||
156 | * @param $query |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public function scopeActive($query) |
||
164 | |||
165 | public function scopeSkipSystem($query) |
||
169 | |||
170 | |||
171 | /** |
||
172 | * @param string $query |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | public function scopeUserStatus($query) |
||
182 | |||
183 | /** |
||
184 | * Store empty values as null in the DB. |
||
185 | * |
||
186 | * @param string $key |
||
187 | * @param mixed $value |
||
188 | * |
||
189 | * @return $this |
||
190 | */ |
||
191 | View Code Duplication | public function setAttribute($key, $value) |
|
199 | |||
200 | /** |
||
201 | * @param $user |
||
202 | * Obtain the Activitylog for the designated user or for all users. |
||
203 | * Populate log array values ["comment", "updated_at", "user_fullname"] |
||
204 | * |
||
205 | * @return Log collection |
||
206 | */ |
||
207 | public function getUserLog($user = null) |
||
240 | |||
241 | /** |
||
242 | * @param Array1 Array2 |
||
243 | * As array_diff function only checks one dimension of a n-dimensional array. |
||
244 | * arrayRecursiveDiff will compare n-dimensional. |
||
245 | * Will insure compared objects are arrays. |
||
246 | * |
||
247 | * @return DiffsArray |
||
248 | */ |
||
249 | private function arrayRecursiveDiff($aArray1, $aArray2) |
||
274 | |||
275 | public function getDestroyDate($status) |
||
287 | } |
||
288 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.