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() |
||
16 | { |
||
17 | static::deleting(function ($model) { |
||
18 | if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) { |
||
19 | return; |
||
20 | } |
||
21 | |||
22 | $model->roles()->detach(); |
||
23 | }); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * A model may have multiple roles. |
||
28 | */ |
||
29 | public function roles(): MorphToMany |
||
30 | { |
||
31 | return $this->morphToMany( |
||
|
|||
32 | config('permission.models.role'), |
||
33 | 'model', |
||
34 | config('permission.table_names.model_has_roles'), |
||
35 | config('permission.column_names.model_morph_key'), |
||
36 | 'role_id' |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Scope the model query to certain roles only. |
||
42 | * |
||
43 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
44 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
45 | * @param string $guard |
||
46 | * |
||
47 | * @return \Illuminate\Database\Eloquent\Builder |
||
48 | */ |
||
49 | View Code Duplication | public function scopeRole(Builder $query, $roles, $guard = null): Builder |
|
50 | { |
||
51 | if ($roles instanceof Collection) { |
||
52 | $roles = $roles->all(); |
||
53 | } |
||
54 | |||
55 | if (!is_array($roles)) { |
||
56 | $roles = [$roles]; |
||
57 | } |
||
58 | |||
59 | $roles = $this->getArgumentRoles($roles, $guard); |
||
60 | |||
61 | return $query->whereHas('roles', function (Builder $subQuery) use ($roles) { |
||
62 | $subQuery->whereIn(config('permission.table_names.roles').'.id', \array_column($roles, 'id')); |
||
63 | }); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Scope the model query without certain roles. |
||
68 | * |
||
69 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
70 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
71 | * @param string $guard |
||
72 | * |
||
73 | * @return \Illuminate\Database\Eloquent\Builder |
||
74 | */ |
||
75 | View Code Duplication | public function scopeWithoutRole(Builder $query, $roles, $guard = null): Builder |
|
76 | { |
||
77 | if ($roles instanceof Collection) { |
||
78 | $roles = $roles->all(); |
||
79 | } |
||
80 | |||
81 | if (!is_array($roles)) { |
||
82 | $roles = [$roles]; |
||
83 | } |
||
84 | |||
85 | $roles = $this->getArgumentRoles($roles, $guard); |
||
86 | |||
87 | return $query->whereDoesntHave('roles', function (Builder $subQuery) use ($roles) { |
||
88 | $subQuery->whereIn(config('permission.table_names.roles').'.id', \array_column($roles, 'id')); |
||
89 | }); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Assign the given role to the model. |
||
94 | * |
||
95 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
96 | * |
||
97 | * @return $this |
||
98 | */ |
||
99 | View Code Duplication | public function assignRole(...$roles) |
|
100 | { |
||
101 | $roles = collect($roles) |
||
102 | ->flatten() |
||
103 | ->map(function ($role) { |
||
104 | if (empty($role)) { |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | return $this->getStoredRole($role); |
||
109 | }) |
||
110 | ->filter(function ($role) { |
||
111 | return $role instanceof Role; |
||
112 | }) |
||
113 | ->each(function ($role) { |
||
114 | $this->ensureModelSharesGuard($role); |
||
115 | }) |
||
116 | ->map->id |
||
117 | ->all(); |
||
118 | |||
119 | $model = $this->getModel(); |
||
120 | |||
121 | if ($model->exists) { |
||
122 | $this->roles()->sync($roles, false); |
||
123 | $model->load('roles'); |
||
124 | } else { |
||
125 | $class = \get_class($model); |
||
126 | |||
127 | $class::saved( |
||
128 | function ($object) use ($roles, $model) { |
||
129 | static $modelLastFiredOn; |
||
130 | if ($modelLastFiredOn !== null && $modelLastFiredOn === $model) { |
||
131 | return; |
||
132 | } |
||
133 | $object->roles()->sync($roles, false); |
||
134 | $object->load('roles'); |
||
135 | $modelLastFiredOn = $object; |
||
136 | }); |
||
137 | } |
||
138 | |||
139 | $this->forgetCachedPermissions(); |
||
140 | |||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Revoke the given role from the model. |
||
146 | * |
||
147 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
148 | */ |
||
149 | public function removeRole($role) |
||
159 | |||
160 | /** |
||
161 | * Remove all current roles and set the given ones. |
||
162 | * |
||
163 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function syncRoles(...$roles) |
||
173 | |||
174 | /** |
||
175 | * Determine if the model has (one of) the given role(s). |
||
176 | * |
||
177 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
178 | * @param string|null $guard |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function hasRole($roles, string $guard = null): bool |
||
215 | |||
216 | /** |
||
217 | * Determine if the model has any of the given role(s). |
||
218 | * |
||
219 | * Alias to hasRole() but without Guard controls |
||
220 | * |
||
221 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function hasAnyRole(...$roles): bool |
||
229 | |||
230 | /** |
||
231 | * Determine if the model has all of the given role(s). |
||
232 | * |
||
233 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
234 | * @param string|null $guard |
||
235 | * @return bool |
||
236 | */ |
||
237 | public function hasAllRoles($roles, string $guard = null): bool |
||
262 | |||
263 | /** |
||
264 | * Return all permissions directly coupled to the model. |
||
265 | */ |
||
266 | public function getDirectPermissions(): Collection |
||
270 | |||
271 | public function getRoleNames(): Collection |
||
275 | |||
276 | protected function getStoredRole($role): Role |
||
290 | |||
291 | protected function convertPipeToArray(string $pipeString) |
||
312 | |||
313 | protected function getArgumentRoles($roles, $guard = null) |
||
314 | { |
||
315 | return array_map(function ($role) use ($guard) { |
||
316 | if ($role instanceof Role) { |
||
317 | return $role; |
||
326 | } |
||
327 |
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.