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 |
||
16 | trait RoleTrait |
||
17 | { |
||
18 | |||
19 | View Code Duplication | public function cachedPermissions() |
|
27 | |||
28 | View Code Duplication | public static function bootEntrustRoleTrait() |
|
45 | |||
46 | /** |
||
47 | * Many-to-Many relations with the user model. |
||
48 | * |
||
49 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
50 | */ |
||
51 | public function users() |
||
56 | |||
57 | /** |
||
58 | * Many-to-Many relations with the permission model. |
||
59 | * Named "perms" for backwards compatibility. Also because "perms" is short and sweet. |
||
60 | * |
||
61 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
62 | */ |
||
63 | public function perms() |
||
67 | |||
68 | /** |
||
69 | * Boot the role model |
||
70 | * Attach event listener to remove the many-to-many records when trying to delete |
||
71 | * Will NOT delete any records if the role model uses soft deletes. |
||
72 | * |
||
73 | * @return void|bool |
||
74 | */ |
||
75 | public static function boot() |
||
88 | |||
89 | /** |
||
90 | * Checks if the role has a permission by its name. |
||
91 | * |
||
92 | * @param string|array $name Permission name or array of permission names. |
||
93 | * @param bool $requireAll All permissions in the array are required. |
||
94 | * @return bool |
||
95 | */ |
||
96 | View Code Duplication | public function hasPermission($name, $requireAll = false) |
|
123 | |||
124 | /** |
||
125 | * Save the inputted permissions. |
||
126 | * |
||
127 | * @param mixed $inputPermissions |
||
128 | * @return void |
||
129 | */ |
||
130 | public function savePermissions($inputPermissions) |
||
138 | |||
139 | /** |
||
140 | * Attach permission to current role. |
||
141 | * |
||
142 | * @param object|array $permission |
||
143 | * @return void |
||
144 | */ |
||
145 | View Code Duplication | public function attachPermission($permission) |
|
157 | |||
158 | /** |
||
159 | * Detach permission from current role. |
||
160 | * |
||
161 | * @param object|array $permission |
||
162 | * @return void |
||
163 | */ |
||
164 | View Code Duplication | public function detachPermission($permission) |
|
176 | |||
177 | /** |
||
178 | * Attach multiple permissions to current role. |
||
179 | * |
||
180 | * @param mixed $permissions |
||
181 | * @return void |
||
182 | */ |
||
183 | public function attachPermissions($permissions) |
||
189 | |||
190 | /** |
||
191 | * Detach multiple permissions from current role |
||
192 | * |
||
193 | * @param mixed $permissions |
||
194 | * @return void |
||
195 | */ |
||
196 | public function detachPermissions($permissions) |
||
202 | } |
||
203 |
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.