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 | public static function bootHasRoles() |
||
| 22 | |||
| 23 | /** |
||
| 24 | * A model may have multiple roles. |
||
| 25 | */ |
||
| 26 | public function roles(): MorphToMany |
||
| 36 | |||
| 37 | /** |
||
| 38 | * A model may have multiple direct permissions. |
||
| 39 | */ |
||
| 40 | public function permissions(): 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 |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | protected function convertToPermissionModels($permissions): array |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Scope the model query to certain permissions only. |
||
| 110 | * |
||
| 111 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 112 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
| 113 | * |
||
| 114 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 115 | */ |
||
| 116 | public function scopePermission(Builder $query, $permissions): Builder |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Assign the given role to the model. |
||
| 147 | * |
||
| 148 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function assignRole(...$roles) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Revoke the given role from the model. |
||
| 173 | * |
||
| 174 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 175 | */ |
||
| 176 | public function removeRole($role) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Remove all current roles and set the given ones. |
||
| 183 | * |
||
| 184 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
| 185 | * |
||
| 186 | * @return $this |
||
| 187 | */ |
||
| 188 | public function syncRoles(...$roles) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Determine if the model has (one of) the given role(s). |
||
| 197 | * |
||
| 198 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function hasRole($roles): bool |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Determine if the model has any of the given role(s). |
||
| 231 | * |
||
| 232 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function hasAnyRole($roles): bool |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Determine if the model has all of the given role(s). |
||
| 243 | * |
||
| 244 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | public function hasAllRoles($roles): bool |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Determine if the model may perform the given permission. |
||
| 271 | * |
||
| 272 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
| 273 | * @param string|null $guardName |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function hasPermissionTo($permission, $guardName = null): bool |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Determine if the model has any of the given permissions. |
||
| 291 | * |
||
| 292 | * @param array ...$permissions |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function hasAnyPermission(...$permissions): bool |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Determine if the model has, via roles, the given permission. |
||
| 313 | * |
||
| 314 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Determine if the model has the given permission. |
||
| 325 | * |
||
| 326 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function hasDirectPermission($permission): bool |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Return all permissions the directory coupled to the model. |
||
| 345 | */ |
||
| 346 | public function getDirectPermissions(): Collection |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Return all the permissions the model has via roles. |
||
| 353 | */ |
||
| 354 | public function getPermissionsViaRoles(): Collection |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Return all the permissions the model has, both directly and via roles. |
||
| 364 | */ |
||
| 365 | public function getAllPermissions(): Collection |
||
| 372 | |||
| 373 | public function getRoleNames(): Collection |
||
| 377 | |||
| 378 | protected function getStoredRole($role): Role |
||
| 386 | |||
| 387 | protected function convertPipeToArray(string $pipeString) |
||
| 408 | } |
||
| 409 |
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.