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 | * |
||
| 57 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 58 | */ |
||
| 59 | public function scopeRole(Builder $query, $roles): Builder |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Assign the given role to the model. |
||
| 90 | * |
||
| 91 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 92 | * |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function assignRole(...$roles) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Revoke the given role from the model. |
||
| 142 | * |
||
| 143 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 144 | */ |
||
| 145 | public function removeRole($role) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Remove all current roles and set the given ones. |
||
| 154 | * |
||
| 155 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function syncRoles(...$roles) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Determine if the model has (one of) the given role(s). |
||
| 168 | * |
||
| 169 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function hasRole($roles): bool |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Determine if the model has any of the given role(s). |
||
| 206 | * |
||
| 207 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public function hasAnyRole($roles): bool |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Determine if the model has all of the given role(s). |
||
| 218 | * |
||
| 219 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | public function hasAllRoles($roles): bool |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Return all permissions directly coupled to the model. |
||
| 246 | */ |
||
| 247 | public function getDirectPermissions(): Collection |
||
| 251 | |||
| 252 | public function getRoleNames(): Collection |
||
| 256 | |||
| 257 | protected function getStoredRole($role): Role |
||
| 271 | |||
| 272 | protected function convertPipeToArray(string $pipeString) |
||
| 293 | } |
||
| 294 |
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.