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 |
||
130 | |||
131 | /** |
||
132 | * Determine if the model has any of the given permissions. |
||
133 | * |
||
134 | * @param array ...$permissions |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
152 | |||
153 | /** |
||
154 | * Determine if the model has all of the given permissions. |
||
155 | * |
||
156 | * @param array ...$permissions |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
174 | |||
175 | /** |
||
176 | * Determine if the model has, via roles, the given permission. |
||
177 | * |
||
178 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | protected function hasPermissionViaRole(Permission $permission): bool |
||
186 | |||
187 | /** |
||
188 | * Determine if the model has the given permission. |
||
189 | * |
||
190 | * @param string|int|\Spatie\Permission\Contracts\Permission $permission |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function hasDirectPermission($permission): bool |
||
217 | |||
218 | /** |
||
219 | * Return all the permissions the model has via roles. |
||
220 | */ |
||
221 | public function getPermissionsViaRoles(): Collection |
||
228 | |||
229 | /** |
||
230 | * Return all the permissions the model has, both directly and via roles. |
||
231 | */ |
||
232 | public function getAllPermissions(): Collection |
||
239 | |||
240 | /** |
||
241 | * Grant the given permission(s) to a role. |
||
242 | * |
||
243 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
268 | |||
269 | /** |
||
270 | * Remove all current permissions and set the given ones. |
||
271 | * |
||
272 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
273 | * |
||
274 | * @return $this |
||
275 | */ |
||
276 | public function syncPermissions(...$permissions) |
||
282 | |||
283 | /** |
||
284 | * Revoke the given permission. |
||
285 | * |
||
286 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
287 | * |
||
288 | * @return $this |
||
289 | */ |
||
290 | public function revokePermissionTo($permission) |
||
298 | |||
299 | /** |
||
300 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
301 | * |
||
302 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
303 | */ |
||
304 | protected function getStoredPermission($permissions) |
||
323 | |||
324 | /** |
||
325 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
326 | * |
||
327 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
328 | */ |
||
329 | protected function ensureModelSharesGuard($roleOrPermission) |
||
335 | |||
336 | protected function getGuardNames(): Collection |
||
340 | |||
341 | protected function getDefaultGuardName(): string |
||
345 | |||
346 | /** |
||
347 | * Forget the cached permissions. |
||
348 | */ |
||
349 | public function forgetCachedPermissions() |
||
353 | } |
||
354 |
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.