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 |
||
23 | View Code Duplication | trait RelationTrait |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * A comment has one user (inverse relationship of User::comments). |
||
27 | * |
||
28 | * @return Relations\BelongsTo |
||
29 | */ |
||
30 | public function user() |
||
31 | { |
||
32 | return $this->belongsTo('\Tinyissue\Model\User', 'created_by'); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * A comment has belongs to an issue. |
||
37 | * |
||
38 | * @return Relations\BelongsTo |
||
39 | */ |
||
40 | 5 | public function issue() |
|
41 | { |
||
42 | 5 | return $this->belongsTo('\Tinyissue\Model\Project\Issue', 'issue_id'); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Comment can have many attachments. |
||
47 | * |
||
48 | * @return Relations\HasMany |
||
49 | */ |
||
50 | 9 | public function attachments() |
|
51 | { |
||
52 | 9 | return $this->hasMany('Tinyissue\Model\Project\Issue\Attachment', 'comment_id'); |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Comment can have one activity. |
||
57 | * |
||
58 | * @return Relations\HasOne |
||
59 | */ |
||
60 | 10 | public function activity() |
|
61 | { |
||
62 | 10 | return $this->hasOne('Tinyissue\Model\User\Activity', 'action_id'); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Comment can have many messages queue. |
||
67 | * |
||
68 | * @return Relations\HasMany |
||
69 | */ |
||
70 | public function messagesQueue() |
||
71 | { |
||
72 | return $this->morphMany('Tinyissue\Model\Message\Queue', 'model'); |
||
73 | } |
||
74 | |||
75 | abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
||
79 | } |
||
80 |
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.