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() |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Test if various value types are properly handled |
||
| 133 | * @dataProvider rangeValueTypesProvider |
||
| 134 | */ |
||
| 135 | public function testRangeValues($testRange, $rangeKey) |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Test forced value type - datetime |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function testDatetimeValue() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @expectedException Radowoj\Yaah\Exception |
||
| 176 | * @expectedExceptionMessage Not supported value type: object; fid=1 |
||
| 177 | */ |
||
| 178 | public function testExceptionOnInvalidValue() |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * @expectedException Radowoj\Yaah\Exception |
||
| 186 | * @expectedExceptionMessage Class Radowoj\Yaah\Field does not have property: fvalueUnicorn |
||
| 187 | */ |
||
| 188 | public function testExceptionOnInvalidForcedValue() |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Test fid value |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function testFid() |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * @dataProvider valueTypesProvider |
||
| 207 | */ |
||
| 208 | public function testCreatingFromArray($value, $key) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @expectedException Radowoj\Yaah\Exception |
||
| 221 | * @expectedExceptionMessage Range array must have exactly 2 elements |
||
| 222 | */ |
||
| 223 | public function testExceptionOnInvalidRangeArrayItemCount() |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * @expectedException Radowoj\Yaah\Exception |
||
| 231 | * @expectedExceptionMessage Fid is required |
||
| 232 | */ |
||
| 233 | public function testExceptionOnFromArrayMissingFid() |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * @expectedException Radowoj\Yaah\Exception |
||
| 244 | * @expectedExceptionMessage Unknown Field property: thisKeyIsInvalid |
||
| 245 | */ |
||
| 246 | public function testExceptionOnFromArrayInvalidArrayKey() |
||
| 253 | |||
| 254 | |||
| 255 | public function testNullReturnValueWhenAllFieldsAreDefault() |
||
| 261 | |||
| 262 | |||
| 263 | } |
||
| 264 |
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.