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 | private $roleClass; |
||
16 | |||
17 | public static function bootHasRoles() |
||
18 | { |
||
19 | static::deleting(function ($model) { |
||
20 | if (method_exists($model, 'isForceDeleting') && !$model->isForceDeleting()) { |
||
21 | return; |
||
22 | } |
||
23 | |||
24 | $model->roles()->detach(); |
||
25 | }); |
||
26 | } |
||
27 | |||
28 | public function getRoleClass() |
||
36 | |||
37 | /** |
||
38 | * A model may have multiple roles. |
||
39 | */ |
||
40 | public function roles(): 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 | * @param string $guard |
||
57 | * |
||
58 | * @return \Illuminate\Database\Eloquent\Builder |
||
59 | */ |
||
60 | public function scopeRole(Builder $query, $listOfRoles, $guard = null): Builder |
||
100 | |||
101 | /** |
||
102 | * Assign the given role to the model. |
||
103 | * |
||
104 | * @param array|string|\Spatie\Permission\Contracts\Role ...$roles |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | View Code Duplication | public function assignRole(...$roles) |
|
153 | |||
154 | /** |
||
155 | * Revoke the given role from the model. |
||
156 | * |
||
157 | * @param string|\Spatie\Permission\Contracts\Role $role |
||
158 | */ |
||
159 | public function removeRole($role) |
||
169 | |||
170 | /** |
||
171 | * Remove all current roles and set the given ones. |
||
172 | * |
||
173 | * @param array|\Spatie\Permission\Contracts\Role|string ...$roles |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function syncRoles(...$roles) |
||
183 | |||
184 | /** |
||
185 | * Determine if the model has (one of) the given role(s). |
||
186 | * |
||
187 | * @param string|int|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
188 | * @param string|null $guard |
||
189 | * @return bool |
||
190 | */ |
||
191 | public function hasRole($roles, string $guard = null): bool |
||
225 | |||
226 | /** |
||
227 | * Determine if the model has any of the given role(s). |
||
228 | * |
||
229 | * @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function hasAnyRole($roles): bool |
||
237 | |||
238 | /** |
||
239 | * Determine if the model has all of the given role(s). |
||
240 | * |
||
241 | * @param string|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles |
||
242 | * @param string|null $guard |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function hasAllRoles($roles, string $guard = null): bool |
||
271 | |||
272 | /** |
||
273 | * Return all permissions directly coupled to the model. |
||
274 | */ |
||
275 | public function getDirectPermissions(): Collection |
||
279 | |||
280 | public function getRoleNames(): Collection |
||
284 | |||
285 | protected function getStoredRole($role): Role |
||
299 | |||
300 | protected function convertPipeToArray(string $pipeString) |
||
321 | } |
||
322 |
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.