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 |
||
118 | |||
119 | /** |
||
120 | * Determine if the model has any of the given permissions. |
||
121 | * |
||
122 | * @param array ...$permissions |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function hasAnyPermission(...$permissions): bool |
||
140 | |||
141 | /** |
||
142 | * Determine if the model has, via roles, the given permission. |
||
143 | * |
||
144 | * @param \Spatie\Permission\Contracts\Permission $permission |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | protected function hasPermissionViaRole(Permission $permission): bool |
||
152 | |||
153 | /** |
||
154 | * Determine if the model has the given permission. |
||
155 | * |
||
156 | * @param string|\Spatie\Permission\Contracts\Permission $permission |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function hasDirectPermission($permission): bool |
||
172 | |||
173 | /** |
||
174 | * Return all the permissions the model has via roles. |
||
175 | */ |
||
176 | public function getPermissionsViaRoles(): Collection |
||
183 | |||
184 | /** |
||
185 | * Return all the permissions the model has, both directly and via roles. |
||
186 | */ |
||
187 | public function getAllPermissions(): Collection |
||
194 | |||
195 | /** |
||
196 | * Grant the given permission(s) to a role. |
||
197 | * |
||
198 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
220 | |||
221 | /** |
||
222 | * Remove all current permissions and set the given ones. |
||
223 | * |
||
224 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function syncPermissions(...$permissions) |
||
234 | |||
235 | /** |
||
236 | * Revoke the given permission. |
||
237 | * |
||
238 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|string|string[] $permission |
||
239 | * |
||
240 | * @return $this |
||
241 | */ |
||
242 | public function revokePermissionTo($permission) |
||
250 | |||
251 | /** |
||
252 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
253 | * |
||
254 | * @return \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Permission[]|\Illuminate\Support\Collection |
||
255 | */ |
||
256 | protected function getStoredPermission($permissions) |
||
275 | |||
276 | /** |
||
277 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
278 | * |
||
279 | * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch |
||
280 | */ |
||
281 | protected function ensureModelSharesGuard($roleOrPermission) |
||
287 | |||
288 | protected function getGuardNames(): Collection |
||
292 | |||
293 | protected function getDefaultGuardName(): string |
||
297 | |||
298 | /** |
||
299 | * Forget the cached permissions. |
||
300 | */ |
||
301 | public function forgetCachedPermissions() |
||
305 | } |
||
306 |
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.