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 |
||
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|\Spatie\Permission\Contracts\Permission $permission |
||
103 | * @param string|null $guardName |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function hasPermissionTo($permission, $guardName = null): bool |
||
122 | |||
123 | /** |
||
124 | * Determine if the model has any of the given permissions. |
||
125 | * |
||
126 | * @param array ...$permissions |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function hasAnyPermission(...$permissions): bool |
||
144 | |||
145 | /** |
||
146 | * Determine if the model has, via roles, the given permission. |
||
147 | * |
||
148 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | protected function hasPermissionViaRole(Permission $permission): bool |
||
156 | |||
157 | /** |
||
158 | * Determine if the model has the given permission. |
||
159 | * |
||
160 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function hasDirectPermission($permission): bool |
||
182 | |||
183 | /** |
||
184 | * Return all the permissions the model has via roles. |
||
185 | */ |
||
186 | public function getPermissionsViaRoles(): Collection |
||
193 | |||
194 | /** |
||
195 | * Return all the permissions the model has, both directly and via roles. |
||
196 | */ |
||
197 | public function getAllPermissions(): Collection |
||
204 | |||
205 | /** |
||
206 | * Grant the given permission(s) to a role. |
||
207 | * |
||
208 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
230 | |||
231 | /** |
||
232 | * Remove all current permissions and set the given ones. |
||
233 | * |
||
234 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | public function syncPermissions(...$permissions) |
||
244 | |||
245 | /** |
||
246 | * Revoke the given permission. |
||
247 | * |
||
248 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
249 | * |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function revokePermissionTo($permission) |
||
260 | |||
261 | /** |
||
262 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
263 | * |
||
264 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
265 | */ |
||
266 | protected function getStoredPermission($permissions) |
||
285 | |||
286 | /** |
||
287 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
288 | * |
||
289 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
290 | */ |
||
291 | protected function ensureModelSharesGuard($roleOrPermission) |
||
297 | |||
298 | protected function getGuardNames(): Collection |
||
302 | |||
303 | protected function getDefaultGuardName(): string |
||
307 | |||
308 | /** |
||
309 | * Forget the cached permissions. |
||
310 | */ |
||
311 | public function forgetCachedPermissions() |
||
315 | } |
||
316 |
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.