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 |
||
14 | trait HasRoles |
||
15 | { |
||
16 | use HasPermissions; |
||
17 | |||
18 | public static function bootHasRoles() |
||
33 | |||
34 | /** |
||
35 | * A model may have multiple roles. |
||
36 | */ |
||
37 | public function roles(): MorphToMany |
||
47 | |||
48 | /** |
||
49 | * A model may have multiple direct permissions. |
||
50 | */ |
||
51 | public function permissions(): MorphToMany |
||
61 | |||
62 | /** |
||
63 | * Scope the model query to certain roles only. |
||
64 | * |
||
65 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
66 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
67 | * |
||
68 | * @return \Illuminate\Database\Eloquent\Builder |
||
69 | */ |
||
70 | public function scopeRole(Builder $query, $roles): Builder |
||
96 | |||
97 | /** |
||
98 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | protected function convertToPermissionModels($permissions): array |
||
118 | |||
119 | /** |
||
120 | * Scope the model query to certain permissions only. |
||
121 | * |
||
122 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
123 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
124 | * |
||
125 | * @return \Illuminate\Database\Eloquent\Builder |
||
126 | */ |
||
127 | public function scopePermission(Builder $query, $permissions): Builder |
||
155 | |||
156 | /** |
||
157 | * Assign the given role to the model. |
||
158 | * |
||
159 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
160 | * |
||
161 | * @return $this |
||
162 | */ |
||
163 | View Code Duplication | public function assignRole(...$roles) |
|
181 | |||
182 | /** |
||
183 | * Revoke the given role from the model. |
||
184 | * |
||
185 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
186 | */ |
||
187 | public function removeRole($role) |
||
191 | |||
192 | /** |
||
193 | * Remove all current roles and set the given ones. |
||
194 | * |
||
195 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function syncRoles(...$roles) |
||
205 | |||
206 | /** |
||
207 | * Determine if the model has (one of) the given role(s). |
||
208 | * |
||
209 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function hasRole($roles): bool |
||
239 | |||
240 | /** |
||
241 | * Determine if the model has any of the given role(s). |
||
242 | * |
||
243 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function hasAnyRole($roles): bool |
||
251 | |||
252 | /** |
||
253 | * Determine if the model has all of the given role(s). |
||
254 | * |
||
255 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function hasAllRoles($roles): bool |
||
279 | |||
280 | /** |
||
281 | * Determine if the model may perform the given permission. |
||
282 | * |
||
283 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
284 | * @param string|null $guardName |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function hasPermissionTo($permission, $guardName = null): bool |
||
299 | |||
300 | /** |
||
301 | * Determine if the model has any of the given permissions. |
||
302 | * |
||
303 | * @param array ...$permissions |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | public function hasAnyPermission(...$permissions): bool |
||
321 | |||
322 | /** |
||
323 | * Determine if the model has, via roles, the given permission. |
||
324 | * |
||
325 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | protected function hasPermissionViaRole(Permission $permission): bool |
||
333 | |||
334 | /** |
||
335 | * Determine if the model has the given permission. |
||
336 | * |
||
337 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | public function hasDirectPermission($permission): bool |
||
353 | |||
354 | /** |
||
355 | * Return all permissions the directory coupled to the model. |
||
356 | */ |
||
357 | public function getDirectPermissions(): Collection |
||
361 | |||
362 | /** |
||
363 | * Return all the permissions the model has via roles. |
||
364 | */ |
||
365 | public function getPermissionsViaRoles(): Collection |
||
372 | |||
373 | /** |
||
374 | * Return all the permissions the model has, both directly and via roles. |
||
375 | */ |
||
376 | public function getAllPermissions(): Collection |
||
383 | |||
384 | public function getRoleNames(): Collection |
||
388 | |||
389 | View Code Duplication | protected function getStoredRole($role): Role |
|
397 | |||
398 | protected function convertPipeToArray(string $pipeString) |
||
419 | } |
||
420 |
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.