Complex classes like HasRoleAndPermission 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 HasRoleAndPermission, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | trait HasRoleAndPermission |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Property for caching roles. |
||
| 18 | * |
||
| 19 | * @var Collection|null |
||
| 20 | */ |
||
| 21 | protected $roles; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Property for caching permissions. |
||
| 25 | * |
||
| 26 | * @var Collection|null |
||
| 27 | */ |
||
| 28 | protected $permissions; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * User belongs to many roles. |
||
| 32 | * |
||
| 33 | * @return BelongsToMany |
||
| 34 | */ |
||
| 35 | public function roles() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get all roles as collection. |
||
| 42 | * |
||
| 43 | * @return Collection |
||
| 44 | */ |
||
| 45 | public function getRoles() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Check if the user has a role or roles. |
||
| 52 | * |
||
| 53 | * @param int|string|array $role |
||
| 54 | * @param bool $all |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | public function hasRole($role, $all = false) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Check if the user has at least one of the given roles. |
||
| 72 | * |
||
| 73 | * @param int|string|array $role |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | public function hasOneRole($role) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Check if the user has all roles. |
||
| 89 | * |
||
| 90 | * @param int|string|array $role |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | public function hasAllRoles($role) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Check if the user has role. |
||
| 106 | * |
||
| 107 | * @param int|string $role |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function checkRole($role) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Attach role to a user. |
||
| 119 | * |
||
| 120 | * @param int|Role $role |
||
| 121 | * @return null|bool |
||
| 122 | */ |
||
| 123 | public function attachRole($role) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Detach role from a user. |
||
| 134 | * |
||
| 135 | * @param int|Role $role |
||
| 136 | * @return int |
||
| 137 | */ |
||
| 138 | public function detachRole($role) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Detach all roles from a user. |
||
| 147 | * |
||
| 148 | * @return int |
||
| 149 | */ |
||
| 150 | public function detachAllRoles() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Sync roles for a user. |
||
| 159 | * |
||
| 160 | * @param array|\Ultraware\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public function syncRoles($roles) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get role level of a user. |
||
| 172 | * |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | public function level() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get all permissions from roles. |
||
| 182 | * |
||
| 183 | * @return Builder |
||
| 184 | */ |
||
| 185 | public function rolePermissions() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * User belongs to many permissions. |
||
| 204 | * |
||
| 205 | * @return BelongsToMany |
||
| 206 | */ |
||
| 207 | public function userPermissions() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get all permissions as collection. |
||
| 214 | * |
||
| 215 | * @return Collection |
||
| 216 | */ |
||
| 217 | public function getPermissions() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check if the user has a permission or permissions. |
||
| 224 | * |
||
| 225 | * @param int|string|array $permission |
||
| 226 | * @param bool $all |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function hasPermission($permission, $all = false) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Check if the user has at least one of the given permissions. |
||
| 244 | * |
||
| 245 | * @param int|string|array $permission |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | public function hasOnePermission($permission) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Check if the user has all permissions. |
||
| 261 | * |
||
| 262 | * @param int|string|array $permission |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public function hasAllPermissions($permission) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Check if the user has a permission. |
||
| 278 | * |
||
| 279 | * @param int|string $permission |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | public function checkPermission($permission) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Check if the user is allowed to manipulate with entity. |
||
| 291 | * |
||
| 292 | * @param string $providedPermission |
||
| 293 | * @param Model $entity |
||
| 294 | * @param bool $owner |
||
| 295 | * @param string $ownerColumn |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function allowed($providedPermission, Model $entity, $owner = true, $ownerColumn = 'user_id') |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Check if the user is allowed to manipulate with provided entity. |
||
| 313 | * |
||
| 314 | * @param string $providedPermission |
||
| 315 | * @param Model $entity |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | protected function isAllowed($providedPermission, Model $entity) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Attach permission to a user. |
||
| 333 | * |
||
| 334 | * @param int|Permission $permission |
||
| 335 | * @return null|bool |
||
| 336 | */ |
||
| 337 | public function attachPermission($permission) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Detach permission from a user. |
||
| 348 | * |
||
| 349 | * @param int|Permission $permission |
||
| 350 | * @return int |
||
| 351 | */ |
||
| 352 | public function detachPermission($permission) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Detach all permissions from a user. |
||
| 361 | * |
||
| 362 | * @return int |
||
| 363 | */ |
||
| 364 | public function detachAllPermissions() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Sync permissions for a user. |
||
| 373 | * |
||
| 374 | * @param array|\Ultraware\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | public function syncPermissions($permissions) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Check if pretend option is enabled. |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | private function isPretendEnabled() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Allows to pretend or simulate package behavior. |
||
| 396 | * |
||
| 397 | * @param string $option |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | private function pretend($option) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get an array from argument. |
||
| 407 | * |
||
| 408 | * @param int|string|array $argument |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | private function getArrayFrom($argument) |
||
| 415 | |||
| 416 | public function callMagic($method, $parameters) |
||
| 428 | |||
| 429 | public function __call($method, $parameters) |
||
| 433 | } |
||
| 434 |