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 | public function isLevel($level) |
|
| 25 | |||
| 26 | /** |
||
| 27 | * USER & LEVEL |
||
| 28 | * Check if a user has or has inherited |
||
| 29 | * a given level |
||
| 30 | * |
||
| 31 | * @param $level |
||
| 32 | * @return bool |
||
| 33 | */ |
||
| 34 | 1 | public function hasLevel($level) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * USER & LEVEL |
||
| 45 | * Add a single permission to a model |
||
| 46 | * |
||
| 47 | * @param $permission |
||
| 48 | */ |
||
| 49 | 15 | public function addPermission($permission) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * USER & LEVEL |
||
| 56 | * Add an array of permissions to a model |
||
| 57 | * |
||
| 58 | * @param $permissions |
||
| 59 | */ |
||
| 60 | 9 | public function addPermissions(array $permissions) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * USER & LEVEL |
||
| 67 | * Remove a permission from a model |
||
| 68 | * |
||
| 69 | * @param $permission |
||
| 70 | */ |
||
| 71 | 2 | public function removePermission($permission) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * USER & LEVEL |
||
| 78 | * Remove an array of permissions objects or permission names (can be mixed) |
||
| 79 | * |
||
| 80 | * @param $permissions |
||
| 81 | */ |
||
| 82 | 1 | public function removePermissions(array $permissions) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * USER & LEVEL |
||
| 89 | * Sync permissions on a model |
||
| 90 | * |
||
| 91 | * @param $permissions |
||
| 92 | */ |
||
| 93 | 2 | public function syncPermissions(array $permissions) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * USER & LEVEL |
||
| 100 | * Clear permissions from a model |
||
| 101 | * |
||
| 102 | * @return mixed |
||
| 103 | */ |
||
| 104 | 3 | public function clearPermissions() |
|
| 112 | |||
| 113 | |||
| 114 | /* ------------------------------------------------------------------------------------------------ |
||
| 115 | | Other Functions |
||
| 116 | | ------------------------------------------------------------------------------------------------ |
||
| 117 | */ |
||
| 118 | /** |
||
| 119 | * USER & LEVEL |
||
| 120 | * First try the cache to return the collection, |
||
| 121 | * then fetch it from the database. |
||
| 122 | * |
||
| 123 | * See @cacheGetAllPermissions() |
||
| 124 | * |
||
| 125 | * @return mixed |
||
| 126 | */ |
||
| 127 | 6 | View Code Duplication | public function getAllPermissions() |
| 139 | |||
| 140 | /** |
||
| 141 | * USER & LEVEL |
||
| 142 | * First try the cache to return the collection, |
||
| 143 | * then fetch it from the database. |
||
| 144 | * |
||
| 145 | * See @cacheGetInheritedPermissions() |
||
| 146 | * |
||
| 147 | * @return mixed |
||
| 148 | */ |
||
| 149 | 3 | View Code Duplication | public function getInheritedPermissions() |
| 161 | |||
| 162 | /** |
||
| 163 | * USER & LEVEL |
||
| 164 | * First try the cache to return the collection, |
||
| 165 | * then fetch it from the database. |
||
| 166 | * |
||
| 167 | * See @cacheGetAvailablePermissions() |
||
| 168 | * |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | 2 | View Code Duplication | public function getAvailablePermissions() |
| 183 | |||
| 184 | /** |
||
| 185 | * USER & LEVEL |
||
| 186 | * First try the cache to return the collection, |
||
| 187 | * then fetch it from the database. |
||
| 188 | * |
||
| 189 | * See @cacheHasPermissionTo() |
||
| 190 | * |
||
| 191 | * @param $permission |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | 2 | public function hasPermissionTo($permission) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * USER & LEVEL |
||
| 209 | * Returns if a level is lower ranking than another level, |
||
| 210 | * or if a user is lower ranking than another user. |
||
| 211 | * |
||
| 212 | * @param $other |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | 3 | public function isLowerThan($other) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * USER & LEVEL |
||
| 224 | * Returns if a level is higher ranking than another level, |
||
| 225 | * or if a user is higher ranking than another user. |
||
| 226 | * |
||
| 227 | * @param $other |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | 3 | public function isHigherThan($other) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * USER & LEVEL |
||
| 239 | * Returns if a equal is lower ranking than another level, |
||
| 240 | * or if a user is equal ranking than another user. |
||
| 241 | * |
||
| 242 | * @param $other |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | 1 | public function isEqualTo($other) |
|
| 251 | |||
| 252 | |||
| 253 | /* ------------------------------------------------------------------------------------------------ |
||
| 254 | | Other Functions |
||
| 255 | | ------------------------------------------------------------------------------------------------ |
||
| 256 | */ |
||
| 257 | /** |
||
| 258 | * USER & LEVEL |
||
| 259 | * Return a collection of all permissions |
||
| 260 | * associated to a user by direct or |
||
| 261 | * inheritance |
||
| 262 | * |
||
| 263 | * @return mixed |
||
| 264 | */ |
||
| 265 | 6 | protected function cacheGetAllPermissions() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * USER & LEVEL |
||
| 284 | * Return a collection of permissions that |
||
| 285 | * are inherited from a higher level |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | 3 | protected function cacheGetInheritedPermissions() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * USER & LEVEL |
||
| 309 | * Return a collection of permissions still able to be assigned |
||
| 310 | * ie, not already inherited or explicitly assigned |
||
| 311 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 312 | */ |
||
| 313 | 2 | protected function cacheGetAvailablePermissions() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * USER & LEVEL |
||
| 322 | * Check if a model has permission to do something |
||
| 323 | * |
||
| 324 | * @param $permission |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | 2 | protected function cacheHasPermissionTo($permission) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * USER & LEVEL |
||
| 345 | * Modify a given set of permissions, given the action to be taken |
||
| 346 | * |
||
| 347 | * @param $permissions |
||
| 348 | * @param $action |
||
| 349 | * @return mixed |
||
| 350 | */ |
||
| 351 | 23 | protected function modifyPermissions($permissions, $action) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * USER & LEVEL |
||
| 376 | * Helper function to build the array |
||
| 377 | * for modifyPermissions |
||
| 378 | * |
||
| 379 | * @param $permissions |
||
| 380 | * @param $action |
||
| 381 | * @param $model |
||
| 382 | * @return mixed |
||
| 383 | * @throws PermissionNotFoundException |
||
| 384 | */ |
||
| 385 | 23 | protected function buildPermissionsArray($permissions, $action, $model) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * USER & LEVEL |
||
| 407 | * Helper function to handle adding or negating permissions |
||
| 408 | * |
||
| 409 | * @param $model |
||
| 410 | * @param $permissionObjects |
||
| 411 | */ |
||
| 412 | 22 | protected function addOrNegate($model, $permissionObjects, $action) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * USER & LEVEL |
||
| 423 | * Helper function to handle removing permissions |
||
| 424 | * |
||
| 425 | * @param $model |
||
| 426 | * @param $permissionObjects |
||
| 427 | */ |
||
| 428 | 3 | View Code Duplication | protected function remove($model, $permissionObjects) |
| 439 | |||
| 440 | /** |
||
| 441 | * USER & LEVEL |
||
| 442 | * Help function to handle syncing permissions |
||
| 443 | * |
||
| 444 | * @param $model |
||
| 445 | * @param $permissionObjects |
||
| 446 | */ |
||
| 447 | 2 | View Code Duplication | protected function sync($model, $permissionObjects) |
| 461 | |||
| 462 | /** |
||
| 463 | * USER & LEVEL |
||
| 464 | * Helper function to get the permission whether it is the permission ID |
||
| 465 | * or the permission name, or the permission object itself. |
||
| 466 | * |
||
| 467 | * @param $permission |
||
| 468 | * @return mixed |
||
| 469 | * @throws PermissionNotFoundException |
||
| 470 | */ |
||
| 471 | 23 | View Code Duplication | protected function getPermission($permission) |
| 484 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.