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:
Complex classes like UserAndLevel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserAndLevel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | trait UserAndLevel |
||
11 | { |
||
12 | /** |
||
13 | * USER & LEVEL |
||
14 | * Check if a model has a given level |
||
15 | * |
||
16 | * @param $level |
||
17 | * @return bool |
||
18 | */ |
||
19 | 1 | View Code Duplication | public function isLevel($level) |
27 | |||
28 | /** |
||
29 | * USER & LEVEL |
||
30 | * Check if a user has or has inherited |
||
31 | * a given level |
||
32 | * |
||
33 | * @param $level |
||
34 | * @return bool |
||
35 | */ |
||
36 | 1 | View Code Duplication | public function hasLevel($level) |
44 | |||
45 | /** |
||
46 | * USER & LEVEL |
||
47 | * Add a single permission to a model |
||
48 | * |
||
49 | * @param $permission |
||
50 | */ |
||
51 | 15 | public function addPermission($permission) |
|
55 | |||
56 | /** |
||
57 | * USER & LEVEL |
||
58 | * Add an array of permissions to a model |
||
59 | * |
||
60 | * @param $permissions |
||
61 | */ |
||
62 | 7 | public function addPermissions(array $permissions) |
|
66 | |||
67 | /** |
||
68 | * USER & LEVEL |
||
69 | * Remove a permission from a model |
||
70 | * |
||
71 | * @param $permission |
||
72 | */ |
||
73 | 2 | public function removePermission($permission) |
|
77 | |||
78 | /** |
||
79 | * USER & LEVEL |
||
80 | * Remove an array of permissions objects or permission names (can be mixed) |
||
81 | * |
||
82 | * @param $permissions |
||
83 | */ |
||
84 | 1 | public function removePermissions(array $permissions) |
|
88 | |||
89 | /** |
||
90 | * USER & LEVEL |
||
91 | * Sync permissions on a model |
||
92 | * |
||
93 | * @param $permissions |
||
94 | */ |
||
95 | 2 | public function syncPermissions(array $permissions) |
|
99 | |||
100 | /** |
||
101 | * USER & LEVEL |
||
102 | * Clear permissions from a model |
||
103 | * |
||
104 | * @return mixed |
||
105 | */ |
||
106 | 3 | public function clearPermissions() |
|
114 | |||
115 | |||
116 | /* ------------------------------------------------------------------------------------------------ |
||
117 | | Other Functions |
||
118 | | ------------------------------------------------------------------------------------------------ |
||
119 | */ |
||
120 | /** |
||
121 | * USER & LEVEL |
||
122 | * First try the cache to return the collection, |
||
123 | * then fetch it from the database. |
||
124 | * |
||
125 | * See @cacheGetAllPermissions() |
||
126 | * |
||
127 | * @return mixed |
||
128 | */ |
||
129 | 6 | View Code Duplication | public function getAllPermissions() |
141 | |||
142 | /** |
||
143 | * USER & LEVEL |
||
144 | * First try the cache to return the collection, |
||
145 | * then fetch it from the database. |
||
146 | * |
||
147 | * See @cacheGetInheritedPermissions() |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | 3 | View Code Duplication | public function getInheritedPermissions() |
163 | |||
164 | /** |
||
165 | * USER & LEVEL |
||
166 | * First try the cache to return the collection, |
||
167 | * then fetch it from the database. |
||
168 | * |
||
169 | * See @cacheGetAvailablePermissions() |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | 2 | View Code Duplication | public function getAvailablePermissions() |
185 | |||
186 | /** |
||
187 | * USER & LEVEL |
||
188 | * First try the cache to return the collection, |
||
189 | * then fetch it from the database. |
||
190 | * |
||
191 | * See @cacheHasPermissionTo() |
||
192 | * |
||
193 | * @param $permission |
||
194 | * @return mixed |
||
195 | */ |
||
196 | 2 | public function hasPermissionTo($permission) |
|
208 | |||
209 | /** |
||
210 | * USER & LEVEL |
||
211 | * Returns if a level is lower ranking than another level, |
||
212 | * or if a user is lower ranking than another user. |
||
213 | * |
||
214 | * @param $other |
||
215 | * @return bool |
||
216 | */ |
||
217 | 3 | public function isLowerThan($other) |
|
223 | |||
224 | /** |
||
225 | * USER & LEVEL |
||
226 | * Returns if a level is higher ranking than another level, |
||
227 | * or if a user is higher ranking than another user. |
||
228 | * |
||
229 | * @param $other |
||
230 | * @return bool |
||
231 | */ |
||
232 | 3 | public function isHigherThan($other) |
|
238 | |||
239 | /** |
||
240 | * USER & LEVEL |
||
241 | * Returns if a equal is lower ranking than another level, |
||
242 | * or if a user is equal ranking than another user. |
||
243 | * |
||
244 | * @param $other |
||
245 | * @return bool |
||
246 | */ |
||
247 | 1 | public function isEqualTo($other) |
|
253 | |||
254 | |||
255 | /* ------------------------------------------------------------------------------------------------ |
||
256 | | Other Functions |
||
257 | | ------------------------------------------------------------------------------------------------ |
||
258 | */ |
||
259 | /** |
||
260 | * USER & LEVEL |
||
261 | * Return a collection of all permissions |
||
262 | * associated to a user by direct or |
||
263 | * inheritance |
||
264 | * |
||
265 | * @return mixed |
||
266 | */ |
||
267 | 6 | protected function cacheGetAllPermissions() |
|
283 | |||
284 | /** |
||
285 | * USER & LEVEL |
||
286 | * Return a collection of permissions that |
||
287 | * are inherited from a higher level |
||
288 | * @return mixed |
||
289 | */ |
||
290 | 3 | protected function cacheGetInheritedPermissions() |
|
308 | |||
309 | /** |
||
310 | * USER & LEVEL |
||
311 | * Return a collection of permissions still able to be assigned |
||
312 | * ie, not already inherited or explicitly assigned |
||
313 | * @return \Illuminate\Database\Eloquent\Collection |
||
314 | */ |
||
315 | 2 | protected function cacheGetAvailablePermissions() |
|
321 | |||
322 | /** |
||
323 | * USER & LEVEL |
||
324 | * Check if a model has permission to do something |
||
325 | * |
||
326 | * @param $permission |
||
327 | * @return bool |
||
328 | */ |
||
329 | 2 | protected function cacheHasPermissionTo($permission) |
|
344 | |||
345 | /** |
||
346 | * USER & LEVEL |
||
347 | * Modify a given set of permissions, given the action to be taken |
||
348 | * |
||
349 | * @param $permissions |
||
350 | * @param $action |
||
351 | * @return mixed |
||
352 | */ |
||
353 | 21 | protected function modifyPermissions($permissions, $action) |
|
375 | |||
376 | /** |
||
377 | * USER & LEVEL |
||
378 | * Helper function to build the array |
||
379 | * for modifyPermissions |
||
380 | * |
||
381 | * @param $permissions |
||
382 | * @param $action |
||
383 | * @param $model |
||
384 | * @return mixed |
||
385 | * @throws PermissionNotFoundException |
||
386 | */ |
||
387 | 21 | protected function buildPermissionsArray($permissions, $action, $model) |
|
406 | |||
407 | /** |
||
408 | * USER & LEVEL |
||
409 | * Helper function to handle adding or negating permissions |
||
410 | * |
||
411 | * @param $model |
||
412 | * @param $permissionObjects |
||
413 | */ |
||
414 | 20 | protected function addOrNegate($model, $permissionObjects, $action) |
|
422 | |||
423 | /** |
||
424 | * USER & LEVEL |
||
425 | * Helper function to handle removing permissions |
||
426 | * |
||
427 | * @param $model |
||
428 | * @param $permissionObjects |
||
429 | */ |
||
430 | 3 | View Code Duplication | protected function remove($model, $permissionObjects) |
441 | |||
442 | /** |
||
443 | * USER & LEVEL |
||
444 | * Help function to handle syncing permissions |
||
445 | * |
||
446 | * @param $model |
||
447 | * @param $permissionObjects |
||
448 | */ |
||
449 | 2 | View Code Duplication | protected function sync($model, $permissionObjects) |
463 | |||
464 | /** |
||
465 | * USER & LEVEL |
||
466 | * Helper function to get the permission whether it is the permission ID |
||
467 | * or the permission name, or the permission object itself. |
||
468 | * |
||
469 | * @param $permission |
||
470 | * @return mixed |
||
471 | * @throws PermissionNotFoundException |
||
472 | */ |
||
473 | 21 | View Code Duplication | protected function getPermission($permission) |
486 | } |
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.