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() |
||
| 28 | |||
| 29 | /** |
||
| 30 | * A model may have multiple roles. |
||
| 31 | */ |
||
| 32 | public function roles(): MorphToMany |
||
| 42 | |||
| 43 | /** |
||
| 44 | * A model may have multiple direct permissions. |
||
| 45 | */ |
||
| 46 | public function permissions(): MorphToMany |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Scope the model query to certain roles only. |
||
| 59 | * |
||
| 60 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 61 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
| 62 | * |
||
| 63 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 64 | */ |
||
| 65 | public function scopeRole(Builder $query, $roles): Builder |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Assign the given role to the model. |
||
| 94 | * |
||
| 95 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 96 | * |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function assignRole(...$roles) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Revoke the given role from the model. |
||
| 120 | * |
||
| 121 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 122 | */ |
||
| 123 | public function removeRole($role) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Remove all current roles and set the given ones. |
||
| 130 | * |
||
| 131 | * @param array ...$roles |
||
| 132 | * |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public function syncRoles(...$roles) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Determine if the model has (one of) the given role(s). |
||
| 144 | * |
||
| 145 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | public function hasRole($roles): bool |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Determine if the model has any of the given role(s). |
||
| 178 | * |
||
| 179 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function hasAnyRole($roles): bool |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Determine if the model has all of the given role(s). |
||
| 190 | * |
||
| 191 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function hasAllRoles($roles): bool |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Determine if the model may perform the given permission. |
||
| 218 | * |
||
| 219 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
| 220 | * @param string|null $guardName |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function hasPermissionTo($permission, $guardName = null): bool |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Determine if the model has any of the given permissions. |
||
| 238 | * |
||
| 239 | * @param array ...$permissions |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function hasAnyPermission(...$permissions): bool |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Determine if the model has, via roles, the given permission. |
||
| 260 | * |
||
| 261 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Determine if the model has the given permission. |
||
| 272 | * |
||
| 273 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function hasDirectPermission($permission): bool |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Return all permissions the directory coupled to the model. |
||
| 292 | */ |
||
| 293 | public function getDirectPermissions(): Collection |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return all the permissions the model has via roles. |
||
| 300 | */ |
||
| 301 | public function getPermissionsViaRoles(): Collection |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Return all the permissions the model has, both directly and via roles. |
||
| 311 | */ |
||
| 312 | public function getAllPermissions(): Collection |
||
| 319 | |||
| 320 | View Code Duplication | protected function getStoredRole($role): Role |
|
| 328 | |||
| 329 | protected function convertPipeToArray(string $pipeString) |
||
| 350 | } |
||
| 351 |
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.