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 |
||
| 7 | class SerializedNameTest extends SerializerTestCase |
||
| 8 | { |
||
| 9 | public function testSerialize() |
||
| 10 | { |
||
| 11 | $data = $this->serialize(new Car(true)); |
||
| 12 | |||
| 13 | $this->assertTrue(isset($data['super_model'])); |
||
| 14 | $this->assertTrue(isset($data['car_size'])); |
||
| 15 | $this->assertTrue(isset($data['color'])); |
||
| 16 | } |
||
| 17 | |||
| 18 | public function testDeserialize() |
||
| 19 | { |
||
| 20 | $data = ['super_model' => 'model_val', 'car_size' => 'size_val', 'color' => 'color_val']; |
||
| 21 | $obj = $this->deserialize($data, Car::class); |
||
| 22 | |||
| 23 | $this->assertPropertyValue($obj, 'model', 'model_val'); |
||
| 24 | $this->assertPropertyValue($obj, 'carSize', 'size_val'); |
||
| 25 | $this->assertPropertyValue($obj, 'color', 'color_val'); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Test when the json data is named as the properties. They should be ignored. |
||
| 30 | */ |
||
| 31 | public function testDeserializeWhenIgnoringSerializedName() |
||
| 32 | { |
||
| 33 | $data = ['model' => 'model_val', 'carSize' => 'size_val', 'color' => 'color_val']; |
||
| 34 | $obj = $this->deserialize($data, Car::class); |
||
| 35 | |||
| 36 | $this->assertPropertyValue($obj, 'model', null); |
||
| 37 | $this->assertPropertyValue($obj, 'carSize', null); |
||
| 38 | $this->assertPropertyValue($obj, 'color', 'color_val'); |
||
| 39 | } |
||
| 40 | } |
||
| 41 |