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:
| 1 | <?php |
||
| 11 | trait HasRoles |
||
| 12 | { |
||
| 13 | use HasPermissions; |
||
| 14 | use RefreshesPermissionCache; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * A model may have multiple roles. |
||
| 18 | */ |
||
| 19 | public function roles(): MorphToMany |
||
| 29 | |||
| 30 | /** |
||
| 31 | * A model may have multiple direct permissions. |
||
| 32 | */ |
||
| 33 | public function permissions(): MorphToMany |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Scope the model query to certain roles only. |
||
| 46 | * |
||
| 47 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 48 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
| 49 | * |
||
| 50 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 51 | */ |
||
| 52 | public function scopeRole(Builder $query, $roles): Builder |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Assign the given role to the model. |
||
| 83 | * |
||
| 84 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 85 | * |
||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function assignRole(...$roles) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Revoke the given role from the model. |
||
| 109 | * |
||
| 110 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 111 | */ |
||
| 112 | public function removeRole($role) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Remove all current roles and set the given ones. |
||
| 119 | * |
||
| 120 | * @param array ...$roles |
||
| 121 | * |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | public function syncRoles(...$roles) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Determine if the model has (one of) the given role(s). |
||
| 133 | * |
||
| 134 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public function hasRole($roles): bool |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Determine if the model has any of the given role(s). |
||
| 163 | * |
||
| 164 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function hasAnyRole($roles): bool |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Determine if the model has all of the given role(s). |
||
| 175 | * |
||
| 176 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function hasAllRoles($roles): bool |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Determine if the model may perform the given permission. |
||
| 199 | * |
||
| 200 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function hasPermissionTo($permission): bool |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Determine if the model has any of the given permissions. |
||
| 215 | * |
||
| 216 | * @param array ...$permissions |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function hasAnyPermission(...$permissions): bool |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Determine if the model has, via roles, the given permission. |
||
| 233 | * |
||
| 234 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | protected function hasPermissionViaRole(Permission $permission): bool |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Determine if the model has the given permission. |
||
| 245 | * |
||
| 246 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function hasDirectPermission($permission): bool |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Return all permissions the directory coupled to the model. |
||
| 265 | */ |
||
| 266 | public function getDirectPermissions(): Collection |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Return all the permissions the model has via roles. |
||
| 273 | */ |
||
| 274 | public function getPermissionsViaRoles(): Collection |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Return all the permissions the model has, both directly and via roles. |
||
| 284 | */ |
||
| 285 | public function getAllPermissions(): Collection |
||
| 292 | |||
| 293 | protected function getStoredRole($role): Role |
||
| 301 | } |
||
| 302 |
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.