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 | public static function bootHasPermissions() |
||
26 | |||
27 | /** |
||
28 | * A model may have multiple direct permissions. |
||
29 | */ |
||
30 | public function permissions(): MorphToMany |
||
40 | |||
41 | /** |
||
42 | * Scope the model query to certain permissions only. |
||
43 | * |
||
44 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
45 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
46 | * |
||
47 | * @return \Illuminate\Database\Eloquent\Builder |
||
48 | */ |
||
49 | public function scopePermission(Builder $query, $permissions): Builder |
||
76 | |||
77 | /** |
||
78 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | protected function convertToPermissionModels($permissions): array |
||
98 | |||
99 | /** |
||
100 | * Determine if the model may perform the given permission. |
||
101 | * |
||
102 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
103 | * @param string|null $guardName |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function hasPermissionTo($permission, $guardName = null): bool |
||
129 | |||
130 | /** |
||
131 | * Determine if the model has any of the given permissions. |
||
132 | * |
||
133 | * @param array ...$permissions |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
151 | |||
152 | /** |
||
153 | * Determine if the model has all of the given permissions. |
||
154 | * |
||
155 | * @param array ...$permissions |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
173 | |||
174 | /** |
||
175 | * Determine if the model has, via roles, the given permission. |
||
176 | * |
||
177 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | protected function hasPermissionViaRole(Permission $permission): bool |
||
185 | |||
186 | /** |
||
187 | * Determine if the model has the given permission. |
||
188 | * |
||
189 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function hasDirectPermission($permission): bool |
||
215 | |||
216 | /** |
||
217 | * Return all the permissions the model has via roles. |
||
218 | */ |
||
219 | public function getPermissionsViaRoles(): Collection |
||
226 | |||
227 | /** |
||
228 | * Return all the permissions the model has, both directly and via roles. |
||
229 | */ |
||
230 | public function getAllPermissions(): Collection |
||
237 | |||
238 | /** |
||
239 | * Grant the given permission(s) to a role. |
||
240 | * |
||
241 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
266 | |||
267 | /** |
||
268 | * Remove all current permissions and set the given ones. |
||
269 | * |
||
270 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function syncPermissions(...$permissions) |
||
280 | |||
281 | /** |
||
282 | * Revoke the given permission. |
||
283 | * |
||
284 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
285 | * |
||
286 | * @return $this |
||
287 | */ |
||
288 | public function revokePermissionTo($permission) |
||
296 | |||
297 | /** |
||
298 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
299 | * |
||
300 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
301 | */ |
||
302 | protected function getStoredPermission($permissions) |
||
321 | |||
322 | /** |
||
323 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
324 | * |
||
325 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
326 | */ |
||
327 | protected function ensureModelSharesGuard($roleOrPermission) |
||
333 | |||
334 | protected function getGuardNames(): Collection |
||
338 | |||
339 | protected function getDefaultGuardName(): string |
||
343 | |||
344 | /** |
||
345 | * Forget the cached permissions. |
||
346 | */ |
||
347 | public function forgetCachedPermissions() |
||
351 | } |
||
352 |
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.