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 |
||
| 14 | trait HasPermissions |
||
| 15 | { |
||
| 16 | private $permissionClass; |
||
| 17 | |||
| 18 | public static function bootHasPermissions() |
||
| 28 | |||
| 29 | public function getPermissionClass() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * A model may have multiple direct permissions. |
||
| 40 | */ |
||
| 41 | public function permissions(): MorphToMany |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Scope the model query to certain permissions only. |
||
| 54 | * |
||
| 55 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 56 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 57 | * |
||
| 58 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 59 | */ |
||
| 60 | public function scopePermission(Builder $query, $permissions): Builder |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | protected function convertToPermissionModels($permissions): array |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Determine if the model may perform the given permission. |
||
| 112 | * |
||
| 113 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 114 | * @param string|null $guardName |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | * @throws PermissionDoesNotExist |
||
| 118 | */ |
||
| 119 | public function hasPermissionTo($permission, $guardName = null): bool |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @deprecated since 2.35.0 |
||
| 146 | * @alias of hasPermissionTo() |
||
| 147 | */ |
||
| 148 | public function hasUncachedPermissionTo($permission, $guardName = null): bool |
||
| 152 | |||
| 153 | /** |
||
| 154 | * An alias to hasPermissionTo(), but avoids throwing an exception. |
||
| 155 | * |
||
| 156 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 157 | * @param string|null $guardName |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function checkPermissionTo($permission, $guardName = null): bool |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Determine if the model has any of the given permissions. |
||
| 172 | * |
||
| 173 | * @param array ...$permissions |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | * @throws \Exception |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Determine if the model has all of the given permissions. |
||
| 195 | * |
||
| 196 | * @param array ...$permissions |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | * @throws \Exception |
||
| 200 | */ |
||
| 201 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Determine if the model has, via roles, the given permission. |
||
| 218 | * |
||
| 219 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Determine if the model has the given permission. |
||
| 230 | * |
||
| 231 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 232 | * |
||
| 233 | * @return bool |
||
| 234 | * @throws PermissionDoesNotExist |
||
| 235 | */ |
||
| 236 | public function hasDirectPermission($permission): bool |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Return all the permissions the model has via roles. |
||
| 257 | */ |
||
| 258 | public function getPermissionsViaRoles(): Collection |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Return all the permissions the model has, both directly and via roles. |
||
| 268 | * |
||
| 269 | * @throws \Exception |
||
| 270 | */ |
||
| 271 | public function getAllPermissions(): Collection |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Grant the given permission(s) to a role. |
||
| 284 | * |
||
| 285 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Remove all current permissions and set the given ones. |
||
| 337 | * |
||
| 338 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 339 | * |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | public function syncPermissions(...$permissions) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Revoke the given permission. |
||
| 351 | * |
||
| 352 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
| 353 | * |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | public function revokePermissionTo($permission) |
||
| 366 | |||
| 367 | public function getPermissionNames(): Collection |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 374 | * |
||
| 375 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
| 376 | */ |
||
| 377 | protected function getStoredPermission($permissions) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
| 401 | * |
||
| 402 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
| 403 | */ |
||
| 404 | protected function ensureModelSharesGuard($roleOrPermission) |
||
| 410 | |||
| 411 | protected function getGuardNames(): Collection |
||
| 415 | |||
| 416 | protected function getDefaultGuardName(): string |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Forget the cached permissions. |
||
| 423 | */ |
||
| 424 | public function forgetCachedPermissions() |
||
| 428 | } |
||
| 429 |
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.