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 |
||
6 | View Code Duplication | class Ruby extends Model |
|
|
|||
7 | { |
||
8 | /** |
||
9 | * The attributes that are mass assignable. |
||
10 | * |
||
11 | * @var array |
||
12 | */ |
||
13 | protected $fillable = [ |
||
14 | 'user_id', |
||
15 | 'obtainable_id', |
||
16 | 'obtainable_type', |
||
17 | 'event_type', |
||
18 | 'data' |
||
19 | ]; |
||
20 | |||
21 | /** |
||
22 | * The attributes that should be cast to native types. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $casts = [ |
||
27 | 'data' => 'array' |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * The "booting" method of the model. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | protected static function boot() |
||
44 | |||
45 | /** |
||
46 | * Get the user that owns the log. |
||
47 | * |
||
48 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
49 | */ |
||
50 | public function user() |
||
54 | |||
55 | /** |
||
56 | * Get the obtainable relation. |
||
57 | * |
||
58 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
59 | */ |
||
60 | public function obtainable() |
||
64 | } |
||
65 |
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.