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 |
||
10 | trait HasPermissions |
||
11 | { |
||
12 | /** |
||
13 | * Grant the given permission(s) to a role. |
||
14 | * |
||
15 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
16 | * |
||
17 | * @return $this |
||
18 | */ |
||
19 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
37 | |||
38 | /** |
||
39 | * Remove all current permissions and set the given ones. |
||
40 | * |
||
41 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
42 | * |
||
43 | * @return $this |
||
44 | */ |
||
45 | public function syncPermissions(...$permissions) |
||
51 | |||
52 | /** |
||
53 | * Revoke the given permission. |
||
54 | * |
||
55 | * @param \Spatie\Permission\Contracts\Permission|string $permission |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function revokePermissionTo($permission) |
||
67 | |||
68 | /** |
||
69 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
70 | * |
||
71 | * @return \Spatie\Permission\Contracts\Permission |
||
72 | */ |
||
73 | protected function getStoredPermission($permissions): Permission |
||
88 | |||
89 | /** |
||
90 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
91 | * |
||
92 | * @throws \Spatie\Permission\Exceptions\GuardMismatch |
||
93 | */ |
||
94 | protected function ensureModelSharesGuard($roleOrPermission) |
||
100 | |||
101 | protected function getGuardNames(): Collection |
||
116 | |||
117 | protected function getDefaultGuardName(): string |
||
123 | |||
124 | /** |
||
125 | * Forget the cached permissions. |
||
126 | */ |
||
127 | public function forgetCachedPermissions() |
||
131 | } |
||
132 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.