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 |
||
26 | class UrlTests extends StringLiteralTests |
||
27 | { |
||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | protected function randomNativeValue() |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | protected function invalidNativeValue() |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | protected function uniqueNativeValue() |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | protected function fromNative($value) |
||
59 | |||
60 | /** |
||
61 | * Test isEmpty method. |
||
62 | */ |
||
63 | View Code Duplication | public function testIsEmpty() |
|
75 | |||
76 | /** |
||
77 | * Test Host method. |
||
78 | */ |
||
79 | public function testHost() |
||
88 | |||
89 | /** |
||
90 | * Test FragmentId method. |
||
91 | */ |
||
92 | public function testFragmentId() |
||
101 | |||
102 | /** |
||
103 | * Test Password method. |
||
104 | */ |
||
105 | public function testPassword() |
||
114 | |||
115 | /** |
||
116 | * Test Path method. |
||
117 | */ |
||
118 | public function testPath() |
||
127 | |||
128 | /** |
||
129 | * Test Port method. |
||
130 | */ |
||
131 | public function testPort() |
||
140 | |||
141 | /** |
||
142 | * Test QueryString method. |
||
143 | */ |
||
144 | public function testQueryString() |
||
153 | |||
154 | /** |
||
155 | * Test Scheme method. |
||
156 | */ |
||
157 | public function testScheme() |
||
166 | |||
167 | /** |
||
168 | * Test User method. |
||
169 | */ |
||
170 | public function testUser() |
||
179 | } |
||
180 |
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.