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 | * Scope the model query to certain roles only. |
||
| 92 | * This will not return an exception if the role does not exist. |
||
| 93 | * |
||
| 94 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 95 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 96 | * @param string $guard |
||
| 97 | * |
||
| 98 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 99 | */ |
||
| 100 | public function scopeWhereRole(Builder $query, $roles, $guard = null): Builder |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Assign the given role to the model. |
||
| 135 | * |
||
| 136 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function assignRole(...$roles) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Revoke the given role from the model. |
||
| 187 | * |
||
| 188 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 189 | */ |
||
| 190 | public function removeRole($role) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Remove all current roles and set the given ones. |
||
| 203 | * |
||
| 204 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function syncRoles(...$roles) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Determine if the model has (one of) the given role(s). |
||
| 217 | * |
||
| 218 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 219 | * @param string|null $guard |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function hasRole($roles, string $guard = null): bool |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Determine if the model has any of the given role(s). |
||
| 259 | * |
||
| 260 | * Alias to hasRole() but without Guard controls |
||
| 261 | * |
||
| 262 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | public function hasAnyRole(...$roles): bool |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Determine if the model has all of the given role(s). |
||
| 273 | * |
||
| 274 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 275 | * @param string|null $guard |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function hasAllRoles($roles, string $guard = null): bool |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Return all permissions directly coupled to the model. |
||
| 306 | */ |
||
| 307 | public function getDirectPermissions(): Collection |
||
| 311 | |||
| 312 | public function getRoleNames(): Collection |
||
| 316 | |||
| 317 | protected function getStoredRole($role): Role |
||
| 331 | |||
| 332 | protected function convertPipeToArray(string $pipeString) |
||
| 353 | } |
||
| 354 |
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.