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 |
||
19 | View Code Duplication | class NotePresenter extends AbstractPresenter |
|
|
|||
20 | { |
||
21 | use TimestampsTrait; |
||
22 | |||
23 | /** |
||
24 | * Renders the message from Markdown into HTML. |
||
25 | * |
||
26 | * @return string |
||
27 | */ |
||
28 | public function formattedMessage() |
||
32 | |||
33 | /** |
||
34 | * Present diff for humans date time. |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | public function created_at_diff() |
||
44 | |||
45 | /** |
||
46 | * Present formatted date time. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | public function created_at_formatted() |
||
56 | |||
57 | /** |
||
58 | * Formats the created_at time ready to be used by bootstrap-datetimepicker. |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public function created_at_datetimepicker() |
||
66 | |||
67 | /** |
||
68 | * Present formatted date time. |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function created_at_iso() |
||
76 | |||
77 | /** |
||
78 | * Returns a formatted timestamp for use within the timeline. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function timestamp_formatted() |
||
86 | |||
87 | /** |
||
88 | * Return the iso timestamp for use within the timeline. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function timestamp_iso() |
||
96 | |||
97 | /** |
||
98 | * Present the status with an icon. |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function icon() |
||
119 | |||
120 | /** |
||
121 | * Convert the presenter instance to an array. |
||
122 | * |
||
123 | * @return string[] |
||
124 | */ |
||
125 | public function toArray() |
||
132 | } |
||
133 |
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.