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 ReadOnlyTest extends SerializerTestCase |
||
9 | { |
||
10 | public function testSerialize() |
||
11 | { |
||
12 | $data = $this->serialize(new Car(true)); |
||
13 | |||
14 | $this->assertTrue(isset($data['model'])); |
||
15 | $this->assertTrue(isset($data['size'])); |
||
16 | } |
||
17 | |||
18 | public function testDeserialize() |
||
19 | { |
||
20 | $data = ['model' => 'model_value', 'size' => 'size_value']; |
||
21 | $obj = $this->deserialize($data, Car::class); |
||
22 | |||
23 | $this->assertPropertyValue($obj, 'model', null); |
||
24 | $this->assertPropertyValue($obj, 'size', 'size_value'); |
||
25 | } |
||
26 | |||
27 | public function testSerializeReadOnlyClass() |
||
28 | { |
||
29 | $data = $this->serialize(new ClassReadOnly(true)); |
||
30 | |||
31 | $this->assertTrue(isset($data['model'])); |
||
32 | $this->assertTrue(isset($data['size'])); |
||
33 | $this->assertTrue(isset($data['color'])); |
||
34 | } |
||
35 | |||
36 | public function testDeserializeReadOnlyClass() |
||
37 | { |
||
38 | $data = ['model' => 'model_value', 'size' => 'size_value', 'color' => 'color_value']; |
||
39 | $obj = $this->deserialize($data, ClassReadOnly::class); |
||
40 | |||
41 | $this->assertPropertyValue($obj, 'model', 'model_value'); |
||
42 | $this->assertPropertyValue($obj, 'size', null); |
||
43 | $this->assertPropertyValue($obj, 'color', null); |
||
44 | } |
||
45 | } |
||
46 |