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:
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 | public function hasAnyPermission(...$permissions): bool |
||
143 | |||
144 | /** |
||
145 | * Determine if the model has, via roles, the given permission. |
||
146 | * |
||
147 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | protected function hasPermissionViaRole(Permission $permission): bool |
||
155 | |||
156 | /** |
||
157 | * Determine if the model has the given permission. |
||
158 | * |
||
159 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function hasDirectPermission($permission): bool |
||
181 | |||
182 | /** |
||
183 | * Return all the permissions the model has via roles. |
||
184 | */ |
||
185 | public function getPermissionsViaRoles(): Collection |
||
192 | |||
193 | /** |
||
194 | * Return all the permissions the model has, both directly and via roles. |
||
195 | */ |
||
196 | public function getAllPermissions(): Collection |
||
203 | |||
204 | /** |
||
205 | * Grant the given permission(s) to a role. |
||
206 | * |
||
207 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
208 | * |
||
209 | * @return $this |
||
210 | */ |
||
211 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
229 | |||
230 | /** |
||
231 | * Remove all current permissions and set the given ones. |
||
232 | * |
||
233 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function syncPermissions(...$permissions) |
||
243 | |||
244 | /** |
||
245 | * Revoke the given permission. |
||
246 | * |
||
247 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function revokePermissionTo($permission) |
||
259 | |||
260 | /** |
||
261 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
262 | * |
||
263 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
264 | */ |
||
265 | protected function getStoredPermission($permissions) |
||
284 | |||
285 | /** |
||
286 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
287 | * |
||
288 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
289 | */ |
||
290 | protected function ensureModelSharesGuard($roleOrPermission) |
||
296 | |||
297 | protected function getGuardNames(): Collection |
||
301 | |||
302 | protected function getDefaultGuardName(): string |
||
306 | |||
307 | /** |
||
308 | * Forget the cached permissions. |
||
309 | */ |
||
310 | public function forgetCachedPermissions() |
||
314 | } |
||
315 |
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.