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 |
||
17 | class Administrator extends Model implements AuthenticatableContract |
||
18 | { |
||
19 | use Authenticatable, AdminBuilder, HasPermissions; |
||
20 | |||
21 | protected $fillable = ['username', 'password', 'name', 'avatar']; |
||
22 | |||
23 | /** |
||
24 | * Create a new Eloquent model instance. |
||
25 | * |
||
26 | * @param array $attributes |
||
27 | */ |
||
28 | View Code Duplication | public function __construct(array $attributes = []) |
|
38 | |||
39 | /** |
||
40 | * Get avatar attribute. |
||
41 | * |
||
42 | * @param string $avatar |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getAvatarAttribute($avatar) |
||
56 | |||
57 | /** |
||
58 | * A user has and belongs to many roles. |
||
59 | * |
||
60 | * @return BelongsToMany |
||
61 | */ |
||
62 | View Code Duplication | public function roles() : BelongsToMany |
|
70 | |||
71 | /** |
||
72 | * A User has and belongs to many permissions. |
||
73 | * |
||
74 | * @return BelongsToMany |
||
75 | */ |
||
76 | public function permissions() : BelongsToMany |
||
84 | } |
||
85 |
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.