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 |
||
19 | class MappingToElasticaTest extends WebTestCase |
||
20 | { |
||
21 | public function testResetIndexAddsMappings() |
||
22 | { |
||
23 | $client = $this->createClient(array('test_case' => 'Basic')); |
||
24 | $resetter = $this->getResetter($client); |
||
25 | $resetter->resetIndex('index'); |
||
26 | |||
27 | $type = $this->getType($client); |
||
28 | $mapping = $type->getMapping(); |
||
29 | |||
30 | $this->assertNotEmpty($mapping, 'Mapping was populated'); |
||
31 | $this->assertArrayHasKey('store', $mapping['type']['properties']['field1']); |
||
32 | $this->assertTrue($mapping['type']['properties']['field1']['store']); |
||
33 | $this->assertArrayNotHasKey('store', $mapping['type']['properties']['field2']); |
||
34 | |||
35 | $type = $this->getType($client, 'type'); |
||
36 | $mapping = $type->getMapping(); |
||
37 | $this->assertEquals('parent', $mapping['type']['_parent']['type']); |
||
38 | |||
39 | $parent = $this->getType($client, 'parent'); |
||
40 | $mapping = $parent->getMapping(); |
||
41 | |||
42 | $this->assertEquals('my_analyzer', $mapping['parent']['index_analyzer']); |
||
43 | $this->assertEquals('whitespace', $mapping['parent']['search_analyzer']); |
||
44 | } |
||
45 | |||
46 | public function testResetType() |
||
47 | { |
||
48 | $client = $this->createClient(array('test_case' => 'Basic')); |
||
49 | $resetter = $this->getResetter($client); |
||
50 | $resetter->resetIndexType('index', 'type'); |
||
51 | |||
52 | $type = $this->getType($client); |
||
53 | $mapping = $type->getMapping(); |
||
54 | |||
55 | $this->assertNotEmpty($mapping, 'Mapping was populated'); |
||
56 | $this->assertFalse($mapping['type']['date_detection']); |
||
57 | $this->assertTrue($mapping['type']['numeric_detection']); |
||
58 | $this->assertEquals(array('yyyy-MM-dd'), $mapping['type']['dynamic_date_formats']); |
||
59 | $this->assertArrayHasKey('store', $mapping['type']['properties']['field1']); |
||
60 | $this->assertTrue($mapping['type']['properties']['field1']['store']); |
||
61 | $this->assertArrayNotHasKey('store', $mapping['type']['properties']['field2']); |
||
62 | } |
||
63 | |||
64 | View Code Duplication | public function testORMResetIndexAddsMappings() |
|
|
|||
65 | { |
||
66 | $client = $this->createClient(array('test_case' => 'ORM')); |
||
67 | $resetter = $this->getResetter($client); |
||
68 | $resetter->resetIndex('index'); |
||
69 | |||
70 | $type = $this->getType($client); |
||
71 | $mapping = $type->getMapping(); |
||
72 | |||
73 | $this->assertNotEmpty($mapping, 'Mapping was populated'); |
||
74 | } |
||
75 | |||
76 | View Code Duplication | public function testORMResetType() |
|
77 | { |
||
78 | $client = $this->createClient(array('test_case' => 'ORM')); |
||
79 | $resetter = $this->getResetter($client); |
||
80 | $resetter->resetIndexType('index', 'type'); |
||
81 | |||
82 | $type = $this->getType($client); |
||
83 | $mapping = $type->getMapping(); |
||
84 | |||
85 | $this->assertNotEmpty($mapping, 'Mapping was populated'); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function testMappingIteratorToArrayField() |
|
89 | { |
||
90 | $client = $this->createClient(array('test_case' => 'ORM')); |
||
91 | $persister = $client->getContainer()->get('fos_elastica.object_persister.index.type'); |
||
92 | |||
93 | $object = new TypeObj(); |
||
94 | $object->id = 1; |
||
95 | $object->coll = new \ArrayIterator(array('foo', 'bar')); |
||
96 | $persister->insertOne($object); |
||
97 | |||
98 | $object->coll = new \ArrayIterator(array('foo', 'bar', 'bazz')); |
||
99 | $object->coll->offsetUnset(1); |
||
100 | |||
101 | $persister->replaceOne($object); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param Client $client |
||
106 | * |
||
107 | * @return \FOS\ElasticaBundle\Resetter $resetter |
||
108 | */ |
||
109 | private function getResetter(Client $client) |
||
113 | |||
114 | /** |
||
115 | * @param Client $client |
||
116 | * @param string $type |
||
117 | * |
||
118 | * @return \Elastica\Type |
||
119 | */ |
||
120 | private function getType(Client $client, $type = 'type') |
||
124 | |||
125 | protected function setUp() |
||
126 | { |
||
127 | parent::setUp(); |
||
128 | |||
129 | $this->deleteTmpDir('Basic'); |
||
132 | |||
133 | protected function tearDown() |
||
140 | } |
||
141 |
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.