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 |
||
| 6 | class FieldTest extends TestCase |
||
|
|
|||
| 7 | {
|
||
| 8 | protected $defaultValues = [ |
||
| 9 | 'fid' => 1, |
||
| 10 | 'fvalueString' => '', |
||
| 11 | 'fvalueInt' => 0, |
||
| 12 | 'fvalueFloat' => 0, |
||
| 13 | 'fvalueImage' => '', |
||
| 14 | 'fvalueDatetime' => 0, |
||
| 15 | 'fvalueDate' => '', |
||
| 16 | 'fvalueRangeInt' => [ |
||
| 17 | 'fvalueRangeIntMin' => 0, |
||
| 18 | 'fvalueRangeIntMax' => 0, |
||
| 19 | ], |
||
| 20 | 'fvalueRangeFloat' => [ |
||
| 21 | 'fvalueRangeFloatMin' => 0, |
||
| 22 | 'fvalueRangeFloatMax' => 0, |
||
| 23 | ], |
||
| 24 | 'fvalueRangeDate' => [ |
||
| 25 | 'fvalueRangeDateMin' => '', |
||
| 26 | 'fvalueRangeDateMax' => '', |
||
| 27 | ], |
||
| 28 | ]; |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * @expectedException Radowoj\Yaah\Exception |
||
| 33 | * @expectedExceptionMessage fid must be an integer |
||
| 34 | */ |
||
| 35 | public function testNonIntegerFid() |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Tests that output of Field->toArray() has the exact keys expected by WebAPI |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function testReturnedArray() |
||
| 72 | |||
| 73 | |||
| 74 | public function valueTypesProvider() |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Test if various value types are properly handled |
||
| 100 | * @dataProvider valueTypesProvider |
||
| 101 | */ |
||
| 102 | View Code Duplication | public function testValueTypes($testValue, $arrayKey) |
|
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | public function rangeValueTypesProvider() |
||
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Test if various value types are properly handled |
||
| 134 | * @dataProvider rangeValueTypesProvider |
||
| 135 | */ |
||
| 136 | public function testRangeValues($testRange, $rangeKey) |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Test forced value type - datetime |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | View Code Duplication | public function testDatetimeValue() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @expectedException Radowoj\Yaah\Exception |
||
| 177 | * @expectedExceptionMessage Not supported value type: object; fid=1 |
||
| 178 | */ |
||
| 179 | public function testExceptionOnInvalidValue() |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * @expectedException Radowoj\Yaah\Exception |
||
| 187 | * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn |
||
| 188 | */ |
||
| 189 | public function testExceptionOnInvalidForcedValue() |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Test fid value |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | public function testFid() |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * @dataProvider valueTypesProvider |
||
| 208 | */ |
||
| 209 | public function testCreatingFromArray($value, $key) |
||
| 220 | |||
| 221 | } |
||
| 222 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.