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 | * @throws \Exception |
||
118 | */ |
||
119 | public function hasPermissionTo($permission, $guardName = null): bool |
||
136 | |||
137 | /** |
||
138 | * Check the uncached permissions for the model. |
||
139 | * |
||
140 | * @param $permission |
||
141 | * @param null $guardName |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function checkHasPermissionTo($permission, $guardName = null) |
||
169 | |||
170 | /** |
||
171 | * An alias to hasPermissionTo(), but avoids throwing an exception. |
||
172 | * |
||
173 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
174 | * @param string|null $guardName |
||
175 | * |
||
176 | * @return bool |
||
177 | * |
||
178 | * @throws \Exception |
||
179 | */ |
||
180 | public function checkPermissionTo($permission, $guardName = null): bool |
||
188 | |||
189 | /** |
||
190 | * Construct the key for the cache entry. |
||
191 | * |
||
192 | * @param array $config |
||
193 | * @param null|string|int|\Spatie\Permission\Contracts\Permission $permission |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | protected function getCacheKey(array $config, $permission = null) |
||
207 | |||
208 | /** |
||
209 | * Construct the tags for the cache entry. |
||
210 | * |
||
211 | * @param array $config |
||
212 | * @param null|string|int|\Spatie\Permission\Contracts\Permission $permission |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | protected function getCacheTags(array $config, $permission = null) |
||
229 | |||
230 | /** |
||
231 | * Get the configured key to cache the model by. |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | protected function getClassCacheString() |
||
239 | |||
240 | /** |
||
241 | * Get the configured key to cache the permission by. |
||
242 | * |
||
243 | * @param array $config |
||
244 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
245 | * |
||
246 | * @return mixed |
||
247 | */ |
||
248 | protected function getPermissionCacheString(array $config, $permission) |
||
256 | |||
257 | /** |
||
258 | * Determine if the model has any of the given permissions. |
||
259 | * |
||
260 | * @param array ...$permissions |
||
261 | * |
||
262 | * @return bool |
||
263 | * @throws \Exception |
||
264 | */ |
||
265 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
279 | |||
280 | /** |
||
281 | * Determine if the model has all of the given permissions. |
||
282 | * |
||
283 | * @param array ...$permissions |
||
284 | * |
||
285 | * @return bool |
||
286 | * @throws \Exception |
||
287 | */ |
||
288 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
302 | |||
303 | /** |
||
304 | * Determine if the model has, via roles, the given permission. |
||
305 | * |
||
306 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | protected function hasPermissionViaRole(Permission $permission): bool |
||
314 | |||
315 | /** |
||
316 | * Determine if the model has the given permission. |
||
317 | * |
||
318 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function hasDirectPermission($permission): bool |
||
346 | |||
347 | /** |
||
348 | * Return all the permissions the model has via roles. |
||
349 | */ |
||
350 | public function getPermissionsViaRoles(): Collection |
||
357 | |||
358 | /** |
||
359 | * Return all the permissions the model has, both directly and via roles. |
||
360 | */ |
||
361 | public function getAllPermissions(): Collection |
||
386 | |||
387 | /** |
||
388 | * Grant the given permission(s) to a role. |
||
389 | * |
||
390 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
391 | * |
||
392 | * @return $this |
||
393 | */ |
||
394 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
429 | |||
430 | /** |
||
431 | * Remove all current permissions and set the given ones. |
||
432 | * |
||
433 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
434 | * |
||
435 | * @return $this |
||
436 | */ |
||
437 | public function syncPermissions(...$permissions) |
||
443 | |||
444 | /** |
||
445 | * Revoke the given permission. |
||
446 | * |
||
447 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
448 | * |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function revokePermissionTo($permission) |
||
461 | |||
462 | /** |
||
463 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
464 | * |
||
465 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
466 | */ |
||
467 | protected function getStoredPermission($permissions) |
||
488 | |||
489 | /** |
||
490 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
491 | * |
||
492 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
493 | */ |
||
494 | protected function ensureModelSharesGuard($roleOrPermission) |
||
500 | |||
501 | protected function getGuardNames(): Collection |
||
505 | |||
506 | protected function getDefaultGuardName(): string |
||
510 | |||
511 | /** |
||
512 | * Forget the cached permissions. |
||
513 | */ |
||
514 | public function forgetCachedPermissions() |
||
518 | } |
||
519 |
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.