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 |
||
13 | class GithubMilestoneSource implements GithubMilestone |
||
14 | { |
||
15 | /** @var GithubMilestoneId */ |
||
16 | protected $id; |
||
17 | /** @var GithubRepo */ |
||
18 | protected $repo; |
||
19 | /** @var int */ |
||
20 | private $number; |
||
21 | /** @var GithubMilestoneState */ |
||
22 | private $state; |
||
23 | /** @var string */ |
||
24 | private $title; |
||
25 | /** @var string */ |
||
26 | private $description; |
||
27 | /** @var GithubUser */ |
||
28 | private $createdByUser; |
||
29 | /** @var int */ |
||
30 | private $openIssueCount; |
||
31 | /** @var int */ |
||
32 | private $closedIssueCount; |
||
33 | /** @var \DateTime */ |
||
34 | private $dueDate; |
||
35 | /** @var \DateTime */ |
||
36 | private $githubCreatedAt; |
||
37 | /** @var \DateTime */ |
||
38 | private $githubUpdatedAt; |
||
39 | /** @var \DateTime */ |
||
40 | private $githubClosedAt; |
||
41 | |||
42 | /** |
||
43 | * GithubMilestoneSource constructor. |
||
44 | * |
||
45 | * @param GithubMilestoneId $id |
||
46 | * @param GithubRepo $repo |
||
47 | * @param int $number |
||
48 | * @param GithubMilestoneState $state |
||
49 | * @param string $title |
||
50 | * @param string $description |
||
51 | * @param GithubUser $createdByUser |
||
52 | * @param int $openIssueCount |
||
53 | * @param int $closedIssueCount |
||
54 | * @param DateTime $dueDate |
||
55 | * @param DateTime $githubCreatedAt |
||
56 | * @param DateTime $githubUpdatedAt |
||
57 | * @param DateTime $githubClosedAt |
||
58 | * |
||
59 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
60 | */ |
||
61 | View Code Duplication | public function __construct( |
|
90 | |||
91 | /** |
||
92 | * @return GithubMilestoneId |
||
93 | */ |
||
94 | public function getId() |
||
98 | |||
99 | /** |
||
100 | * @return GithubRepoId |
||
101 | */ |
||
102 | public function getRepoId() |
||
106 | |||
107 | /** |
||
108 | * @return GithubRepo |
||
109 | */ |
||
110 | public function getRepo() |
||
114 | |||
115 | /** |
||
116 | * @return int |
||
117 | */ |
||
118 | public function getNumber() |
||
122 | |||
123 | /** |
||
124 | * @return GithubMilestoneState |
||
125 | */ |
||
126 | public function getState() |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | public function getTitle() |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | public function getDescription() |
||
146 | |||
147 | /** |
||
148 | * @return GithubUserId |
||
149 | */ |
||
150 | public function getCreatedByUserId() |
||
154 | |||
155 | /** |
||
156 | * @return GithubUser |
||
157 | */ |
||
158 | public function getCreatedByUser() |
||
162 | |||
163 | /** |
||
164 | * @return int |
||
165 | */ |
||
166 | public function getOpenIssueCount() |
||
170 | |||
171 | /** |
||
172 | * @return int |
||
173 | */ |
||
174 | public function getClosedIssueCount() |
||
178 | |||
179 | /** |
||
180 | * @return DateTime |
||
181 | */ |
||
182 | public function getDueDate() |
||
186 | |||
187 | /** |
||
188 | * @return DateTime |
||
189 | */ |
||
190 | public function getGithubCreatedAt() |
||
194 | |||
195 | /** |
||
196 | * @return DateTime |
||
197 | */ |
||
198 | public function getGithubUpdatedAt() |
||
202 | |||
203 | /** |
||
204 | * @return DateTime |
||
205 | */ |
||
206 | public function getGithubClosedAt() |
||
210 | } |
||
211 |
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.