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 |
||
| 10 | trait HasRoles |
||
| 11 | { |
||
| 12 | use HasPermissions; |
||
| 13 | |||
| 14 | public static function bootHasRoles() |
||
| 15 | { |
||
| 16 | static::deleting(function ($model) { |
||
| 17 | if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) { |
||
| 18 | return; |
||
| 19 | } |
||
| 20 | |||
| 21 | $model->roles()->detach(); |
||
| 22 | }); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * A model may have multiple roles. |
||
| 27 | */ |
||
| 28 | public function roles(): MorphToMany |
||
| 29 | { |
||
| 30 | return $this->morphToMany( |
||
|
|
|||
| 31 | config('permission.models.role'), |
||
| 32 | 'model', |
||
| 33 | config('permission.table_names.model_has_roles'), |
||
| 34 | 'model_id', |
||
| 35 | 'role_id' |
||
| 36 | ); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Scope the model query to certain roles only. |
||
| 41 | * |
||
| 42 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 43 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 44 | * |
||
| 45 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 46 | */ |
||
| 47 | public function scopeRole(Builder $query, $roles): Builder |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Assign the given role to the model. |
||
| 76 | * |
||
| 77 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function assignRole(...$roles) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Revoke the given role from the model. |
||
| 102 | * |
||
| 103 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
| 104 | */ |
||
| 105 | public function removeRole($role) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Remove all current roles and set the given ones. |
||
| 112 | * |
||
| 113 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
| 114 | * |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function syncRoles(...$roles) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Determine if the model has (one of) the given role(s). |
||
| 126 | * |
||
| 127 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function hasRole($roles): bool |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Determine if the model has any of the given role(s). |
||
| 160 | * |
||
| 161 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function hasAnyRole($roles): bool |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Determine if the model has all of the given role(s). |
||
| 172 | * |
||
| 173 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public function hasAllRoles($roles): bool |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return all permissions directly coupled to the model. |
||
| 200 | */ |
||
| 201 | public function getDirectPermissions(): Collection |
||
| 205 | |||
| 206 | public function getRoleNames(): Collection |
||
| 210 | |||
| 211 | protected function getStoredRole($role): Role |
||
| 223 | |||
| 224 | protected function convertPipeToArray(string $pipeString) |
||
| 245 | } |
||
| 246 |
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.