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 |
||
| 10 | trait HasRoles |
||
| 11 | { |
||
| 12 | use HasPermissions; |
||
| 13 | |||
| 14 | public static function bootHasRoles() |
||
| 15 | { |
||
| 16 | static::deleting(function ($model) { |
||
| 17 | if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) { |
||
| 18 | return; |
||
| 19 | } |
||
| 20 | |||
| 21 | $model->roles()->detach(); |
||
| 22 | }); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * A model may have multiple roles. |
||
| 27 | */ |
||
| 28 | public function roles(): BelongsToMany |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Scope the model query to certain roles only. |
||
| 41 | * |
||
| 42 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 43 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 44 | * @param string $guard |
||
| 45 | * |
||
| 46 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function scopeRole(Builder $query, $roles, $guard = null): Builder |
|
| 49 | { |
||
| 50 | if ($roles instanceof Collection) { |
||
| 51 | $roles = $roles->all(); |
||
| 52 | } |
||
| 53 | |||
| 54 | if (! is_array($roles)) { |
||
| 55 | $roles = [$roles]; |
||
| 56 | } |
||
| 57 | |||
| 58 | $roles = $this->getArgumentRoles($roles, $guard); |
||
| 59 | |||
| 60 | return $query->whereHas('roles', function (Builder $subQuery) use ($roles) { |
||
| 61 | $subQuery->whereIn(config('permission.table_names.roles').'.id', \array_column($roles, 'id')); |
||
| 62 | }); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Scope the model query without certain roles. |
||
| 67 | * |
||
| 68 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 69 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 70 | * @param string $guard |
||
| 71 | * |
||
| 72 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function scopeWithoutRole(Builder $query, $roles, $guard = null): Builder |
|
| 75 | { |
||
| 76 | if ($roles instanceof Collection) { |
||
| 77 | $roles = $roles->all(); |
||
| 78 | } |
||
| 79 | |||
| 80 | if (! is_array($roles)) { |
||
| 81 | $roles = [$roles]; |
||
| 82 | } |
||
| 83 | |||
| 84 | $roles = $this->getArgumentRoles($roles, $guard); |
||
| 85 | |||
| 86 | return $query->whereDoesntHave('roles', function (Builder $subQuery) use ($roles) { |
||
| 87 | $subQuery->whereIn(config('permission.table_names.roles').'.id', \array_column($roles, 'id')); |
||
| 88 | }); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Assign the given role to the model. |
||
| 93 | * |
||
| 94 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 95 | * |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | View Code Duplication | public function assignRole(...$roles) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Revoke the given role from the model. |
||
| 145 | * |
||
| 146 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 147 | */ |
||
| 148 | public function removeRole($role) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Remove all current roles and set the given ones. |
||
| 161 | * |
||
| 162 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
| 163 | * |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function syncRoles(...$roles) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Determine if the model has (one of) the given role(s). |
||
| 175 | * |
||
| 176 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 177 | * @param string|null $guard |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function hasRole($roles, string $guard = null): bool |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Determine if the model has any of the given role(s). |
||
| 217 | * |
||
| 218 | * Alias to hasRole() but without Guard controls |
||
| 219 | * |
||
| 220 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function hasAnyRole(...$roles): bool |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Determine if the model has all of the given role(s). |
||
| 231 | * |
||
| 232 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 233 | * @param string|null $guard |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function hasAllRoles($roles, string $guard = null): bool |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Return all permissions directly coupled to the model. |
||
| 264 | */ |
||
| 265 | public function getDirectPermissions(): Collection |
||
| 269 | |||
| 270 | public function getRoleNames(): Collection |
||
| 274 | |||
| 275 | protected function getStoredRole($role): Role |
||
| 289 | |||
| 290 | protected function convertPipeToArray(string $pipeString) |
||
| 311 | |||
| 312 | protected function getArgumentRoles($roles, $guard = null) |
||
| 325 | } |
||
| 326 |
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.