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 | private $roleClass; |
||
| 16 | |||
| 17 | public static function bootHasRoles() |
||
| 18 | { |
||
| 19 | static::deleting(function ($model) { |
||
| 20 | if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) { |
||
| 21 | return; |
||
| 22 | } |
||
| 23 | |||
| 24 | $model->roles()->detach(); |
||
| 25 | }); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function getRoleClass() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * A model may have multiple roles. |
||
| 39 | */ |
||
| 40 | public function roles(): MorphToMany |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Scope the model query to certain roles only. |
||
| 53 | * |
||
| 54 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 55 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 56 | * @param string $guard |
||
| 57 | * |
||
| 58 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 59 | */ |
||
| 60 | public function scopeRole(Builder $query, $roles, $guard = null): Builder |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Assign the given role to the model. |
||
| 92 | * |
||
| 93 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 94 | * |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function assignRole(...$roles) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Revoke the given role from the model. |
||
| 144 | * |
||
| 145 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 146 | */ |
||
| 147 | public function removeRole($role) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Remove all current roles and set the given ones. |
||
| 160 | * |
||
| 161 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
| 162 | * |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | public function syncRoles(...$roles) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Determine if the model has (one of) the given role(s). |
||
| 174 | * |
||
| 175 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 176 | * @param string|null $guard |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function hasRole($roles, string $guard = null): bool |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Determine if the model has any of the given role(s). |
||
| 216 | * |
||
| 217 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function hasAnyRole($roles): bool |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Determine if the model has all of the given role(s). |
||
| 228 | * |
||
| 229 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 230 | * @param string|null $guard |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function hasAllRoles($roles, string $guard = null): bool |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return all permissions directly coupled to the model. |
||
| 261 | */ |
||
| 262 | public function getDirectPermissions(): Collection |
||
| 266 | |||
| 267 | public function getRoleNames(): Collection |
||
| 271 | |||
| 272 | protected function getStoredRole($role): Role |
||
| 286 | |||
| 287 | protected function convertPipeToArray(string $pipeString) |
||
| 308 | } |
||
| 309 |
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.