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:
Complex classes like ComposeManagerTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ComposeManagerTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class ComposeManagerTest extends PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | public function setUp() |
||
18 | |||
19 | /** |
||
20 | * Test simple start without error |
||
21 | */ |
||
22 | public function testStart() |
||
27 | |||
28 | /** |
||
29 | * Test start success with one compose file |
||
30 | */ |
||
31 | public function testStartWithOneComposeFileSpecified() |
||
36 | |||
37 | /** |
||
38 | * Test start success with two compose files |
||
39 | */ |
||
40 | public function testStartWithTwoComposeFilesSpecified() |
||
45 | |||
46 | /** |
||
47 | * Test start with project option |
||
48 | */ |
||
49 | public function testStartWithprojectOption() |
||
59 | |||
60 | /** |
||
61 | * Test start with project and networking option |
||
62 | */ |
||
63 | public function testStartWithprojectAndNetworkingOption() |
||
71 | |||
72 | /** |
||
73 | * Test start with project, networking and network driver option |
||
74 | */ |
||
75 | View Code Duplication | public function testStartWithprojectAndNetworkingWithDriverOption() |
|
83 | |||
84 | /** |
||
85 | * Test start with ComposeFileNotFoundException |
||
86 | * |
||
87 | * @expectedException \Exception |
||
88 | */ |
||
89 | public function testStartThrowExceptionForDriver() |
||
95 | |||
96 | /** |
||
97 | * Test start with ComposeFileNotFoundException |
||
98 | * |
||
99 | * @expectedException \DockerCompose\Exception\ComposeFileNotFoundException |
||
100 | */ |
||
101 | View Code Duplication | public function testStartThrowComposeFileNotFound() |
|
108 | |||
109 | /** |
||
110 | * Test start with DockerHostConnexionErrorException |
||
111 | * |
||
112 | * @expectedException \DockerCompose\Exception\DockerHostConnexionErrorException |
||
113 | */ |
||
114 | View Code Duplication | public function testStartThrowDockerHostConnexionErrorException() |
|
121 | |||
122 | /** |
||
123 | * Test start with DockerInstallationMissingException |
||
124 | * |
||
125 | * @expectedException \DockerCompose\Exception\DockerInstallationMissingException |
||
126 | */ |
||
127 | public function testStartThrowDockerInstallationMissingException() |
||
132 | |||
133 | /** |
||
134 | * Test start with DockerInstallationMissingException |
||
135 | * |
||
136 | * @expectedException \Exception |
||
137 | */ |
||
138 | public function testStartThrowUnknownException() |
||
143 | |||
144 | /** |
||
145 | * Test stop whithout error |
||
146 | */ |
||
147 | public function testStop() |
||
152 | |||
153 | /** |
||
154 | * Test stop success with one compose file |
||
155 | */ |
||
156 | public function testStopWithOneComposeFileSpecified() |
||
162 | |||
163 | /** |
||
164 | * Test stop success with two compose files |
||
165 | */ |
||
166 | public function testStopWithTwoComposeFilesSpecified() |
||
172 | |||
173 | /** |
||
174 | * Test stop with ComposeFileNotFoundException |
||
175 | * |
||
176 | * @expectedException \DockerCompose\Exception\ComposeFileNotFoundException |
||
177 | */ |
||
178 | View Code Duplication | public function testStopThrowComposeFileNotFound() |
|
185 | |||
186 | /** |
||
187 | * Test stop with DockerHostConnexionErrorException |
||
188 | * |
||
189 | * @expectedException \DockerCompose\Exception\DockerHostConnexionErrorException |
||
190 | */ |
||
191 | View Code Duplication | public function testStopThrowDockerHostConnexionErrorException() |
|
198 | |||
199 | /** |
||
200 | * Test stop with DockerInstallationMissingException |
||
201 | * |
||
202 | * @expectedException \DockerCompose\Exception\DockerInstallationMissingException |
||
203 | */ |
||
204 | public function testStopThrowDockerInstallationMissingException() |
||
209 | |||
210 | /** |
||
211 | * Test remove whithout error |
||
212 | */ |
||
213 | public function testRemove() |
||
218 | |||
219 | /** |
||
220 | * Test remove force |
||
221 | */ |
||
222 | public function testRemoveForce() |
||
227 | |||
228 | /** |
||
229 | * Test remove volumes |
||
230 | */ |
||
231 | public function testRemoveVolumes() |
||
236 | |||
237 | /** |
||
238 | * Test remove force and volumes |
||
239 | */ |
||
240 | public function testRemoveForceAndVolumes() |
||
245 | |||
246 | /** |
||
247 | * Test remove success with one compose file |
||
248 | */ |
||
249 | public function testRemoveWithOneComposeFileSpecified() |
||
255 | |||
256 | /** |
||
257 | * Test remove success with two compose files |
||
258 | */ |
||
259 | public function testRemoveWithTwoComposeFilesSpecified() |
||
265 | |||
266 | /** |
||
267 | * Test remove with ComposeFileNotFoundException |
||
268 | * |
||
269 | * @expectedException \DockerCompose\Exception\ComposeFileNotFoundException |
||
270 | */ |
||
271 | View Code Duplication | public function testRemoveThrowComposeFileNotFound() |
|
278 | |||
279 | /** |
||
280 | * Test remove with DockerHostConnexionErrorException |
||
281 | * |
||
282 | * @expectedException \DockerCompose\Exception\DockerHostConnexionErrorException |
||
283 | */ |
||
284 | View Code Duplication | public function testRemoveThrowDockerHostConnexionErrorException() |
|
291 | |||
292 | /** |
||
293 | * Test remove with DockerInstallationMissingException |
||
294 | * |
||
295 | * @expectedException \DockerCompose\Exception\DockerInstallationMissingException |
||
296 | */ |
||
297 | public function testRemoveThrowDockerInstallationMissingException() |
||
302 | |||
303 | /** |
||
304 | * Test run |
||
305 | */ |
||
306 | public function testrun() |
||
311 | |||
312 | /** |
||
313 | * Test run |
||
314 | * @expectedException \DockerCompose\Exception\NoSuchServiceException |
||
315 | */ |
||
316 | public function testrunThrowNoSuchServiceException() |
||
321 | |||
322 | /** |
||
323 | * Test run with project, networking and network driver option |
||
324 | */ |
||
325 | View Code Duplication | public function testRuntWithprojectAndNetworkingWithDriverOption() |
|
333 | |||
334 | /** |
||
335 | * Test simple build without error |
||
336 | */ |
||
337 | public function testBuild() |
||
342 | |||
343 | /** |
||
344 | * Test build success with one compose file |
||
345 | */ |
||
346 | public function testBuildWithOneComposeFileSpecified() |
||
351 | |||
352 | /** |
||
353 | * Test build success with two compose files |
||
354 | */ |
||
355 | public function testBuildWithTwoComposeFilesSpecified() |
||
360 | |||
361 | /** |
||
362 | * Test simple build without pull |
||
363 | */ |
||
364 | public function testBuildWithoutPull() |
||
369 | |||
370 | /** |
||
371 | * Test simple build with --force-rm |
||
372 | */ |
||
373 | public function testBuildWithForceRm() |
||
378 | |||
379 | /** |
||
380 | * Test simple build with --no-cache |
||
381 | */ |
||
382 | public function testBuildWithNoCache() |
||
387 | |||
388 | /** |
||
389 | * Test simple build with --pull --force-rm --no-cache |
||
390 | */ |
||
391 | public function testBuildWithPullAndForceRmAndNoCache() |
||
397 | |||
398 | /** |
||
399 | * Test simple ps without error |
||
400 | */ |
||
401 | public function testPs() |
||
406 | |||
407 | /** |
||
408 | * Test ps success with one compose file |
||
409 | */ |
||
410 | public function testPsWithOneComposeFileSpecified() |
||
415 | |||
416 | /** |
||
417 | * Test ps success with two compose files |
||
418 | */ |
||
419 | public function testPsWithTwoComposeFilesSpecified() |
||
424 | |||
425 | /** |
||
426 | * Test ps with project option |
||
427 | */ |
||
428 | public function testPsWithprojectOption() |
||
437 | |||
438 | /** |
||
439 | * Test simple restart without error |
||
440 | */ |
||
441 | public function testRestart() |
||
446 | |||
447 | /** |
||
448 | * Test simple restart with timeout |
||
449 | */ |
||
450 | public function testRestartWithTimeout() |
||
455 | |||
456 | /** |
||
457 | * Test restart success with one compose file |
||
458 | */ |
||
459 | public function testRestartWithOneComposeFileSpecified() |
||
464 | |||
465 | /** |
||
466 | * Test restart success with two compose files |
||
467 | */ |
||
468 | public function testRestartWithTwoComposeFilesSpecified() |
||
473 | |||
474 | /** |
||
475 | * Test restart with project option |
||
476 | */ |
||
477 | public function testRestartWithprojectOption() |
||
487 | |||
488 | /** |
||
489 | * Test simple kill without error |
||
490 | */ |
||
491 | public function testKill() |
||
496 | |||
497 | /** |
||
498 | * Test simple kill with specific SIGNAL |
||
499 | */ |
||
500 | public function testKillWithSpecificSIGNAL() |
||
505 | |||
506 | /** |
||
507 | * Test kill success with one compose file |
||
508 | */ |
||
509 | public function testKillWithOneComposeFileSpecified() |
||
514 | |||
515 | /** |
||
516 | * Test kill success with two compose files |
||
517 | */ |
||
518 | public function testKillWithTwoComposeFilesSpecified() |
||
523 | |||
524 | /** |
||
525 | * Test kill with project option |
||
526 | */ |
||
527 | public function testKillWithprojectOption() |
||
537 | |||
538 | /** |
||
539 | * Test simple get ips containers |
||
540 | */ |
||
541 | View Code Duplication | public function testIps() |
|
550 | |||
551 | /** |
||
552 | * Test start success with one compose file |
||
553 | */ |
||
554 | View Code Duplication | public function testIpsWithOneComposeFileSpecified() |
|
562 | |||
563 | /** |
||
564 | * Test ips success with two compose files |
||
565 | */ |
||
566 | View Code Duplication | public function testIpsWithTwoComposeFilesSpecified() |
|
574 | |||
575 | /** |
||
576 | * Test ips with project option |
||
577 | */ |
||
578 | public function testIpsWithprojectOption() |
||
590 | |||
591 | /** |
||
592 | * Test stop whithout error |
||
593 | */ |
||
594 | public function testPull() |
||
599 | |||
600 | /** |
||
601 | * Test stop success with one compose file |
||
602 | */ |
||
603 | public function testPullWithOneComposeFileSpecified() |
||
609 | |||
610 | /** |
||
611 | * Test stop success with two compose files |
||
612 | */ |
||
613 | public function testPullpWithTwoComposeFilesSpecified() |
||
619 | } |
||
620 |
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.