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() |
||
26 | |||
27 | /** |
||
28 | * A model may have multiple roles. |
||
29 | */ |
||
30 | public function roles(): MorphToMany |
||
31 | { |
||
32 | return $this->morphToMany( |
||
|
|||
33 | config('permission.models.role'), |
||
34 | 'model', |
||
35 | config('permission.table_names.model_has_roles'), |
||
36 | 'model_id', |
||
37 | 'role_id' |
||
38 | ); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * A model may have multiple direct permissions. |
||
43 | */ |
||
44 | public function permissions(): MorphToMany |
||
54 | |||
55 | /** |
||
56 | * Scope the model query to certain roles only. |
||
57 | * |
||
58 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
59 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
60 | * |
||
61 | * @return \Illuminate\Database\Eloquent\Builder |
||
62 | */ |
||
63 | public function scopeRole(Builder $query, $roles): Builder |
||
64 | { |
||
65 | if ($roles instanceof Collection) { |
||
66 | $roles = $roles->all(); |
||
67 | } |
||
68 | |||
69 | if (! is_array($roles)) { |
||
70 | $roles = [$roles]; |
||
71 | } |
||
72 | |||
73 | $roles = array_map(function ($role) { |
||
74 | if ($role instanceof Role) { |
||
75 | return $role; |
||
76 | } |
||
77 | |||
78 | return app(Role::class)->findByName($role, $this->getDefaultGuardName()); |
||
79 | }, $roles); |
||
80 | |||
81 | return $query->whereHas('roles', function ($query) use ($roles) { |
||
82 | $query->where(function ($query) use ($roles) { |
||
83 | foreach ($roles as $role) { |
||
84 | $query->orWhere(config('permission.table_names.roles').'.id', $role->id); |
||
85 | } |
||
86 | }); |
||
87 | }); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | protected function convertToPermissionModels($permissions): array |
||
111 | |||
112 | /** |
||
113 | * Scope the model query to certain permissions only. |
||
114 | * |
||
115 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
116 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
117 | * |
||
118 | * @return \Illuminate\Database\Eloquent\Builder |
||
119 | */ |
||
120 | public function scopePermission(Builder $query, $permissions): Builder |
||
148 | |||
149 | /** |
||
150 | * Assign the given role to the model. |
||
151 | * |
||
152 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | View Code Duplication | public function assignRole(...$roles) |
|
157 | { |
||
158 | $roles = collect($roles) |
||
159 | ->flatten() |
||
160 | ->map(function ($role) { |
||
161 | return $this->getStoredRole($role); |
||
162 | }) |
||
163 | ->each(function ($role) { |
||
164 | $this->ensureModelSharesGuard($role); |
||
165 | }) |
||
166 | ->all(); |
||
167 | |||
168 | $this->roles()->saveMany($roles); |
||
169 | |||
170 | $this->forgetCachedPermissions(); |
||
171 | |||
172 | return $this; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Revoke the given role from the model. |
||
177 | * |
||
178 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
179 | */ |
||
180 | public function removeRole($role) |
||
184 | |||
185 | /** |
||
186 | * Remove all current roles and set the given ones. |
||
187 | * |
||
188 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function syncRoles(...$roles) |
||
198 | |||
199 | /** |
||
200 | * Determine if the model has (one of) the given role(s). |
||
201 | * |
||
202 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function hasRole($roles): bool |
||
232 | |||
233 | /** |
||
234 | * Determine if the model has any of the given role(s). |
||
235 | * |
||
236 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
237 | * |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function hasAnyRole($roles): bool |
||
244 | |||
245 | /** |
||
246 | * Determine if the model has all of the given role(s). |
||
247 | * |
||
248 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function hasAllRoles($roles): bool |
||
272 | |||
273 | /** |
||
274 | * Determine if the model may perform the given permission. |
||
275 | * |
||
276 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
277 | * @param string|null $guardName |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function hasPermissionTo($permission, $guardName = null): bool |
||
296 | |||
297 | /** |
||
298 | * Determine if the model has any of the given permissions. |
||
299 | * |
||
300 | * @param array ...$permissions |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function hasAnyPermission(...$permissions): bool |
||
318 | |||
319 | /** |
||
320 | * Determine if the model has, via roles, the given permission. |
||
321 | * |
||
322 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
323 | * |
||
324 | * @return bool |
||
325 | */ |
||
326 | protected function hasPermissionViaRole(Permission $permission): bool |
||
330 | |||
331 | /** |
||
332 | * Determine if the model has the given permission. |
||
333 | * |
||
334 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | public function hasDirectPermission($permission): bool |
||
356 | |||
357 | /** |
||
358 | * Return all permissions the directory coupled to the model. |
||
359 | */ |
||
360 | public function getDirectPermissions(): Collection |
||
364 | |||
365 | /** |
||
366 | * Return all the permissions the model has via roles. |
||
367 | */ |
||
368 | public function getPermissionsViaRoles(): Collection |
||
375 | |||
376 | /** |
||
377 | * Return all the permissions the model has, both directly and via roles. |
||
378 | */ |
||
379 | public function getAllPermissions(): Collection |
||
386 | |||
387 | public function getRoleNames(): Collection |
||
391 | |||
392 | protected function getStoredRole($role): Role |
||
404 | |||
405 | protected function convertPipeToArray(string $pipeString) |
||
426 | } |
||
427 |
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.