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 |
||
17 | trait EntrustRoleTrait |
||
18 | { |
||
19 | //Big block of caching functionality. |
||
20 | View Code Duplication | public function cachedPermissions() |
|
33 | |||
34 | View Code Duplication | public function save(array $options = []) |
|
44 | |||
45 | View Code Duplication | public function delete(array $options = []) |
|
55 | |||
56 | View Code Duplication | public function restore() |
|
66 | |||
67 | /** |
||
68 | * Many-to-Many relations with the user model. |
||
69 | * |
||
70 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
71 | */ |
||
72 | public function users() |
||
79 | |||
80 | /** |
||
81 | * Many-to-Many relations with the permission model. |
||
82 | * Named "perms" for backwards compatibility. Also because "perms" is short |
||
83 | * and sweet. |
||
84 | * |
||
85 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
86 | */ |
||
87 | public function perms() |
||
94 | |||
95 | /** |
||
96 | * Boot the role model |
||
97 | * Attach event listener to remove the many-to-many records when trying to |
||
98 | * delete Will NOT delete any records if the role model uses soft deletes. |
||
99 | * |
||
100 | * @return void|bool |
||
101 | */ |
||
102 | public static function boot() |
||
117 | |||
118 | /** |
||
119 | * Checks if the role has a permission by its name. |
||
120 | * |
||
121 | * @param string|array $name Permission name or array of permission |
||
122 | * names. |
||
123 | * @param bool $requireAll All permissions in the array are |
||
124 | * required. |
||
125 | * |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function hasPermission($name, $requireAll = false) |
||
155 | |||
156 | /** |
||
157 | * Save the inputted permissions. |
||
158 | * |
||
159 | * @param mixed $inputPermissions |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | public function savePermissions($inputPermissions) |
||
175 | |||
176 | /** |
||
177 | * Attach permission to current role. |
||
178 | * |
||
179 | * @param object|array $permission |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | public function attachPermission($permission) |
||
195 | |||
196 | /** |
||
197 | * Detach permission from current role. |
||
198 | * |
||
199 | * @param object|array $permission |
||
200 | * |
||
201 | * @return void |
||
202 | */ |
||
203 | public function detachPermission($permission) |
||
215 | |||
216 | /** |
||
217 | * Attach multiple permissions to current role. |
||
218 | * |
||
219 | * @param mixed $permissions |
||
220 | * |
||
221 | * @return void |
||
222 | */ |
||
223 | public function attachPermissions($permissions) |
||
229 | |||
230 | /** |
||
231 | * Detach multiple permissions from current role |
||
232 | * |
||
233 | * @param mixed $permissions |
||
234 | * |
||
235 | * @return void |
||
236 | */ |
||
237 | public function detachPermissions($permissions = null) |
||
247 | } |
||
248 |
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.