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 | |||
| 21 | public static function bootHasPermissions() |
||
| 31 | |||
| 32 | public function getPermissionClass() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * A model may have multiple direct permissions. |
||
| 43 | */ |
||
| 44 | public function permissions(): MorphToMany |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Scope the model query to certain permissions only. |
||
| 57 | * |
||
| 58 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 59 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 60 | * |
||
| 61 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 62 | */ |
||
| 63 | public function scopePermission(Builder $query, $permissions): Builder |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | protected function convertToPermissionModels($permissions): array |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Determine if the model may perform the given permission. |
||
| 107 | * |
||
| 108 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 109 | * @param string|null $guardName |
||
| 110 | * |
||
| 111 | * @return bool |
||
| 112 | * @throws PermissionDoesNotExist |
||
| 113 | */ |
||
| 114 | public function hasPermissionTo($permission, $guardName = null): bool |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Validates a wildcard permission against all permissions of a user. |
||
| 145 | * |
||
| 146 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 147 | * @param string|null $guardName |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | protected function hasWildcardPermission($permission, $guardName = null): bool |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @deprecated since 2.35.0 |
||
| 184 | * @alias of hasPermissionTo() |
||
| 185 | */ |
||
| 186 | public function hasUncachedPermissionTo($permission, $guardName = null): bool |
||
| 190 | |||
| 191 | /** |
||
| 192 | * An alias to hasPermissionTo(), but avoids throwing an exception. |
||
| 193 | * |
||
| 194 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 195 | * @param string|null $guardName |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function checkPermissionTo($permission, $guardName = null): bool |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Determine if the model has any of the given permissions. |
||
| 210 | * |
||
| 211 | * @param array ...$permissions |
||
| 212 | * |
||
| 213 | * @return bool |
||
| 214 | * @throws \Exception |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Determine if the model has all of the given permissions. |
||
| 231 | * |
||
| 232 | * @param array ...$permissions |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | * @throws \Exception |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Determine if the model has, via roles, the given permission. |
||
| 252 | * |
||
| 253 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Determine if the model has the given permission. |
||
| 264 | * |
||
| 265 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | * @throws PermissionDoesNotExist |
||
| 269 | */ |
||
| 270 | public function hasDirectPermission($permission): bool |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return all the permissions the model has via roles. |
||
| 291 | */ |
||
| 292 | public function getPermissionsViaRoles(): Collection |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Return all the permissions the model has, both directly and via roles. |
||
| 302 | */ |
||
| 303 | public function getAllPermissions(): Collection |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Grant the given permission(s) to a role. |
||
| 317 | * |
||
| 318 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Remove all current permissions and set the given ones. |
||
| 370 | * |
||
| 371 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 372 | * |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function syncPermissions(...$permissions) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Revoke the given permission. |
||
| 384 | * |
||
| 385 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
| 386 | * |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | public function revokePermissionTo($permission) |
||
| 399 | |||
| 400 | public function getPermissionNames(): Collection |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 407 | * |
||
| 408 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
| 409 | */ |
||
| 410 | protected function getStoredPermission($permissions) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
| 434 | * |
||
| 435 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
| 436 | */ |
||
| 437 | protected function ensureModelSharesGuard($roleOrPermission) |
||
| 443 | |||
| 444 | protected function getGuardNames(): Collection |
||
| 448 | |||
| 449 | protected function getDefaultGuardName(): string |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Forget the cached permissions. |
||
| 456 | */ |
||
| 457 | public function forgetCachedPermissions() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Check if the model has All of the requested Direct permissions. |
||
| 464 | * @param array ...$permissions |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function hasAllDirectPermissions(...$permissions): bool |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Check if the model has Any of the requested Direct permissions. |
||
| 482 | * @param array ...$permissions |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | View Code Duplication | public function hasAnyDirectPermission(...$permissions): bool |
|
| 497 | } |
||
| 498 |
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.