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 |
||
9 | View Code Duplication | class DiscussComment extends Model |
|
|
|||
10 | { |
||
11 | use Countable, |
||
12 | DiscussCommentPresenter, |
||
13 | HasMentionsTrait; |
||
14 | |||
15 | /** |
||
16 | * The attributes that are mass assignable. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $fillable = [ |
||
21 | 'user_id', |
||
22 | 'thread_id', |
||
23 | 'content', |
||
24 | 'is_edited' |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * The accessors to append to the model's array form. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $appends = [ |
||
33 | 'content_markdown', |
||
34 | 'comment_url' |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * Return the count cache configuration. |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | public function countCaches(): array |
||
49 | |||
50 | /** |
||
51 | * Get the user that owns the comment. |
||
52 | * |
||
53 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
54 | */ |
||
55 | public function user() |
||
59 | |||
60 | /** |
||
61 | * Get the thread that owns the comment. |
||
62 | * |
||
63 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
64 | */ |
||
65 | public function thread() |
||
69 | } |
||
70 |
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.