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 | |||
15 | /** |
||
16 | * A model may have multiple roles. |
||
17 | */ |
||
18 | public function roles(): MorphToMany |
||
28 | |||
29 | /** |
||
30 | * A model may have multiple direct permissions. |
||
31 | */ |
||
32 | public function permissions(): MorphToMany |
||
42 | |||
43 | /** |
||
44 | * Scope the model query to certain roles only. |
||
45 | * |
||
46 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
47 | * @param string|array|Role|\Illuminate\Support\Collection $roles |
||
48 | * |
||
49 | * @return \Illuminate\Database\Eloquent\Builder |
||
50 | */ |
||
51 | public function scopeRole(Builder $query, $roles): Builder |
||
79 | |||
80 | /** |
||
81 | * Assign the given role to the model. |
||
82 | * |
||
83 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
84 | * |
||
85 | * @return $this |
||
86 | */ |
||
87 | View Code Duplication | public function assignRole(...$roles) |
|
105 | |||
106 | /** |
||
107 | * Revoke the given role from the model. |
||
108 | * |
||
109 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
110 | */ |
||
111 | public function removeRole($role) |
||
115 | |||
116 | /** |
||
117 | * Remove all current roles and set the given ones. |
||
118 | * |
||
119 | * @param array ...$roles |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function syncRoles(...$roles) |
||
129 | |||
130 | /** |
||
131 | * Determine if the model has (one of) the given role(s). |
||
132 | * |
||
133 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function hasRole($roles): bool |
||
159 | |||
160 | /** |
||
161 | * Determine if the model has any of the given role(s). |
||
162 | * |
||
163 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function hasAnyRole($roles): bool |
||
171 | |||
172 | /** |
||
173 | * Determine if the model has all of the given role(s). |
||
174 | * |
||
175 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function hasAllRoles($roles): bool |
||
195 | |||
196 | /** |
||
197 | * Determine if the model may perform the given permission. |
||
198 | * |
||
199 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function hasPermissionTo($permission): bool |
||
211 | |||
212 | /** |
||
213 | * Determine if the model has any of the given permissions. |
||
214 | * |
||
215 | * @param array ...$permissions |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function hasAnyPermission(...$permissions): bool |
||
229 | |||
230 | /** |
||
231 | * Determine if the model has, via roles, the given permission. |
||
232 | * |
||
233 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | protected function hasPermissionViaRole(Permission $permission): bool |
||
241 | |||
242 | /** |
||
243 | * Determine if the model has the given permission. |
||
244 | * |
||
245 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | public function hasDirectPermission($permission): bool |
||
261 | |||
262 | /** |
||
263 | * Return all permissions the directory coupled to the model. |
||
264 | */ |
||
265 | public function getDirectPermissions(): Collection |
||
269 | |||
270 | /** |
||
271 | * Return all the permissions the model has via roles. |
||
272 | */ |
||
273 | public function getPermissionsViaRoles(): Collection |
||
280 | |||
281 | /** |
||
282 | * Return all the permissions the model has, both directly and via roles. |
||
283 | */ |
||
284 | public function getAllPermissions(): Collection |
||
291 | |||
292 | protected function getStoredRole($role): Role |
||
300 | } |
||
301 |
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
Idable
provides a methodequalsId
that 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.