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 |
||
| 94 | { |
||
| 95 | if ($permissions instanceof Collection) { |
||
| 96 | $permissions = $permissions->all(); |
||
| 97 | } |
||
| 98 | |||
| 99 | $permissions = is_array($permissions) ? $permissions : [$permissions]; |
||
| 100 | |||
| 101 | return array_map(function ($permission) { |
||
| 102 | if ($permission instanceof Permission) { |
||
| 103 | return $permission; |
||
| 104 | } |
||
| 105 | |||
| 106 | return $this->getPermissionClass()->findByName($permission, $this->getDefaultGuardName()); |
||
| 107 | }, $permissions); |
||
| 108 | } |
||
| 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 |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Check the uncached permissions for the model. |
||
| 143 | * |
||
| 144 | * @param string|int|Permission $permission |
||
| 145 | * @param string|null $guardName |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | * |
||
| 149 | * @throws PermissionDoesNotExist |
||
| 150 | */ |
||
| 151 | public function hasUncachedPermissionTo($permission, $guardName = null): bool |
||
| 152 | { |
||
| 153 | $permissionClass = $this->getPermissionClass(); |
||
| 154 | |||
| 155 | if (is_string($permission)) { |
||
| 156 | $permission = $permissionClass->findByName( |
||
| 157 | $permission, |
||
| 158 | $guardName ?? $this->getDefaultGuardName() |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (is_int($permission)) { |
||
| 163 | $permission = $permissionClass->findById( |
||
| 164 | $permission, |
||
| 165 | $guardName ?? $this->getDefaultGuardName() |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | if (! $permission instanceof Permission) { |
||
| 170 | throw new PermissionDoesNotExist; |
||
| 171 | } |
||
| 172 | |||
| 173 | return $this->hasDirectPermission($permission) || $this->hasPermissionViaRole($permission); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * An alias to hasPermissionTo(), but avoids throwing an exception. |
||
| 178 | * |
||
| 179 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 180 | * @param string|null $guardName |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function checkPermissionTo($permission, $guardName = null): bool |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Construct the key for the cache entry. |
||
| 195 | * |
||
| 196 | * @param null|string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | protected function getPermissionCacheKey($permission = null) |
||
| 201 | { |
||
| 202 | $key = PermissionRegistrar::$cacheKey.'.'.$this->getClassCacheString(); |
||
| 203 | |||
| 204 | if ($permission !== null) { |
||
| 205 | $key .= $this->getPermissionCacheString($permission); |
||
| 206 | } |
||
| 207 | |||
| 208 | return $key; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Construct the tags for the cache entry. |
||
| 213 | * |
||
| 214 | * @param null|string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | protected function getCacheTags($permission = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get the key to cache the model by. |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | private function getClassCacheString() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the key to cache the permission by. |
||
| 244 | * |
||
| 245 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 246 | * |
||
| 247 | * @return mixed |
||
| 248 | */ |
||
| 249 | protected function getPermissionCacheString($permission) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Determine if the model has any of the given permissions. |
||
| 260 | * |
||
| 261 | * @param array ...$permissions |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | * @throws \Exception |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Determine if the model has all of the given permissions. |
||
| 283 | * |
||
| 284 | * @param array ...$permissions |
||
| 285 | * |
||
| 286 | * @return bool |
||
| 287 | * @throws \Exception |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Determine if the model has, via roles, the given permission. |
||
| 306 | * |
||
| 307 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Determine if the model has the given permission. |
||
| 318 | * |
||
| 319 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function hasDirectPermission($permission): bool |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return all the permissions the model has via roles. |
||
| 350 | */ |
||
| 351 | public function getPermissionsViaRoles(): Collection |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Return all the permissions the model has, both directly and via roles. |
||
| 361 | * |
||
| 362 | * @throws \Exception |
||
| 363 | */ |
||
| 364 | public function getAllPermissions(): Collection |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Grant the given permission(s) to a role. |
||
| 392 | * |
||
| 393 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 394 | * |
||
| 395 | * @return $this |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Remove all current permissions and set the given ones. |
||
| 445 | * |
||
| 446 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function syncPermissions(...$permissions) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Revoke the given permission. |
||
| 459 | * |
||
| 460 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
| 461 | * |
||
| 462 | * @return $this |
||
| 463 | */ |
||
| 464 | public function revokePermissionTo($permission) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 477 | * |
||
| 478 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
| 479 | */ |
||
| 480 | protected function getStoredPermission($permissions) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
| 504 | * |
||
| 505 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
| 506 | */ |
||
| 507 | protected function ensureModelSharesGuard($roleOrPermission) |
||
| 513 | |||
| 514 | protected function getGuardNames(): Collection |
||
| 518 | |||
| 519 | protected function getDefaultGuardName(): string |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Forget the cached permissions. |
||
| 526 | */ |
||
| 527 | public function forgetCachedPermissions() |
||
| 531 | } |
||
| 532 |
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.