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 |
||
| 15 | trait EntrustPermissionTrait |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Many-to-Many relations with role model. |
||
| 19 | * |
||
| 20 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 21 | */ |
||
| 22 | public function roles() |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Boot the permission model |
||
| 32 | * Attach event listener to remove the many-to-many records when trying to |
||
| 33 | * delete Will NOT delete any records if the permission model uses soft |
||
| 34 | * deletes. |
||
| 35 | * |
||
| 36 | * @return void|bool |
||
| 37 | */ |
||
| 38 | View Code Duplication | public static function boot() |
|
| 52 | } |
||
| 53 |
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.