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 |
||
11 | class WpHydraSetupContentTest extends WP_UnitTestCase { |
||
12 | /** |
||
13 | * Test setup |
||
14 | */ |
||
15 | public function setUp() { |
||
26 | |||
27 | /** |
||
28 | * Test teardown |
||
29 | */ |
||
30 | public function tearDown() { |
||
34 | |||
35 | /** |
||
36 | * Test occurences in the beginning. |
||
37 | * |
||
38 | * @covers WP_Hydra::setup_content |
||
39 | */ |
||
40 | View Code Duplication | public function testOccurencesInTheBeginning() { |
|
47 | |||
48 | /** |
||
49 | * Test occurences in the middle. |
||
50 | * |
||
51 | * @covers WP_Hydra::setup_content |
||
52 | */ |
||
53 | View Code Duplication | public function testOccurencesInTheMiddle() { |
|
60 | |||
61 | /** |
||
62 | * Test occurences in the end. |
||
63 | * |
||
64 | * @covers WP_Hydra::setup_content |
||
65 | */ |
||
66 | View Code Duplication | public function testOccurencesInTheEnd() { |
|
73 | |||
74 | /** |
||
75 | * Test multiple occurences. |
||
76 | * |
||
77 | * @covers WP_Hydra::setup_content |
||
78 | */ |
||
79 | public function testMultipleOccurences() { |
||
86 | |||
87 | /** |
||
88 | * Test occurences in an image. |
||
89 | * |
||
90 | * @covers WP_Hydra::setup_content |
||
91 | */ |
||
92 | View Code Duplication | public function testOccurencesInAnImage() { |
|
99 | |||
100 | /** |
||
101 | * Test occurences in a link. |
||
102 | * |
||
103 | * @covers WP_Hydra::setup_content |
||
104 | */ |
||
105 | View Code Duplication | public function testOccurencesInALink() { |
|
112 | |||
113 | } |
||
114 |
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.