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 |
||
| 14 | trait UserAndLevel |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * USER & LEVEL |
||
| 18 | * Check if a model has a given level |
||
| 19 | * |
||
| 20 | * @param $level |
||
| 21 | * @return bool |
||
| 22 | */ |
||
| 23 | public function isLevel($level) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * USER & LEVEL |
||
| 32 | * Check if a user has or has inherited |
||
| 33 | * a given level |
||
| 34 | * |
||
| 35 | * @param $level |
||
| 36 | * @return bool |
||
| 37 | */ |
||
| 38 | public function hasLevel($level) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * USER & LEVEL |
||
| 47 | * Add a single permission to a model |
||
| 48 | * |
||
| 49 | * @param $permission |
||
| 50 | */ |
||
| 51 | public function addPermission($permission) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * USER & LEVEL |
||
| 58 | * Add an array of permissions to a model |
||
| 59 | * |
||
| 60 | * @param $permissions |
||
| 61 | */ |
||
| 62 | public function addPermissions(array $permissions) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * USER & LEVEL |
||
| 69 | * Remove a permission from a model |
||
| 70 | * |
||
| 71 | * @param $permission |
||
| 72 | */ |
||
| 73 | 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 | public function removePermissions(array $permissions) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * USER & LEVEL |
||
| 91 | * Sync permissions on a model |
||
| 92 | * |
||
| 93 | * @param $permissions |
||
| 94 | */ |
||
| 95 | public function syncPermissions(array $permissions) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * USER & LEVEL |
||
| 102 | * Clear permissions from a model |
||
| 103 | * |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | 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 | 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 | 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 | 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 | 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 | 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 | public function isHigherThan($other) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * USER & LEVEL |
||
| 237 | * Returns if a equal is lower ranking than another level, |
||
| 238 | * or if a user is equal ranking than another user. |
||
| 239 | * |
||
| 240 | * @param $other |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function isEqualTo($other) |
||
| 247 | |||
| 248 | |||
| 249 | /* ------------------------------------------------------------------------------------------------ |
||
| 250 | | Other Functions |
||
| 251 | | ------------------------------------------------------------------------------------------------ |
||
| 252 | */ |
||
| 253 | /** |
||
| 254 | * USER & LEVEL |
||
| 255 | * Return a collection of all permissions |
||
| 256 | * associated to a user by direct or |
||
| 257 | * inheritance |
||
| 258 | * |
||
| 259 | * @return mixed |
||
| 260 | */ |
||
| 261 | protected function cacheGetAllPermissions() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * USER & LEVEL |
||
| 278 | * Return a collection of permissions that |
||
| 279 | * are inherited from a higher level |
||
| 280 | * @return mixed |
||
| 281 | */ |
||
| 282 | protected function cacheGetInheritedPermissions() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * USER & LEVEL |
||
| 299 | * Return a collection of permissions still able to be assigned |
||
| 300 | * ie, not already inherited or explicitly assigned |
||
| 301 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 302 | */ |
||
| 303 | protected function cacheGetAvailablePermissions() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * USER & LEVEL |
||
| 312 | * Check if a model has permission to do something |
||
| 313 | * |
||
| 314 | * @param $permission |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | protected function cacheHasPermissionTo($permission) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * USER & LEVEL |
||
| 335 | * Modify a given set of permissions, given the action to be taken |
||
| 336 | * |
||
| 337 | * @param $permissions |
||
| 338 | * @param $action |
||
| 339 | * @return mixed |
||
| 340 | */ |
||
| 341 | protected function modifyPermissions($permissions, $action) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * USER & LEVEL |
||
| 366 | * Helper function to build the array |
||
| 367 | * for modifyPermissions |
||
| 368 | * |
||
| 369 | * @param $permissions |
||
| 370 | * @param $action |
||
| 371 | * @param $model |
||
| 372 | * @return mixed |
||
| 373 | * @throws PermissionNotFoundException |
||
| 374 | */ |
||
| 375 | protected function buildPermissionsArray($permissions, $action, $model) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * USER & LEVEL |
||
| 397 | * Helper function to handle adding or negating permissions |
||
| 398 | * |
||
| 399 | * @param $model |
||
| 400 | * @param $permissionObjects |
||
| 401 | */ |
||
| 402 | protected function addOrNegate($model, $permissionObjects, $action) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * USER & LEVEL |
||
| 413 | * Helper function to handle removing permissions |
||
| 414 | * |
||
| 415 | * @param $model |
||
| 416 | * @param $permissionObjects |
||
| 417 | */ |
||
| 418 | View Code Duplication | protected function remove($model, $permissionObjects) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * USER & LEVEL |
||
| 432 | * Help function to handle syncing permissions |
||
| 433 | * |
||
| 434 | * @param $model |
||
| 435 | * @param $permissionObjects |
||
| 436 | */ |
||
| 437 | View Code Duplication | protected function sync($model, $permissionObjects) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * USER & LEVEL |
||
| 454 | * Helper function to get the permission whether it is the permission ID |
||
| 455 | * or the permission name, or the permission object itself. |
||
| 456 | * |
||
| 457 | * @param $permission |
||
| 458 | * @return mixed |
||
| 459 | * @throws PermissionNotFoundException |
||
| 460 | */ |
||
| 461 | View Code Duplication | protected function getPermission($permission) |
|
| 474 | } |
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.