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 |
||
17 | class SerializerTest extends WebTestCase |
||
18 | { |
||
19 | View Code Duplication | public function testMappingIteratorToArrayField() |
|
34 | |||
35 | /** |
||
36 | * Tests that the serialize_null configuration attribute works |
||
37 | */ |
||
38 | public function testWithNullValues() |
||
39 | { |
||
40 | $client = $this->createClient(array('test_case' => 'Serializer')); |
||
41 | $container = $client->getContainer(); |
||
42 | |||
43 | $disabledNullPersister = $container->get('fos_elastica.object_persister.index.type_serialize_null_disabled'); |
||
44 | $enabledNullPersister = $container->get('fos_elastica.object_persister.index.type_serialize_null_enabled'); |
||
45 | |||
46 | $object = new TypeObj(); |
||
47 | $object->id = 1; |
||
48 | $object->field1 = null; |
||
49 | $disabledNullPersister->insertOne($object); |
||
50 | $enabledNullPersister->insertOne($object); |
||
51 | |||
52 | // Tests that attributes with null values are not persisted into an Elasticsearch type without the serialize_null option |
||
53 | $disabledNullType = $container->get('fos_elastica.index.index.type_serialize_null_disabled'); |
||
54 | $documentData = $disabledNullType->getDocument(1)->getData(); |
||
55 | $this->assertArrayNotHasKey('field1', $documentData); |
||
56 | |||
57 | // Tests that attributes with null values are persisted into an Elasticsearch type with the serialize_null option |
||
58 | $enabledNullType = $container->get('fos_elastica.index.index.type_serialize_null_enabled'); |
||
59 | $documentData = $enabledNullType->getDocument(1)->getData(); |
||
60 | $this->assertArrayHasKey('field1', $documentData); |
||
61 | $this->assertEquals($documentData['field1'], null); |
||
62 | } |
||
63 | |||
64 | public function testUnmappedType() |
||
70 | |||
71 | public function testIfEventOnPostTransformEventIsCalled() |
||
89 | |||
90 | protected function setUp() |
||
96 | |||
97 | protected function tearDown() |
||
103 | } |
||
104 |
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.