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 |
||
15 | class GithubIssueSource implements GithubIssue |
||
16 | { |
||
17 | /** @var GithubIssueId */ |
||
18 | protected $id; |
||
19 | /** @var GithubRepo */ |
||
20 | protected $repo; |
||
21 | /** @var int */ |
||
22 | private $number; |
||
23 | /** @var GithubIssueState */ |
||
24 | private $state; |
||
25 | /** @var string */ |
||
26 | private $title; |
||
27 | /** @var string */ |
||
28 | private $body; |
||
29 | /** @var GithubUser */ |
||
30 | private $createdByUser; |
||
31 | /** @var GithubUser */ |
||
32 | private $assignedToUser; |
||
33 | /** @var GithubMilestone */ |
||
34 | private $milestone; |
||
35 | /** @var int */ |
||
36 | private $commentCount; |
||
37 | /** @var \DateTime */ |
||
38 | private $githubCreatedAt; |
||
39 | /** @var \DateTime */ |
||
40 | private $githubUpdatedAt; |
||
41 | /** @var \DateTime */ |
||
42 | private $githubClosedAt; |
||
43 | |||
44 | /** |
||
45 | * GithubIssueSource constructor. |
||
46 | * |
||
47 | * @param GithubIssueId $id |
||
48 | * @param GithubRepo $repo |
||
49 | * @param int $number |
||
50 | * @param GithubIssueState $state |
||
51 | * @param string $title |
||
52 | * @param string $body |
||
53 | * @param GithubUser $createdByUser |
||
54 | * @param GithubUser $assignedToUser |
||
55 | * @param GithubMilestone $milestone |
||
56 | * @param int $commentCount |
||
57 | * @param DateTime $githubCreatedAt |
||
58 | * @param DateTime $githubUpdatedAt |
||
59 | * @param DateTime $githubClosedAt |
||
60 | * |
||
61 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
62 | */ |
||
63 | View Code Duplication | public function __construct( |
|
92 | |||
93 | /** |
||
94 | * @return GithubIssueId |
||
95 | */ |
||
96 | public function getId() |
||
100 | |||
101 | /** |
||
102 | * @return GithubRepoId |
||
103 | */ |
||
104 | public function getRepoId() |
||
108 | |||
109 | /** |
||
110 | * @return GithubRepo |
||
111 | */ |
||
112 | public function getRepo() |
||
116 | |||
117 | /** |
||
118 | * @return int |
||
119 | */ |
||
120 | public function getNumber() |
||
124 | |||
125 | /** |
||
126 | * @return GithubIssueState |
||
127 | */ |
||
128 | public function getState() |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getTitle() |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getBody() |
||
148 | |||
149 | /** |
||
150 | * @return GithubUserId |
||
151 | */ |
||
152 | public function getCreatedByUserId() |
||
156 | |||
157 | /** |
||
158 | * @return GithubUser |
||
159 | */ |
||
160 | public function getCreatedByUser() |
||
164 | |||
165 | /** |
||
166 | * @return GithubUserId |
||
167 | */ |
||
168 | public function getAssignedToUserId() |
||
176 | |||
177 | /** |
||
178 | * @return GithubUser |
||
179 | */ |
||
180 | public function getAssignedToUser() |
||
184 | |||
185 | /** |
||
186 | * @return GithubMilestoneId |
||
187 | */ |
||
188 | public function getMilestoneId() |
||
196 | |||
197 | /** |
||
198 | * @return GithubMilestone |
||
199 | */ |
||
200 | public function getMilestone() |
||
204 | |||
205 | /** |
||
206 | * @return int |
||
207 | */ |
||
208 | public function getCommentCount() |
||
212 | |||
213 | /** |
||
214 | * @return DateTime |
||
215 | */ |
||
216 | public function getGithubCreatedAt() |
||
220 | |||
221 | /** |
||
222 | * @return DateTime |
||
223 | */ |
||
224 | public function getGithubUpdatedAt() |
||
228 | |||
229 | /** |
||
230 | * @return DateTime |
||
231 | */ |
||
232 | public function getGithubClosedAt() |
||
236 | } |
||
237 |
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.