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 |
||
8 | trait HasPermissions |
||
9 | { |
||
10 | /** |
||
11 | * Grant the given permission(s) to a role. |
||
12 | * |
||
13 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
14 | * |
||
15 | * @return $this |
||
16 | */ |
||
17 | View Code Duplication | public function givePermissionTo(...$permissions) |
|
|
|||
18 | { |
||
19 | $permissions = collect($permissions) |
||
20 | ->flatten() |
||
21 | ->map(function ($permission) { |
||
22 | return $this->getStoredPermission($permission); |
||
23 | }) |
||
24 | ->each(function ($permission) { |
||
25 | $this->ensureGuardIsEqual($permission); |
||
26 | }) |
||
27 | ->all(); |
||
28 | |||
29 | $this->permissions()->saveMany($permissions); |
||
30 | |||
31 | $this->forgetCachedPermissions(); |
||
32 | |||
33 | return $this; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Remove all current permissions and set the given ones. |
||
38 | * |
||
39 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
40 | * |
||
41 | * @return $this |
||
42 | */ |
||
43 | public function syncPermissions(...$permissions) |
||
44 | { |
||
45 | $this->permissions()->detach(); |
||
46 | |||
47 | return $this->givePermissionTo($permissions); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Revoke the given permission. |
||
52 | * |
||
53 | * @param \Spatie\Permission\Contracts\Permission|string $permission |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function revokePermissionTo($permission) |
||
65 | |||
66 | /** |
||
67 | * @param string|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions |
||
68 | * |
||
69 | * @return \Spatie\Permission\Contracts\Permission |
||
70 | */ |
||
71 | protected function getStoredPermission($permissions): Permission |
||
83 | |||
84 | /** |
||
85 | * @param \Spatie\Permission\Contracts\Permission|\Spatie\Permission\Contracts\Role $roleOrPermission |
||
86 | * |
||
87 | * @throws \Spatie\Permission\Exceptions\GuardMismatch |
||
88 | */ |
||
89 | protected function ensureGuardIsEqual($roleOrPermission) |
||
95 | |||
96 | /** |
||
97 | * Check if the class is assigned to a guard in `config/auth.php`. |
||
98 | */ |
||
99 | protected function isAssignedToGuard(): bool |
||
103 | |||
104 | /** |
||
105 | * Get the name of the guard that this class is assigned to based on it's `guard_name` property or the auth config |
||
106 | * file. |
||
107 | */ |
||
108 | protected function getGuardName(): string |
||
112 | |||
113 | /** |
||
114 | * Get an array of all models assigned to guards with the guard names as the keys. |
||
115 | */ |
||
116 | protected function getAllAuthGuardProviderModels(): array |
||
123 | } |
||
124 |
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.