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($listOfPermissions): array |
||
127 | |||
128 | /** |
||
129 | * Determine if the model may perform the given permission. |
||
130 | * |
||
131 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
132 | * @param string|null $guardName |
||
133 | * |
||
134 | * @return bool |
||
135 | * @throws PermissionDoesNotExist |
||
136 | */ |
||
137 | public function hasPermissionTo($permission, $guardName = null): bool |
||
161 | |||
162 | /** |
||
163 | * @deprecated since 2.35.0 |
||
164 | * @alias of hasPermissionTo() |
||
165 | */ |
||
166 | public function hasUncachedPermissionTo($permission, $guardName = null): bool |
||
170 | |||
171 | /** |
||
172 | * An alias to hasPermissionTo(), but avoids throwing an exception. |
||
173 | * |
||
174 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
175 | * @param string|null $guardName |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function checkPermissionTo($permission, $guardName = null): bool |
||
187 | |||
188 | /** |
||
189 | * Determine if the model has any of the given permissions. |
||
190 | * |
||
191 | * @param array ...$permissions |
||
192 | * |
||
193 | * @return bool |
||
194 | * @throws \Exception |
||
195 | */ |
||
196 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
210 | |||
211 | /** |
||
212 | * Determine if the model has all of the given permissions. |
||
213 | * |
||
214 | * @param array ...$permissions |
||
215 | * |
||
216 | * @return bool |
||
217 | * @throws \Exception |
||
218 | */ |
||
219 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
233 | |||
234 | /** |
||
235 | * Determine if the model has, via roles, the given permission. |
||
236 | * |
||
237 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | protected function hasPermissionViaRole(Permission $permission): bool |
||
245 | |||
246 | /** |
||
247 | * Determine if the model has the given permission. |
||
248 | * |
||
249 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
250 | * |
||
251 | * @return bool |
||
252 | * @throws PermissionDoesNotExist |
||
253 | */ |
||
254 | public function hasDirectPermission($permission): bool |
||
272 | |||
273 | /** |
||
274 | * Return all the permissions the model has via roles. |
||
275 | */ |
||
276 | public function getPermissionsViaRoles(): Collection |
||
283 | |||
284 | /** |
||
285 | * Return all the permissions the model has, both directly and via roles. |
||
286 | * |
||
287 | * @throws \Exception |
||
288 | */ |
||
289 | public function getAllPermissions(): Collection |
||
299 | |||
300 | /** |
||
301 | * Grant the given permission(s) to a role. |
||
302 | * |
||
303 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
304 | * |
||
305 | * @return $this |
||
306 | */ |
||
307 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
352 | |||
353 | /** |
||
354 | * Remove all current permissions and set the given ones. |
||
355 | * |
||
356 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
357 | * |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function syncPermissions(...$permissions) |
||
366 | |||
367 | /** |
||
368 | * Revoke the given permission. |
||
369 | * |
||
370 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
371 | * |
||
372 | * @return $this |
||
373 | */ |
||
374 | public function revokePermissionTo($permission) |
||
384 | |||
385 | public function getPermissionNames(): Collection |
||
389 | |||
390 | /** |
||
391 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
392 | * |
||
393 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
394 | */ |
||
395 | protected function getStoredPermission($permissions) |
||
416 | |||
417 | /** |
||
418 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
419 | * |
||
420 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
421 | */ |
||
422 | protected function ensureModelSharesGuard($roleOrPermission) |
||
428 | |||
429 | protected function getGuardNames(): Collection |
||
433 | |||
434 | protected function getDefaultGuardName(): string |
||
438 | |||
439 | /** |
||
440 | * Forget the cached permissions. |
||
441 | */ |
||
442 | public function forgetCachedPermissions() |
||
446 | } |
||
447 |
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.