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 |
||
12 | trait DiscussLogPresenter |
||
13 | { |
||
14 | /** |
||
15 | * Get the type related to the Event. |
||
16 | * |
||
17 | * @return string |
||
|
|||
18 | */ |
||
19 | public function getTypeAttribute() |
||
41 | |||
42 | /** |
||
43 | * Get the user related to the deleted post. |
||
44 | * |
||
45 | * @return null|\Xetaravel\Models\User |
||
46 | */ |
||
47 | public function getPostUserAttribute() |
||
48 | { |
||
49 | if ($this->event_type !== PostWasDeletedEvent::class) { |
||
50 | return null; |
||
51 | } |
||
52 | |||
53 | return User::find($this->data['post_user_id']); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get the old category. |
||
58 | * |
||
59 | * @return null|\Xetaravel\Models\DiscussCategory |
||
60 | */ |
||
61 | View Code Duplication | public function getOldCategoryAttribute() |
|
69 | |||
70 | /** |
||
71 | * Get the new category. |
||
72 | * |
||
73 | * @return null|\Xetaravel\Models\DiscussCategory |
||
74 | */ |
||
75 | View Code Duplication | public function getNewCategoryAttribute() |
|
83 | } |
||
84 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.