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 |
||
6 | class Create extends SenhorVerdugo |
||
7 | { |
||
8 | View Code Duplication | public function issueWatcher() |
|
24 | |||
25 | View Code Duplication | public function issueWorklog() |
|
26 | { |
||
27 | $issueKey = 'TEST-961'; |
||
28 | |||
29 | try { |
||
30 | $workLog = new Worklog(); |
||
31 | |||
32 | $workLog->setComment('I did some work here.') |
||
33 | ->setStarted("2016-05-28 12:35:54") |
||
34 | ->setTimeSpent('1d 2h 3m'); |
||
35 | |||
36 | $issueService = new IssueService(); |
||
37 | |||
38 | $ret = $issueService->addWorklog($issueKey, $workLog); |
||
39 | |||
40 | $workLogid = $ret->{'id'}; |
||
41 | |||
42 | var_dump($ret); |
||
43 | } catch (SenhorVerdugoException $e) { |
||
44 | $this->assertTrue(false, 'Create Failed : '.$e->getMessage()); |
||
45 | } |
||
46 | |||
47 | } |
||
48 | |||
49 | |||
50 | public function issueLink() |
||
68 | |||
69 | |||
70 | View Code Duplication | public function issueRemoteLink() |
|
71 | { |
||
72 | $issueKey = 'TEST-316'; |
||
73 | |||
74 | try { |
||
75 | $issueService = new IssueService(); |
||
76 | |||
77 | $ril = new RemoteIssueLink(); |
||
78 | |||
79 | $ril->setUrl('http://www.mycompany.com/support?id=1') |
||
80 | ->setTitle('Remote Link Title') |
||
81 | ->setRelationship('causes') |
||
82 | ->setSummary('Crazy customer support issue'); |
||
83 | |||
84 | $rils = $issueService->createOrUpdateRemoteIssueLink($issueKey, $ril); |
||
85 | |||
86 | // rils is array of RemoteIssueLink classes |
||
87 | var_dump($rils); |
||
88 | } catch (SenhorVerdugoException $e) { |
||
89 | $this->assertTrue(false, 'Create Failed : '.$e->getMessage()); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | public function comment() |
||
120 | |||
121 | public function addAttachment() |
||
140 | |||
141 | public function multipleIssue() |
||
142 | { |
||
143 | |||
144 | try { |
||
145 | $issueFieldOne = new IssueField(); |
||
146 | |||
147 | $issueFieldOne->setProjectKey("TEST") |
||
148 | ->setSummary("something's wrong") |
||
149 | ->setPriorityName("Critical") |
||
150 | ->setIssueType("Bug") |
||
151 | ->setDescription("Full description for issue"); |
||
152 | |||
153 | $issueFieldTwo = new IssueField(); |
||
154 | |||
155 | $issueFieldTwo->setProjectKey("TEST") |
||
156 | ->setSummary("something else is wrong") |
||
157 | ->setPriorityName("Critical") |
||
158 | ->setIssueType("Bug") |
||
159 | ->setDescription("Full description for second issue"); |
||
160 | |||
161 | $issueService = new IssueService(); |
||
162 | |||
163 | $ret = $issueService->createMultiple([$issueFieldOne, $issueFieldTwo]); |
||
164 | |||
165 | //If success, returns an array of the created issues |
||
166 | var_dump($ret); |
||
167 | } catch (SenhorVerdugoException $e) { |
||
168 | print("Error Occured! " . $e->getMessage()); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | public function issue() |
||
173 | { |
||
174 | try { |
||
175 | $issueField = new IssueField(); |
||
176 | |||
177 | $issueField->setProjectKey("TEST") |
||
178 | ->setSummary("something's wrong") |
||
179 | ->setAssigneeName("lesstif") |
||
180 | ->setPriorityName("Critical") |
||
181 | ->setIssueType("Bug") |
||
182 | ->setDescription("Full description for issue") |
||
183 | ->addVersion(["1.0.1", "1.0.3"]) |
||
184 | ->addComponents(['Component-1', 'Component-2']) |
||
185 | // set issue security if you need. |
||
186 | ->setSecurityId(10001 /* security scheme id */) |
||
187 | ->setDueDate('2019-06-19'); |
||
188 | |||
189 | $issueService = new IssueService(); |
||
190 | |||
191 | $ret = $issueService->create($issueField); |
||
192 | |||
193 | //If success, Returns a link to the created issue. |
||
194 | var_dump($ret); |
||
195 | } catch (SenhorVerdugoException $e) { |
||
196 | print("Error Occured! " . $e->getMessage()); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | public function field() |
||
220 | |||
221 | public function project(ProjectModel $project) |
||
222 | { |
||
223 | |||
224 | try { |
||
253 | |||
254 | public function user() |
||
274 | } |
||
275 |
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.