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 GistsTest extends AbstractClientTest |
||
14 | { |
||
15 | /** Public test gist ID */ |
||
16 | const PUBLIC_GIST = '76e253825bb3c6c084cf31f92997eb72'; |
||
17 | |||
18 | /** @var Gists */ |
||
19 | protected $gists; |
||
20 | |||
21 | /** @var Gists\Comments */ |
||
22 | protected $comments; |
||
23 | |||
24 | /** |
||
25 | * GistsTest constructor. |
||
26 | * |
||
27 | * @param null $name |
||
28 | * @param array $data |
||
29 | * @param string $dataName |
||
30 | */ |
||
31 | public function __construct($name = null, array $data = [], $dataName = '') |
||
41 | |||
42 | /** |
||
43 | * Test instance of Gists's class |
||
44 | */ |
||
45 | public function testGists() |
||
49 | |||
50 | /** |
||
51 | * Test instance of Comments's class |
||
52 | */ |
||
53 | public function testComments() |
||
57 | |||
58 | /** |
||
59 | * Test list gists of current users |
||
60 | */ |
||
61 | View Code Duplication | public function testListGists() |
|
75 | |||
76 | /** |
||
77 | * Test list public gists |
||
78 | */ |
||
79 | View Code Duplication | public function testPublicListGists() |
|
93 | |||
94 | /** |
||
95 | * Test list user's starred gists |
||
96 | */ |
||
97 | View Code Duplication | public function testListUsersStarredGists() |
|
111 | |||
112 | /** |
||
113 | * Test creating a new gist |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function testCreateGist(): string |
||
134 | |||
135 | /** |
||
136 | * Test getting gist by ID |
||
137 | * |
||
138 | * @depends testCreateGist |
||
139 | * |
||
140 | * @param string $gistId |
||
141 | */ |
||
142 | public function testGetGistById(string $gistId) |
||
153 | |||
154 | /** |
||
155 | * Test updating an existing gist |
||
156 | * |
||
157 | * @depends testCreateGist |
||
158 | * |
||
159 | * @param string $gistId |
||
160 | */ |
||
161 | public function testUpdateGist(string $gistId) |
||
171 | |||
172 | /** |
||
173 | * Test list commits of a gist |
||
174 | * |
||
175 | * @depends testCreateGist |
||
176 | * |
||
177 | * @param string $gistId |
||
178 | */ |
||
179 | View Code Duplication | public function testListGistsCommit(string $gistId) |
|
190 | |||
191 | /** |
||
192 | * Test starring a gist |
||
193 | * |
||
194 | * @depends testCreateGist |
||
195 | * |
||
196 | * @param string $gistId |
||
197 | */ |
||
198 | public function testStarGist(string $gistId) |
||
202 | |||
203 | /** |
||
204 | * Test gist is starred |
||
205 | * |
||
206 | * @depends testCreateGist |
||
207 | * |
||
208 | * @param string $gistId |
||
209 | */ |
||
210 | public function testGistIsStarred(string $gistId) |
||
214 | |||
215 | /** |
||
216 | * Test unstar a gist |
||
217 | * |
||
218 | * @depends testCreateGist |
||
219 | * |
||
220 | * @param string $gistId |
||
221 | */ |
||
222 | public function testUnStarGist(string $gistId) |
||
226 | |||
227 | /** |
||
228 | * Test fork a public gist |
||
229 | */ |
||
230 | public function testForkGist() |
||
238 | |||
239 | /** |
||
240 | * Test list forks of a specific gist |
||
241 | */ |
||
242 | public function testListGistForks() |
||
250 | |||
251 | /** |
||
252 | * Test deleting a forked gist |
||
253 | * |
||
254 | * @depends testForkGist |
||
255 | * |
||
256 | * @param string $gistId |
||
257 | */ |
||
258 | public function testDeleteForkedGist(string $gistId) |
||
262 | |||
263 | /** |
||
264 | * Test create a new comment in specific gist |
||
265 | * |
||
266 | * @depends testCreateGist |
||
267 | * |
||
268 | * @param string $gistId |
||
269 | */ |
||
270 | public function testCreateComment(string $gistId) |
||
278 | |||
279 | /** |
||
280 | * Test listing all comments for specific gist |
||
281 | * |
||
282 | * @depends testCreateGist |
||
283 | * |
||
284 | * @param string $gistId |
||
285 | */ |
||
286 | public function testListComments(string $gistId) |
||
295 | |||
296 | /** |
||
297 | * Test getting a single comment |
||
298 | * |
||
299 | * @depends testCreateGist |
||
300 | * @depends testCreateComment |
||
301 | * |
||
302 | * @param string $gistId |
||
303 | * @param string $commentId |
||
304 | */ |
||
305 | public function testGetSingleComment(string $gistId, string $commentId) |
||
311 | |||
312 | /** |
||
313 | * Test editing a gist's comment |
||
314 | * |
||
315 | * @depends testCreateGist |
||
316 | * @depends testCreateComment |
||
317 | * |
||
318 | * @param string $gistId |
||
319 | * @param string $commentId |
||
320 | */ |
||
321 | public function testEditComment(string $gistId, string $commentId) |
||
327 | |||
328 | /** |
||
329 | * Test deleting a comment |
||
330 | * |
||
331 | * @depends testCreateGist |
||
332 | * @depends testCreateComment |
||
333 | * |
||
334 | * @param string $gistId |
||
335 | * @param string $commentId |
||
336 | */ |
||
337 | public function testDeleteComment(string $gistId, string $commentId) |
||
341 | |||
342 | /** |
||
343 | * Test deleting an existing gist |
||
344 | * |
||
345 | * @depends testCreateGist |
||
346 | * |
||
347 | * @param string $gistId |
||
348 | */ |
||
349 | public function testDeleteGist(string $gistId) |
||
353 | |||
354 | } |
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.