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() |
||
22 | |||
23 | /** |
||
24 | * A model may have multiple roles. |
||
25 | */ |
||
26 | public function roles(): MorphToMany |
||
36 | |||
37 | /** |
||
38 | * A model may have multiple direct permissions. |
||
39 | */ |
||
40 | public function permissions(): MorphToMany |
||
50 | |||
51 | /** |
||
52 | * Scope the model query to certain roles only. |
||
53 | * |
||
54 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
55 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
56 | * |
||
57 | * @return \Illuminate\Database\Eloquent\Builder |
||
58 | */ |
||
59 | public function scopeRole(Builder $query, $roles): Builder |
||
85 | |||
86 | /** |
||
87 | * Assign the given role to the model. |
||
88 | * |
||
89 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | View Code Duplication | public function assignRole(...$roles) |
|
94 | { |
||
95 | $roles = collect($roles) |
||
96 | ->flatten() |
||
97 | ->map(function ($role) { |
||
98 | return $this->getStoredRole($role); |
||
99 | }) |
||
100 | ->each(function ($role) { |
||
101 | $this->ensureModelSharesGuard($role); |
||
102 | }) |
||
103 | ->all(); |
||
104 | |||
105 | $this->roles()->saveMany($roles); |
||
106 | |||
107 | $this->forgetCachedPermissions(); |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Revoke the given role from the model. |
||
114 | * |
||
115 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
116 | */ |
||
117 | public function removeRole($role) |
||
121 | |||
122 | /** |
||
123 | * Remove all current roles and set the given ones. |
||
124 | * |
||
125 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function syncRoles(...$roles) |
||
135 | |||
136 | /** |
||
137 | * Determine if the model has (one of) the given role(s). |
||
138 | * |
||
139 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function hasRole($roles): bool |
||
169 | |||
170 | /** |
||
171 | * Determine if the model has any of the given role(s). |
||
172 | * |
||
173 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function hasAnyRole($roles): bool |
||
181 | |||
182 | /** |
||
183 | * Determine if the model has all of the given role(s). |
||
184 | * |
||
185 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function hasAllRoles($roles): bool |
||
209 | |||
210 | /** |
||
211 | * Determine if the model may perform the given permission. |
||
212 | * |
||
213 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
214 | * @param string|null $guardName |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function hasPermissionTo($permission, $guardName = null): bool |
||
229 | |||
230 | /** |
||
231 | * Determine if the model has any of the given permissions. |
||
232 | * |
||
233 | * @param array ...$permissions |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | public function hasAnyPermission(...$permissions): bool |
||
251 | |||
252 | /** |
||
253 | * Determine if the model has, via roles, the given permission. |
||
254 | * |
||
255 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | protected function hasPermissionViaRole(Permission $permission): bool |
||
263 | |||
264 | /** |
||
265 | * Determine if the model has the given permission. |
||
266 | * |
||
267 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function hasDirectPermission($permission): bool |
||
283 | |||
284 | /** |
||
285 | * Return all permissions the directory coupled to the model. |
||
286 | */ |
||
287 | public function getDirectPermissions(): Collection |
||
291 | |||
292 | /** |
||
293 | * Return all the permissions the model has via roles. |
||
294 | */ |
||
295 | public function getPermissionsViaRoles(): Collection |
||
302 | |||
303 | /** |
||
304 | * Return all the permissions the model has, both directly and via roles. |
||
305 | */ |
||
306 | public function getAllPermissions(): Collection |
||
313 | |||
314 | /** |
||
315 | * Return a collection of role names associated with this user. |
||
316 | * |
||
317 | * @return Collection |
||
318 | */ |
||
319 | public function getRoleNames(): Collection |
||
323 | |||
324 | protected function getStoredRole($role): Role |
||
332 | |||
333 | protected function convertPipeToArray(string $pipeString) |
||
354 | } |
||
355 |
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.