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 | * Get related roles. |
||
| 94 | * |
||
| 95 | * @return RoleCollection |
||
| 96 | */ |
||
| 97 | public function getRoles() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Return whether a user is in a role. |
||
| 104 | * |
||
| 105 | * @param $role |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | public function hasRole($role) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Return whether a user is in |
||
| 130 | * any of the provided roles. |
||
| 131 | * |
||
| 132 | * @param $roles |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function hasAnyRole($roles) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return whether the user |
||
| 156 | * is an admin or not. |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | public function isAdmin() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get the permissions. |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | public function getPermissions() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Return whether a user or it's roles has a permission. |
||
| 184 | * |
||
| 185 | * @param $permission |
||
| 186 | * @param bool $checkRoles |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | public function hasPermission($permission, $checkRoles = true) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return whether a user has any of provided permission. |
||
| 214 | * |
||
| 215 | * @param $permissions |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public function hasAnyPermission(array $permissions) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Hash the password whenever setting it. |
||
| 231 | * |
||
| 232 | * @param $password |
||
| 233 | */ |
||
| 234 | public function setPasswordAttribute($password) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Return whether the model is deletable or not. |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function isDeletable() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return the activated flag. |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | public function isActivated() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return the enabled flag. |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function isEnabled() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the reset code. |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getResetCode() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the activation code. |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function getActivationCode() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return the full name. |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function name() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Attach a role to the user. |
||
| 311 | * |
||
| 312 | * @param RoleInterface $role |
||
| 313 | */ |
||
| 314 | public function attachRole(RoleInterface $role) |
||
| 318 | } |
||
| 319 |