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 HasRoles 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 HasRoles, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | trait HasRoles |
||
| 12 | { |
||
| 13 | use HasPermissions; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * A model may have multiple roles. |
||
| 17 | */ |
||
| 18 | public function roles(): MorphToMany |
||
| 19 | { |
||
| 20 | return $this->morphToMany( |
||
|
|
|||
| 21 | config('permission.models.role'), |
||
| 22 | 'model', |
||
| 23 | config('permission.table_names.model_has_roles'), |
||
| 24 | 'model_id', |
||
| 25 | 'role_id' |
||
| 26 | ); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * A model may have multiple direct permissions. |
||
| 31 | */ |
||
| 32 | public function permissions(): MorphToMany |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Scope the model query to certain roles only. |
||
| 45 | * |
||
| 46 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 47 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
| 48 | * |
||
| 49 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 50 | */ |
||
| 51 | public function scopeRole(Builder $query, $roles): Builder |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Assign the given role to the model. |
||
| 80 | * |
||
| 81 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 82 | * |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function assignRole(...$roles) |
||
| 86 | { |
||
| 87 | $roles = collect($roles) |
||
| 88 | ->flatten() |
||
| 89 | ->map(function ($role) { |
||
| 90 | return $this->getStoredRole($role); |
||
| 91 | }) |
||
| 92 | ->each(function ($role) { |
||
| 93 | $this->ensureModelSharesGuard($role); |
||
| 94 | }) |
||
| 95 | ->all(); |
||
| 96 | |||
| 97 | $this->roles()->saveMany($roles); |
||
| 98 | |||
| 99 | $this->forgetCachedPermissions(); |
||
| 100 | |||
| 101 | return $this; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Revoke the given role from the model. |
||
| 106 | * |
||
| 107 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 108 | */ |
||
| 109 | public function removeRole($role) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Remove all current roles and set the given ones. |
||
| 116 | * |
||
| 117 | * @param array ...$roles |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function syncRoles(...$roles) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Determine if the model has (one of) the given role(s). |
||
| 130 | * |
||
| 131 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function hasRole($roles): bool |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Determine if the model has any of the given role(s). |
||
| 164 | * |
||
| 165 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function hasAnyRole($roles): bool |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Determine if the model has all of the given role(s). |
||
| 176 | * |
||
| 177 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function hasAllRoles($roles): bool |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Determine if the model may perform the given permission. |
||
| 204 | * |
||
| 205 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
| 206 | * @param string|null $guardName |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function hasPermissionTo($permission, $guardName = null): bool |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Determine if the model has any of the given permissions. |
||
| 224 | * |
||
| 225 | * @param array ...$permissions |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function hasAnyPermission(...$permissions): bool |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Determine if the model has, via roles, the given permission. |
||
| 246 | * |
||
| 247 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Determine if the model has the given permission. |
||
| 258 | * |
||
| 259 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | public function hasDirectPermission($permission): bool |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Return all permissions the directory coupled to the model. |
||
| 278 | */ |
||
| 279 | public function getDirectPermissions(): Collection |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return all the permissions the model has via roles. |
||
| 286 | */ |
||
| 287 | public function getPermissionsViaRoles(): Collection |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Return all the permissions the model has, both directly and via roles. |
||
| 297 | */ |
||
| 298 | public function getAllPermissions(): Collection |
||
| 305 | |||
| 306 | View Code Duplication | protected function getStoredRole($role): Role |
|
| 314 | |||
| 315 | protected function convertPipeToArray(string $pipeString) |
||
| 336 | } |
||
| 337 |
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.