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 |
||
13 | trait HasPermissions |
||
14 | { |
||
15 | public static function bootHasPermissions() |
||
25 | |||
26 | /** |
||
27 | * A model may have multiple direct permissions. |
||
28 | */ |
||
29 | public function permissions(): MorphToMany |
||
39 | |||
40 | /** |
||
41 | * Scope the model query to certain permissions only. |
||
42 | * |
||
43 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
44 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
45 | * |
||
46 | * @return \Illuminate\Database\Eloquent\Builder |
||
47 | */ |
||
48 | public function scopePermission(Builder $query, $permissions): Builder |
||
75 | |||
76 | /** |
||
77 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | protected function convertToPermissionModels($permissions): array |
||
97 | |||
98 | /** |
||
99 | * Determine if the model may perform the given permission. |
||
100 | * |
||
101 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
102 | * @param string|null $guardName |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function hasPermissionTo($permission, $guardName = null): bool |
||
121 | |||
122 | /** |
||
123 | * Determine if the model has any of the given permissions. |
||
124 | * |
||
125 | * @param array ...$permissions |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | View Code Duplication | public function hasAnyPermission(...$permissions): bool |
|
143 | |||
144 | /** |
||
145 | * Determine if the model has all of the given permissions. |
||
146 | * |
||
147 | * @param array ...$permissions |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | View Code Duplication | public function hasAllPermissions(...$permissions): bool |
|
165 | |||
166 | /** |
||
167 | * Determine if the model has, via roles, the given permission. |
||
168 | * |
||
169 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | protected function hasPermissionViaRole(Permission $permission): bool |
||
177 | |||
178 | /** |
||
179 | * Determine if the model has the given permission. |
||
180 | * |
||
181 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function hasDirectPermission($permission): bool |
||
203 | |||
204 | /** |
||
205 | * Return all the permissions the model has via roles. |
||
206 | */ |
||
207 | public function getPermissionsViaRoles(): Collection |
||
214 | |||
215 | /** |
||
216 | * Return all the permissions the model has, both directly and via roles. |
||
217 | */ |
||
218 | public function getAllPermissions(): Collection |
||
225 | |||
226 | /** |
||
227 | * Grant the given permission(s) to a role. |
||
228 | * |
||
229 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
251 | |||
252 | /** |
||
253 | * Remove all current permissions and set the given ones. |
||
254 | * |
||
255 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function syncPermissions(...$permissions) |
||
265 | |||
266 | /** |
||
267 | * Revoke the given permission. |
||
268 | * |
||
269 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
270 | * |
||
271 | * @return $this |
||
272 | */ |
||
273 | public function revokePermissionTo($permission) |
||
281 | |||
282 | /** |
||
283 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
284 | * |
||
285 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
286 | */ |
||
287 | protected function getStoredPermission($permissions) |
||
306 | |||
307 | /** |
||
308 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
309 | * |
||
310 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
311 | */ |
||
312 | protected function ensureModelSharesGuard($roleOrPermission) |
||
318 | |||
319 | protected function getGuardNames(): Collection |
||
323 | |||
324 | protected function getDefaultGuardName(): string |
||
328 | |||
329 | /** |
||
330 | * Forget the cached permissions. |
||
331 | */ |
||
332 | public function forgetCachedPermissions() |
||
336 | } |
||
337 |
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.