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 |
||
10 | final class ArraysTest extends TestCase |
||
11 | { |
||
12 | /** |
||
13 | * @test |
||
14 | * @covers ::filter |
||
15 | */ |
||
16 | public function filterBasicPass() |
||
20 | |||
21 | /** |
||
22 | * @test |
||
23 | * @covers ::filter |
||
24 | * @expectedException \TraderInteractive\Filter\Exception |
||
25 | * @expectedExceptionMessage Value '1' is not an array |
||
26 | */ |
||
27 | public function filterFailNotArray() |
||
31 | |||
32 | /** |
||
33 | * @test |
||
34 | * @covers ::filter |
||
35 | * @expectedException \TraderInteractive\Filter\Exception |
||
36 | * @expectedExceptionMessage $value count of 0 is less than 1 |
||
37 | */ |
||
38 | public function filterFailEmpty() |
||
42 | |||
43 | /** |
||
44 | * @test |
||
45 | * @covers ::filter |
||
46 | * @expectedException \TraderInteractive\Filter\Exception |
||
47 | * @expectedExceptionMessage $value count of 1 is less than 2 |
||
48 | */ |
||
49 | public function filterCountLessThanMin() |
||
53 | |||
54 | /** |
||
55 | * @test |
||
56 | * @covers ::filter |
||
57 | * @expectedException \TraderInteractive\Filter\Exception |
||
58 | * @expectedExceptionMessage $value count of 2 is greater than 1 |
||
59 | */ |
||
60 | public function filterCountGreaterThanMax() |
||
64 | |||
65 | /** |
||
66 | * @test |
||
67 | * @covers ::in |
||
68 | */ |
||
69 | public function inPassStrict() |
||
73 | |||
74 | /** |
||
75 | * @test |
||
76 | * @covers ::in |
||
77 | */ |
||
78 | View Code Duplication | public function inFailStrict() |
|
87 | |||
88 | /** |
||
89 | * @test |
||
90 | * @covers ::in |
||
91 | */ |
||
92 | View Code Duplication | public function inFailNotStrict() |
|
101 | |||
102 | /** |
||
103 | * @test |
||
104 | * @covers ::in |
||
105 | */ |
||
106 | public function inPassNotStrict() |
||
110 | |||
111 | /** |
||
112 | * @test |
||
113 | * @covers ::ofScalars |
||
114 | */ |
||
115 | public function ofScalars() |
||
119 | |||
120 | /** |
||
121 | * @test |
||
122 | * @covers ::ofScalars |
||
123 | */ |
||
124 | public function ofScalarsChained() |
||
128 | |||
129 | /** |
||
130 | * @test |
||
131 | * @covers ::ofScalars |
||
132 | */ |
||
133 | public function ofScalarsWithMeaninglessKeys() |
||
137 | |||
138 | /** |
||
139 | * @test |
||
140 | * @covers ::ofScalars |
||
141 | */ |
||
142 | View Code Duplication | public function ofScalarsFail() |
|
159 | |||
160 | /** |
||
161 | * @test |
||
162 | * @covers ::ofArrays |
||
163 | */ |
||
164 | public function ofArrays() |
||
169 | |||
170 | /** |
||
171 | * @test |
||
172 | * @covers ::ofArrays |
||
173 | */ |
||
174 | public function ofArraysChained() |
||
180 | |||
181 | /** |
||
182 | * @test |
||
183 | * @covers ::ofArrays |
||
184 | */ |
||
185 | View Code Duplication | public function ofArraysRequiredAndUnknown() |
|
195 | |||
196 | /** |
||
197 | * @test |
||
198 | * @covers ::ofArrays |
||
199 | */ |
||
200 | public function ofArraysFail() |
||
222 | |||
223 | /** |
||
224 | * @test |
||
225 | * @covers ::ofArray |
||
226 | */ |
||
227 | public function ofArray() |
||
233 | |||
234 | /** |
||
235 | * @test |
||
236 | * @covers ::ofArray |
||
237 | */ |
||
238 | public function ofArrayChained() |
||
244 | |||
245 | /** |
||
246 | * @test |
||
247 | * @covers ::ofArray |
||
248 | */ |
||
249 | public function ofArrayRequiredSuccess() |
||
255 | |||
256 | /** |
||
257 | * @test |
||
258 | * @covers ::ofArray |
||
259 | */ |
||
260 | View Code Duplication | public function ofArrayRequiredFail() |
|
270 | |||
271 | /** |
||
272 | * @test |
||
273 | * @covers ::ofArray |
||
274 | */ |
||
275 | View Code Duplication | public function ofArrayUnknown() |
|
285 | |||
286 | /** |
||
287 | * Verifies the basic behavior of the flatten filter. |
||
288 | * |
||
289 | * @test |
||
290 | * @covers ::flatten |
||
291 | */ |
||
292 | public function flatten() |
||
296 | } |
||
297 |
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.