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 |
||
8 | class FieldTest extends TestCase |
||
9 | { |
||
10 | protected $defaultValues = [ |
||
11 | 'fid' => 1, |
||
12 | 'fvalueString' => '', |
||
13 | 'fvalueInt' => 0, |
||
14 | 'fvalueFloat' => 0, |
||
15 | 'fvalueImage' => '', |
||
16 | 'fvalueDatetime' => 0, |
||
17 | 'fvalueDate' => '', |
||
18 | 'fvalueRangeInt' => [ |
||
19 | 'fvalueRangeIntMin' => 0, |
||
20 | 'fvalueRangeIntMax' => 0, |
||
21 | ], |
||
22 | 'fvalueRangeFloat' => [ |
||
23 | 'fvalueRangeFloatMin' => 0, |
||
24 | 'fvalueRangeFloatMax' => 0, |
||
25 | ], |
||
26 | 'fvalueRangeDate' => [ |
||
27 | 'fvalueRangeDateMin' => '', |
||
28 | 'fvalueRangeDateMax' => '', |
||
29 | ], |
||
30 | ]; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * @expectedException InvalidArgumentException |
||
35 | * @expectedExceptionMessage fid must be an integer |
||
36 | */ |
||
37 | public function testNonIntegerFid() |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Tests that output of Field->toArray() has the exact keys expected by WebAPI |
||
45 | * @return void |
||
46 | */ |
||
47 | public function testReturnedArray() |
||
74 | |||
75 | |||
76 | public function valueTypesProvider() |
||
98 | |||
99 | |||
100 | /** |
||
101 | * Test if various value types are properly handled |
||
102 | * @dataProvider valueTypesProvider |
||
103 | */ |
||
104 | View Code Duplication | public function testValueTypes($testValue, $arrayKey) |
|
111 | |||
112 | |||
113 | |||
114 | public function rangeValueTypesProvider() |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Test if various range types are properly handled |
||
135 | * @dataProvider rangeValueTypesProvider |
||
136 | */ |
||
137 | View Code Duplication | public function testRangeValues($testRange, $rangeKey) |
|
163 | |||
164 | |||
165 | /** |
||
166 | * Test if range types are properly handled when reversed |
||
167 | * @dataProvider rangeValueTypesProvider |
||
168 | */ |
||
169 | View Code Duplication | public function testRangeValuesReversed($testRange, $rangeKey) |
|
197 | |||
198 | |||
199 | /** |
||
200 | * Test forced value type - datetime |
||
201 | * @return void |
||
202 | */ |
||
203 | View Code Duplication | public function testDatetimeValue() |
|
211 | |||
212 | |||
213 | /** |
||
214 | * @expectedException InvalidArgumentException |
||
215 | * @expectedExceptionMessage Not supported value type: object; fid=1 |
||
216 | */ |
||
217 | public function testExceptionOnInvalidValue() |
||
221 | |||
222 | |||
223 | /** |
||
224 | * @expectedException InvalidArgumentException |
||
225 | * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn |
||
226 | */ |
||
227 | public function testExceptionOnInvalidForcedValue() |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Test fid value |
||
235 | * @return void |
||
236 | */ |
||
237 | public function testFid() |
||
242 | |||
243 | |||
244 | /** |
||
245 | * @dataProvider valueTypesProvider |
||
246 | */ |
||
247 | public function testCreatingFromArray($value, $key) |
||
257 | |||
258 | /** |
||
259 | * @expectedException InvalidArgumentException |
||
260 | * @expectedExceptionMessage Range array must have exactly 2 elements |
||
261 | */ |
||
262 | public function testExceptionOnInvalidRangeArrayItemCount() |
||
266 | |||
267 | |||
268 | /** |
||
269 | * @expectedException InvalidArgumentException |
||
270 | * @expectedExceptionMessage Fid is required |
||
271 | */ |
||
272 | public function testExceptionOnFromArrayMissingFid() |
||
279 | |||
280 | |||
281 | /** |
||
282 | * @expectedException InvalidArgumentException |
||
283 | * @expectedExceptionMessage Unknown Field property: thisKeyIsInvalid |
||
284 | */ |
||
285 | public function testExceptionOnFromArrayInvalidArrayKey() |
||
292 | |||
293 | |||
294 | public function testNullReturnValueWhenAllFieldsAreDefault() |
||
300 | |||
301 | |||
302 | } |
||
303 |
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.