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 HasPermissions 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 HasPermissions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | trait HasPermissions |
||
| 18 | { |
||
| 19 | private $permissionClass; |
||
| 20 | private $roleClass; |
||
| 21 | |||
| 22 | public static function bootHasPermissions() |
||
| 32 | |||
| 33 | public function getPermissionClass() |
||
| 41 | |||
| 42 | public function getRoleClass() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * A model may have multiple direct permissions. |
||
| 53 | */ |
||
| 54 | public function permissions(): MorphToMany |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Scope the model query to certain permissions only. |
||
| 67 | * |
||
| 68 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 69 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 70 | * |
||
| 71 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 72 | */ |
||
| 73 | View Code Duplication | public function scopePermission(Builder $query, $permissions): Builder |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Scope the model query without certain permissions. |
||
| 93 | * |
||
| 94 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 95 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 96 | * |
||
| 97 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function scopeWithoutPermission(Builder $query, $permissions): Builder |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | protected function convertToPermissionModels($permissions): array |
||
| 138 | |||
| 139 | protected function convertPermissionsToRoleIds($permissions) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Determine if the model may perform the given permission. |
||
| 150 | * |
||
| 151 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 152 | * @param string|null $guardName |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | * @throws PermissionDoesNotExist |
||
| 156 | */ |
||
| 157 | public function hasPermissionTo($permission, $guardName = null): bool |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Validates a wildcard permission against all permissions of a user. |
||
| 188 | * |
||
| 189 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 190 | * @param string|null $guardName |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | protected function hasWildcardPermission($permission, $guardName = null): bool |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @deprecated since 2.35.0 |
||
| 227 | * @alias of hasPermissionTo() |
||
| 228 | */ |
||
| 229 | public function hasUncachedPermissionTo($permission, $guardName = null): bool |
||
| 233 | |||
| 234 | /** |
||
| 235 | * An alias to hasPermissionTo(), but avoids throwing an exception. |
||
| 236 | * |
||
| 237 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 238 | * @param string|null $guardName |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public function checkPermissionTo($permission, $guardName = null): bool |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Determine if the model has any of the given permissions. |
||
| 253 | * |
||
| 254 | * @param array ...$permissions |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | * @throws \Exception |
||
| 258 | */ |
||
| 259 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Determine if the model has all of the given permissions. |
||
| 274 | * |
||
| 275 | * @param array ...$permissions |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | * @throws \Exception |
||
| 279 | */ |
||
| 280 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Determine if the model has, via roles, the given permission. |
||
| 295 | * |
||
| 296 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 297 | * |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Determine if the model has the given permission. |
||
| 307 | * |
||
| 308 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | * @throws PermissionDoesNotExist |
||
| 312 | */ |
||
| 313 | public function hasDirectPermission($permission): bool |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Return all the permissions the model has via roles. |
||
| 334 | */ |
||
| 335 | public function getPermissionsViaRoles(): Collection |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Return all the permissions the model has, both directly and via roles. |
||
| 345 | */ |
||
| 346 | public function getAllPermissions(): Collection |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Grant the given permission(s) to a role. |
||
| 360 | * |
||
| 361 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Remove all current permissions and set the given ones. |
||
| 413 | * |
||
| 414 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 415 | * |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | public function syncPermissions(...$permissions) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Revoke the given permission. |
||
| 427 | * |
||
| 428 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
| 429 | * |
||
| 430 | * @return $this |
||
| 431 | */ |
||
| 432 | public function revokePermissionTo($permission) |
||
| 442 | |||
| 443 | public function getPermissionNames(): Collection |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 450 | * |
||
| 451 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
| 452 | */ |
||
| 453 | protected function getStoredPermission($permissions) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
| 477 | * |
||
| 478 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
| 479 | */ |
||
| 480 | protected function ensureModelSharesGuard($roleOrPermission) |
||
| 486 | |||
| 487 | protected function getGuardNames(): Collection |
||
| 491 | |||
| 492 | protected function getDefaultGuardName(): string |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Forget the cached permissions. |
||
| 499 | */ |
||
| 500 | public function forgetCachedPermissions() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Check if the model has All of the requested Direct permissions. |
||
| 507 | * @param array ...$permissions |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | View Code Duplication | public function hasAllDirectPermissions(...$permissions): bool |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Check if the model has Any of the requested Direct permissions. |
||
| 525 | * @param array ...$permissions |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | View Code Duplication | public function hasAnyDirectPermission(...$permissions): bool |
|
| 540 | } |
||
| 541 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.