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 HasPermissions 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 HasPermissions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait HasPermissions |
||
15 | { |
||
16 | private $permissionClass; |
||
17 | |||
18 | public static function bootHasPermissions() |
||
28 | |||
29 | public function getPermissionClass() |
||
37 | |||
38 | /** |
||
39 | * A model may have multiple direct permissions. |
||
40 | */ |
||
41 | public function permissions(): MorphToMany |
||
51 | |||
52 | /** |
||
53 | * Scope the model query to certain permissions only. |
||
54 | * |
||
55 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
56 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
57 | * |
||
58 | * @return \Illuminate\Database\Eloquent\Builder |
||
59 | */ |
||
60 | public function scopePermission(Builder $query, $permissions): Builder |
||
87 | |||
88 | /** |
||
89 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | protected function convertToPermissionModels($permissions): array |
||
109 | |||
110 | /** |
||
111 | * Determine if the model may perform the given permission. |
||
112 | * |
||
113 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
114 | * @param string|null $guardName |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function hasPermissionTo($permission, $guardName = null): bool |
||
142 | |||
143 | /** |
||
144 | * Determine if the model has any of the given permissions. |
||
145 | * |
||
146 | * @param array ...$permissions |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
164 | |||
165 | /** |
||
166 | * Determine if the model has all of the given permissions. |
||
167 | * |
||
168 | * @param array ...$permissions |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
186 | |||
187 | /** |
||
188 | * Determine if the model has, via roles, the given permission. |
||
189 | * |
||
190 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | protected function hasPermissionViaRole(Permission $permission): bool |
||
198 | |||
199 | /** |
||
200 | * Determine if the model has the given permission. |
||
201 | * |
||
202 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function hasDirectPermission($permission): bool |
||
230 | |||
231 | /** |
||
232 | * Return all the permissions the model has via roles. |
||
233 | */ |
||
234 | public function getPermissionsViaRoles(): Collection |
||
241 | |||
242 | /** |
||
243 | * Return all the permissions the model has, both directly and via roles. |
||
244 | */ |
||
245 | public function getAllPermissions(): Collection |
||
252 | |||
253 | /** |
||
254 | * Grant the given permission(s) to a role. |
||
255 | * |
||
256 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
257 | * |
||
258 | * @return $this |
||
259 | */ |
||
260 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
282 | |||
283 | /** |
||
284 | * Remove all current permissions and set the given ones. |
||
285 | * |
||
286 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
287 | * |
||
288 | * @return $this |
||
289 | */ |
||
290 | public function syncPermissions(...$permissions) |
||
296 | |||
297 | /** |
||
298 | * Revoke the given permission. |
||
299 | * |
||
300 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
301 | * |
||
302 | * @return $this |
||
303 | */ |
||
304 | public function revokePermissionTo($permission) |
||
312 | |||
313 | /** |
||
314 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
315 | * |
||
316 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
317 | */ |
||
318 | protected function getStoredPermission($permissions) |
||
339 | |||
340 | /** |
||
341 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
342 | * |
||
343 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
344 | */ |
||
345 | protected function ensureModelSharesGuard($roleOrPermission) |
||
351 | |||
352 | protected function getGuardNames(): Collection |
||
356 | |||
357 | protected function getDefaultGuardName(): string |
||
361 | |||
362 | /** |
||
363 | * Forget the cached permissions. |
||
364 | */ |
||
365 | public function forgetCachedPermissions() |
||
369 | } |
||
370 |
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.