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 |
||
| 16 | trait HasPermissions |
||
| 17 | { |
||
| 18 | private $permissionClass; |
||
| 19 | private $roleClass; |
||
| 20 | |||
| 21 | public static function bootHasPermissions() |
||
| 31 | |||
| 32 | public function getPermissionClass() |
||
| 40 | |||
| 41 | public function getRoleClass() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * A model may have multiple direct permissions. |
||
| 52 | */ |
||
| 53 | public function permissions(): BelongsToMany |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Scope the model query to certain permissions only. |
||
| 66 | * |
||
| 67 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 68 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 69 | * |
||
| 70 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function scopePermission(Builder $query, $permissions): Builder |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Scope the model query without certain permissions. |
||
| 92 | * |
||
| 93 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 94 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 95 | * |
||
| 96 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 97 | */ |
||
| 98 | View Code Duplication | public function scopeWithoutPermission(Builder $query, $permissions): Builder |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | protected function convertToPermissionModels($permissions): array |
||
| 137 | |||
| 138 | protected function convertPermissionsToRoleIds($permissions) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Determine if the model may perform the given permission. |
||
| 149 | * |
||
| 150 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 151 | * @param string|null $guardName |
||
| 152 | * |
||
| 153 | * @return bool |
||
| 154 | * @throws PermissionDoesNotExist |
||
| 155 | */ |
||
| 156 | public function hasPermissionTo($permission, $guardName = null): bool |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Validates a wildcard permission against all permissions of a user. |
||
| 187 | * |
||
| 188 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 189 | * @param string|null $guardName |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | protected function hasWildcardPermission($permission, $guardName = null): bool |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @deprecated since 2.35.0 |
||
| 226 | * @alias of hasPermissionTo() |
||
| 227 | */ |
||
| 228 | public function hasUncachedPermissionTo($permission, $guardName = null): bool |
||
| 232 | |||
| 233 | /** |
||
| 234 | * An alias to hasPermissionTo(), but avoids throwing an exception. |
||
| 235 | * |
||
| 236 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 237 | * @param string|null $guardName |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function checkPermissionTo($permission, $guardName = null): bool |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Determine if the model has any of the given permissions. |
||
| 252 | * |
||
| 253 | * @param array ...$permissions |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | * @throws \Exception |
||
| 257 | */ |
||
| 258 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Determine if the model has all of the given permissions. |
||
| 273 | * |
||
| 274 | * @param array ...$permissions |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | * @throws \Exception |
||
| 278 | */ |
||
| 279 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Determine if the model has, via roles, the given permission. |
||
| 294 | * |
||
| 295 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Determine if the model has the given permission. |
||
| 306 | * |
||
| 307 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | * @throws PermissionDoesNotExist |
||
| 311 | */ |
||
| 312 | public function hasDirectPermission($permission): bool |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Return all the permissions the model has via roles. |
||
| 333 | */ |
||
| 334 | public function getPermissionsViaRoles(): Collection |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Return all the permissions the model has, both directly and via roles. |
||
| 344 | */ |
||
| 345 | public function getAllPermissions(): Collection |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Grant the given permission(s) to a role. |
||
| 359 | * |
||
| 360 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 361 | * |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Remove all current permissions and set the given ones. |
||
| 412 | * |
||
| 413 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 414 | * |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | public function syncPermissions(...$permissions) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Revoke the given permission. |
||
| 426 | * |
||
| 427 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
| 428 | * |
||
| 429 | * @return $this |
||
| 430 | */ |
||
| 431 | public function revokePermissionTo($permission) |
||
| 441 | |||
| 442 | public function getPermissionNames(): Collection |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 449 | * |
||
| 450 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
| 451 | */ |
||
| 452 | protected function getStoredPermission($permissions) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
| 476 | * |
||
| 477 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
| 478 | */ |
||
| 479 | protected function ensureModelSharesGuard($roleOrPermission) |
||
| 485 | |||
| 486 | protected function getGuardNames(): Collection |
||
| 490 | |||
| 491 | protected function getDefaultGuardName(): string |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Forget the cached permissions. |
||
| 498 | */ |
||
| 499 | public function forgetCachedPermissions() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Check if the model has All of the requested Direct permissions. |
||
| 506 | * @param array ...$permissions |
||
| 507 | * @return bool |
||
| 508 | */ |
||
| 509 | View Code Duplication | public function hasAllDirectPermissions(...$permissions): bool |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Check if the model has Any of the requested Direct permissions. |
||
| 524 | * @param array ...$permissions |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | View Code Duplication | public function hasAnyDirectPermission(...$permissions): bool |
|
| 539 | } |
||
| 540 |
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.