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  | 
            ||
| 12 | trait UserAndLevel  | 
            ||
| 13 | { | 
            ||
| 14 | /**  | 
            ||
| 15 | * USER & LEVEL  | 
            ||
| 16 | * Check if a model has a given level  | 
            ||
| 17 | *  | 
            ||
| 18 | * @param mixed level  | 
            ||
| 19 | * @return bool  | 
            ||
| 20 | */  | 
            ||
| 21 | 1 | public function isLevel($level)  | 
            |
| 22 |     { | 
            ||
| 23 | 1 | $level = $this->getALevel($level);  | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 24 | |||
| 25 | 1 |         return is_a($this, config('laravel-acl.level'), true) ? $this->name == $level->name : $this->level->name == $level->name; | 
            |
| 26 | }  | 
            ||
| 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 | public function hasLevel($level)  | 
            |
| 37 |     { | 
            ||
| 38 | 1 | $level = $this->getLevel($level);  | 
            |
| 39 | |||
| 40 | 1 |         return is_a($this, config('laravel-acl.level'), true) ? $this->rank <= $level->rank : $this->level->rank <= $level->rank; | 
            |
| 41 | }  | 
            ||
| 42 | |||
| 43 | /**  | 
            ||
| 44 | * USER & LEVEL  | 
            ||
| 45 | * Add a single permission to a model  | 
            ||
| 46 | *  | 
            ||
| 47 | * @param $permission  | 
            ||
| 48 | */  | 
            ||
| 49 | 16 | 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 | 3 | public function clearPermissions()  | 
            |
| 112 | |||
| 113 | /**  | 
            ||
| 114 | * USER & LEVEL  | 
            ||
| 115 | * First try the cache to return the collection,  | 
            ||
| 116 | * then fetch it from the database.  | 
            ||
| 117 | *  | 
            ||
| 118 | * See @cacheGetPermissions()  | 
            ||
| 119 | *  | 
            ||
| 120 | * @return \Illuminate\Support\Collection  | 
            ||
| 121 | */  | 
            ||
| 122 | 2 | View Code Duplication | public function getPermissions()  | 
            
| 123 |     { | 
            ||
| 124 | 2 | return Cache::remember(  | 
            |
| 125 | 2 | 'laravel-acl.getPermissionsFor' . (new ReflectionClass($this))->getShortName() . '_' . $this->id,  | 
            |
| 126 | 2 |             config('laravel-acl.cacheMinutes'), | 
            |
| 127 |             function() { | 
            ||
| 128 | 2 | return $this->cachegetPermissions();  | 
            |
| 129 | 2 | }  | 
            |
| 130 | );  | 
            ||
| 131 | }  | 
            ||
| 132 | |||
| 133 | /**  | 
            ||
| 134 | * USER & LEVEL  | 
            ||
| 135 | * First try the cache to return the collection,  | 
            ||
| 136 | * then fetch it from the database.  | 
            ||
| 137 | *  | 
            ||
| 138 | * See @cacheGetAllPermissions()  | 
            ||
| 139 | *  | 
            ||
| 140 | * @return \Illuminate\Support\Collection  | 
            ||
| 141 | */  | 
            ||
| 142 | 7 | View Code Duplication | public function getAllPermissions()  | 
            
| 143 |     { | 
            ||
| 144 | 7 | return Cache::remember(  | 
            |
| 145 | 7 | 'laravel-acl.getAllPermissionsFor' . (new ReflectionClass($this))->getShortName() . '_' . $this->id,  | 
            |
| 146 | 7 |             config('laravel-acl.cacheMinutes'), | 
            |
| 147 |             function() { | 
            ||
| 148 | 7 | return $this->cacheGetAllPermissions();  | 
            |
| 149 | 7 | }  | 
            |
| 150 | );  | 
            ||
| 151 | }  | 
            ||
| 152 | |||
| 153 | /**  | 
            ||
| 154 | * USER & LEVEL  | 
            ||
| 155 | * First try the cache to return the collection,  | 
            ||
| 156 | * then fetch it from the database.  | 
            ||
| 157 | *  | 
            ||
| 158 | * See @cacheGetInheritedPermissions()  | 
            ||
| 159 | *  | 
            ||
| 160 | * @return \Illuminate\Support\Collection  | 
            ||
| 161 | */  | 
            ||
| 162 | 3 | View Code Duplication | public function getInheritedPermissions()  | 
            
| 163 |     { | 
            ||
| 164 | 3 | return Cache::remember(  | 
            |
| 165 | 3 | 'laravel-acl.getInheritedPermissionsFor' . (new ReflectionClass($this))->getShortName() . '_' . $this->id,  | 
            |
| 166 | 3 |             config('laravel-acl.cacheMinutes'), | 
            |
| 167 |             function() { | 
            ||
| 168 | 3 | return $this->cacheGetInheritedPermissions();  | 
            |
| 169 | 3 | }  | 
            |
| 170 | );  | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 173 | /**  | 
            ||
| 174 | * USER & LEVEL  | 
            ||
| 175 | * First try the cache to return the collection,  | 
            ||
| 176 | * then fetch it from the database.  | 
            ||
| 177 | *  | 
            ||
| 178 | * See @cacheGetAvailablePermissions()  | 
            ||
| 179 | *  | 
            ||
| 180 | * @return \Illuminate\Support\Collection  | 
            ||
| 181 | */  | 
            ||
| 182 | 2 | View Code Duplication | public function getAvailablePermissions()  | 
            
| 183 |     { | 
            ||
| 184 | 2 | return Cache::remember(  | 
            |
| 185 | 2 | 'laravel-acl.getAvailablePermissionsFor' . (new ReflectionClass($this))->getShortName() . '_' . $this->id,  | 
            |
| 186 | 2 |             config('laravel-acl.cacheMinutes'), | 
            |
| 187 |             function() { | 
            ||
| 188 | 2 | return $this->cacheGetAvailablePermissions();  | 
            |
| 189 | 2 | }  | 
            |
| 190 | );  | 
            ||
| 191 | }  | 
            ||
| 192 | |||
| 193 | /**  | 
            ||
| 194 | * USER & LEVEL  | 
            ||
| 195 | * First try the cache to return the collection,  | 
            ||
| 196 | * then fetch it from the database.  | 
            ||
| 197 | *  | 
            ||
| 198 | * See @cacheHasPermissionTo()  | 
            ||
| 199 | *  | 
            ||
| 200 | * @param $permission  | 
            ||
| 201 | * @return bool  | 
            ||
| 202 | */  | 
            ||
| 203 | 3 | public function hasPermissionTo($permission)  | 
            |
| 204 |     { | 
            ||
| 205 | 3 | return Cache::remember(  | 
            |
| 206 | 3 | 'laravel-acl.hasPermissionTo_' . (new ReflectionClass($this))->getShortName() . '_' . $this->id . '_Permission_' . $permission,  | 
            |
| 207 | 3 |             config('laravel-acl.cacheMinutes'), | 
            |
| 208 |             function() use ($permission) { | 
            ||
| 209 | 3 | return $this->cacheHasPermissionTo($permission);  | 
            |
| 210 | 3 | }  | 
            |
| 211 | );  | 
            ||
| 212 | }  | 
            ||
| 213 | |||
| 214 | /**  | 
            ||
| 215 | * USER & LEVEL  | 
            ||
| 216 | * Returns if a level is lower ranking than another level,  | 
            ||
| 217 | * or if a user is lower ranking than another user.  | 
            ||
| 218 | *  | 
            ||
| 219 | * @param $other  | 
            ||
| 220 | * @return bool  | 
            ||
| 221 | */  | 
            ||
| 222 | 3 | public function isLowerThan($other)  | 
            |
| 223 |     { | 
            ||
| 224 | 3 |         return is_a($this, config('laravel-acl.user'), true) ? $this->level->rank > $other->level->rank : $this->rank > $other->rank; | 
            |
| 225 | }  | 
            ||
| 226 | |||
| 227 | /**  | 
            ||
| 228 | * USER & LEVEL  | 
            ||
| 229 | * Returns if a level is higher ranking than another level,  | 
            ||
| 230 | * or if a user is higher ranking than another user.  | 
            ||
| 231 | *  | 
            ||
| 232 | * @param $other  | 
            ||
| 233 | * @return bool  | 
            ||
| 234 | */  | 
            ||
| 235 | 3 | public function isHigherThan($other)  | 
            |
| 236 |     { | 
            ||
| 237 | 3 |         return is_a($this, config('laravel-acl.user'), true) ? $this->level->rank < $other->level->rank : $this->rank < $other->rank; | 
            |
| 238 | }  | 
            ||
| 239 | |||
| 240 | /**  | 
            ||
| 241 | * USER & LEVEL  | 
            ||
| 242 | * Returns if a equal is lower ranking than another level,  | 
            ||
| 243 | * or if a user is equal ranking than another user.  | 
            ||
| 244 | *  | 
            ||
| 245 | * @param $other  | 
            ||
| 246 | * @return bool  | 
            ||
| 247 | */  | 
            ||
| 248 | 1 | public function isEqualTo($other)  | 
            |
| 249 |     { | 
            ||
| 250 | 1 |         return is_a($this, config('laravel-acl.user'), true) ? $this->level->rank == $other->level->rank : $this->rank == $other->rank; | 
            |
| 251 | }  | 
            ||
| 252 | |||
| 253 | |||
| 254 | /* ------------------------------------------------------------------------------------------------  | 
            ||
| 255 | | Other Functions  | 
            ||
| 256 | | ------------------------------------------------------------------------------------------------  | 
            ||
| 257 | */  | 
            ||
| 258 | /**  | 
            ||
| 259 | * USER & LEVEL  | 
            ||
| 260 | * Return a collection of permissions  | 
            ||
| 261 | * assigned to a user  | 
            ||
| 262 | *  | 
            ||
| 263 | * @return \Illuminate\Support\Collection  | 
            ||
| 264 | */  | 
            ||
| 265 | 2 | protected function cacheGetPermissions()  | 
            |
| 266 |     { | 
            ||
| 267 | 2 | return $this->permissions;  | 
            |
| 268 | }  | 
            ||
| 269 | |||
| 270 | /**  | 
            ||
| 271 | * USER & LEVEL  | 
            ||
| 272 | * Return a collection of all permissions  | 
            ||
| 273 | * associated to a user by direct or  | 
            ||
| 274 | * inheritance  | 
            ||
| 275 | *  | 
            ||
| 276 | * @return \Illuminate\Support\Collection  | 
            ||
| 277 | */  | 
            ||
| 278 | 7 | protected function cacheGetAllPermissions()  | 
            |
| 279 |     { | 
            ||
| 280 | 7 |         $rank = is_a($this, config('laravel-acl.level'), true) ? $this->rank : $this->level->rank; | 
            |
| 281 | |||
| 282 | 7 |         $levels = config('laravel-acl.level', Level::class)::where('rank', '>=', $rank)->with('permissions')->get(); | 
            |
| 283 | |||
| 284 | 7 | $allPerms = new Collection();  | 
            |
| 285 | |||
| 286 |         $levels->each(function($level) use (&$allPerms) { | 
            ||
| 287 | 7 | $allPerms = $allPerms->merge($level->permissions);  | 
            |
| 288 | 7 | });  | 
            |
| 289 | |||
| 290 | 7 |         return $allPerms->merge($this->permissions)->unique('id'); | 
            |
| 291 | }  | 
            ||
| 292 | |||
| 293 | /**  | 
            ||
| 294 | * USER & LEVEL  | 
            ||
| 295 | * Return a collection of permissions that  | 
            ||
| 296 | * are inherited from a higher level  | 
            ||
| 297 | *  | 
            ||
| 298 | * @return \Illuminate\Support\Collection  | 
            ||
| 299 | */  | 
            ||
| 300 | 3 | protected function cacheGetInheritedPermissions()  | 
            |
| 315 | |||
| 316 | /**  | 
            ||
| 317 | * USER & LEVEL  | 
            ||
| 318 | * Return a collection of permissions still able to be assigned  | 
            ||
| 319 | * ie, not already inherited or explicitly assigned  | 
            ||
| 320 | *  | 
            ||
| 321 | * @return \Illuminate\Support\Collection  | 
            ||
| 322 | */  | 
            ||
| 323 | 2 | protected function cacheGetAvailablePermissions()  | 
            |
| 324 |     { | 
            ||
| 325 | 2 |         $allPerms = config('laravel-acl.permission', Permission::class)::with('level')->get(); | 
            |
| 329 | |||
| 330 | /**  | 
            ||
| 331 | * USER & LEVEL  | 
            ||
| 332 | * Check if a model has permission to do something  | 
            ||
| 333 | *  | 
            ||
| 334 | * @param $permission  | 
            ||
| 335 | * @return bool  | 
            ||
| 336 | */  | 
            ||
| 337 | 3 | protected function cacheHasPermissionTo($permission)  | 
            |
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * USER & LEVEL  | 
            ||
| 354 | * Modify a given set of permissions, given the action to be taken  | 
            ||
| 355 | *  | 
            ||
| 356 | * @param $permissions  | 
            ||
| 357 | * @param string $action  | 
            ||
| 358 | */  | 
            ||
| 359 | 24 | protected function modifyPermissions($permissions, $action)  | 
            |
| 382 | |||
| 383 | /**  | 
            ||
| 384 | * USER & LEVEL  | 
            ||
| 385 | * Helper function to build the array  | 
            ||
| 386 | * for modifyPermissions  | 
            ||
| 387 | *  | 
            ||
| 388 | * @param $permissions  | 
            ||
| 389 | * @param $action  | 
            ||
| 390 | * @return array  | 
            ||
| 391 | * @throws PermissionNotFoundException  | 
            ||
| 392 | */  | 
            ||
| 393 | 24 | protected function buildPermissionsArray($permissions, $action)  | 
            |
| 411 | |||
| 412 | /**  | 
            ||
| 413 | * USER & LEVEL  | 
            ||
| 414 | * Helper function to handle adding or negating permissions  | 
            ||
| 415 | *  | 
            ||
| 416 | * @param $permissionObjects  | 
            ||
| 417 | */  | 
            ||
| 418 | 23 | protected function addOrNegate($permissionObjects)  | 
            |
| 428 | |||
| 429 | /**  | 
            ||
| 430 | * USER & LEVEL  | 
            ||
| 431 | * Helper function to handle removing permissions  | 
            ||
| 432 | *  | 
            ||
| 433 | * @param $permissionObjects  | 
            ||
| 434 | */  | 
            ||
| 435 | 3 | View Code Duplication | protected function remove($permissionObjects)  | 
            
| 447 | |||
| 448 | /**  | 
            ||
| 449 | * USER & LEVEL  | 
            ||
| 450 | * Help function to handle syncing permissions  | 
            ||
| 451 | *  | 
            ||
| 452 | * @param $permissionObjects  | 
            ||
| 453 | */  | 
            ||
| 454 | 2 | View Code Duplication | protected function sync($permissionObjects)  | 
            
| 469 | |||
| 470 | /**  | 
            ||
| 471 | * USER & LEVEL  | 
            ||
| 472 | * Helper function to get the permission whether it is the permission ID  | 
            ||
| 473 | * or the permission name, or the permission object itself.  | 
            ||
| 474 | *  | 
            ||
| 475 | * @param $permission  | 
            ||
| 476 | * @return \z1haze\Acl\Models\Permission  | 
            ||
| 477 | * @throws PermissionNotFoundException  | 
            ||
| 478 | */  | 
            ||
| 479 | 24 | View Code Duplication | protected function getPermission($permission)  | 
            
| 495 | }  | 
            
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.