Complex classes like UserModel 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 UserModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Anomaly\UsersModule\User; |
||
| 19 | class UserModel extends UsersUsersEntryModel implements UserInterface, \Illuminate\Contracts\Auth\Authenticatable |
||
|
|
|||
| 20 | { |
||
| 21 | |||
| 22 | use Authenticatable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The eager loaded relationships. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $with = [ |
||
| 30 | 'roles' |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The hidden attributes. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $hidden = [ |
||
| 39 | 'password' |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get the email. |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | public function getEmail() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the username. |
||
| 54 | * |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | public function getUsername() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get the display name. |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getDisplayName() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get the first name. |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function getFirstName() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the last name. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getLastName() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set the password. |
||
| 94 | * |
||
| 95 | * @param $password |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | public function setPassword($password) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get related roles. |
||
| 107 | * |
||
| 108 | * @return EntryCollection |
||
| 109 | */ |
||
| 110 | public function getRoles() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Return whether a user is in a role. |
||
| 117 | * |
||
| 118 | * @param $role |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function hasRole($role) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Return whether a user is in |
||
| 143 | * any of the provided roles. |
||
| 144 | * |
||
| 145 | * @param $roles |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function hasAnyRole($roles) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return whether the user |
||
| 169 | * is an admin or not. |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function isAdmin() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the permissions. |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function getPermissions() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set the permissions. |
||
| 197 | * |
||
| 198 | * @param array $permissions |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setPermissions(array $permissions) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Return whether a user or it's roles has a permission. |
||
| 210 | * |
||
| 211 | * @param $permission |
||
| 212 | * @param bool $checkRoles |
||
| 213 | * @return mixed |
||
| 214 | */ |
||
| 215 | public function hasPermission($permission, $checkRoles = true) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return whether a user has any of provided permission. |
||
| 240 | * |
||
| 241 | * @param $permissions |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function hasAnyPermission(array $permissions) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Hash the password whenever setting it. |
||
| 257 | * |
||
| 258 | * @param $password |
||
| 259 | */ |
||
| 260 | public function setPasswordAttribute($password) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Return whether the model is deletable or not. |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function isDeletable() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return the activated flag. |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function isActivated() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Return the enabled flag. |
||
| 297 | * |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | public function isEnabled() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the reset code. |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function getResetCode() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Set the reset code. |
||
| 317 | * |
||
| 318 | * @param $code |
||
| 319 | * @return $this |
||
| 320 | */ |
||
| 321 | public function setResetCode($code) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the activation code. |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function getActivationCode() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set the activation code. |
||
| 340 | * |
||
| 341 | * @param $code |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function setActivationCode($code) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Return the full name. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function name() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Attach a role to the user. |
||
| 363 | * |
||
| 364 | * @param RoleInterface $role |
||
| 365 | */ |
||
| 366 | public function attachRole(RoleInterface $role) |
||
| 370 | } |
||
| 371 |