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 | /** |
||
| 92 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 93 | */ |
||
| 94 | public function trainings() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 101 | */ |
||
| 102 | public function groups() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 109 | */ |
||
| 110 | public function duties() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * If we have a nickname, return 'lastname, nickname' otherwise return 'lastname, firstname'. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getUserFullNameAttribute() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param $query |
||
| 141 | * @param $input |
||
| 142 | */ |
||
| 143 | public function scopeSearchUsers($query, $input) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param $query |
||
| 152 | * |
||
| 153 | * @return mixed |
||
| 154 | */ |
||
| 155 | public function scopeActive($query) |
||
| 159 | |||
| 160 | public function scopeSkipSystem($query) |
||
| 164 | |||
| 165 | public function scopeUserStatus($query) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Store empty values as null in the DB. |
||
| 174 | * |
||
| 175 | * @param string $key |
||
| 176 | * @param mixed $value |
||
| 177 | * |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function setAttribute($key, $value) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param $user |
||
| 191 | * Obtain the Activitylog for the designated user or for all users. |
||
| 192 | * Populate log array values ["comment", "updated_at", "user_fullname"] |
||
| 193 | * |
||
| 194 | * @return Log collection |
||
| 195 | */ |
||
| 196 | public function getUserLog($user = null) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param Array1 Array2 |
||
| 232 | * As array_diff function only checks one dimension of a n-dimensional array. |
||
| 233 | * arrayRecursiveDiff will compare n-dimensional. |
||
| 234 | * Will insure compared objects are arrays. |
||
| 235 | * |
||
| 236 | * @return DiffsArray |
||
| 237 | */ |
||
| 238 | private function arrayRecursiveDiff($aArray1, $aArray2) |
||
| 263 | |||
| 264 | public function getDestroyDate($status) |
||
| 276 | } |
||
| 277 |