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 |
||
18 | class ResetterTest extends \PHPUnit_Framework_TestCase |
||
19 | { |
||
20 | /** |
||
21 | * @var Resetter |
||
22 | */ |
||
23 | private $resetter; |
||
24 | |||
25 | private $aliasProcessor; |
||
26 | private $configManager; |
||
27 | private $dispatcher; |
||
28 | private $elasticaClient; |
||
29 | private $indexManager; |
||
30 | private $mappingBuilder; |
||
31 | |||
32 | public function testResetAllIndexes() |
||
56 | |||
57 | public function testResetIndex() |
||
76 | |||
77 | public function testResetIndexWithDifferentName() |
||
97 | |||
98 | public function testResetIndexWithDifferentNameAndAlias() |
||
123 | |||
124 | /** |
||
125 | * @expectedException \InvalidArgumentException |
||
126 | */ |
||
127 | View Code Duplication | public function testFailureWhenMissingIndexDoesntDispatch() |
|
139 | |||
140 | public function testResetType() |
||
141 | { |
||
142 | $typeConfig = new TypeConfig('type', array(), array()); |
||
143 | $indexConfig = new IndexConfig('index', array(), array()); |
||
144 | $this->mockType('type', 'index', $typeConfig, $indexConfig); |
||
145 | |||
146 | $this->dispatcherExpects(array( |
||
147 | array(IndexResetEvent::PRE_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
||
148 | array(IndexResetEvent::POST_INDEX_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\IndexResetEvent')), |
||
149 | array(TypeResetEvent::PRE_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')), |
||
150 | array(TypeResetEvent::POST_TYPE_RESET, $this->isInstanceOf('FOS\\ElasticaBundle\\Event\\TypeResetEvent')) |
||
151 | )); |
||
152 | |||
153 | $this->elasticaClient->expects($this->exactly(3)) |
||
154 | ->method('request') |
||
155 | ->withConsecutive( |
||
156 | array('index/', 'DELETE'), |
||
157 | array('index/', 'PUT', array(), array()), |
||
158 | array('index/type/_mapping', 'PUT', array('type' => array()), array()) |
||
159 | ); |
||
160 | |||
161 | $this->resetter->resetIndexType('index', 'type'); |
||
162 | } |
||
163 | |||
164 | public function testResetTypeWithChangedSettings() |
||
165 | { |
||
166 | $settingsValue = array( |
||
167 | 'analysis' => array( |
||
168 | 'analyzer' => array( |
||
169 | 'test_analyzer' => array( |
||
170 | 'type' => 'standard', |
||
171 | 'tokenizer' => 'standard' |
||
172 | ) |
||
173 | ) |
||
174 | ) |
||
175 | ); |
||
176 | $typeConfig = new TypeConfig('type', array(), array()); |
||
177 | $indexConfig = new IndexConfig('index', array(), array('settings' => $settingsValue)); |
||
178 | $this->mockType('type', 'index', $typeConfig, $indexConfig); |
||
179 | |||
180 | $this->elasticaClient->expects($this->exactly(3)) |
||
181 | ->method('request') |
||
182 | ->withConsecutive( |
||
183 | array('index/', 'DELETE'), |
||
184 | array('index/', 'PUT', array(), array()), |
||
185 | array('index/type/_mapping', 'PUT', array('type' => array()), array()) |
||
186 | ); |
||
187 | |||
188 | $this->resetter->resetIndexType('index', 'type'); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @expectedException \InvalidArgumentException |
||
193 | */ |
||
194 | View Code Duplication | public function testNonExistantResetType() |
|
206 | |||
207 | public function testPostPopulateWithoutAlias() |
||
218 | |||
219 | public function testPostPopulate() |
||
230 | |||
231 | private function dispatcherExpects(array $events) |
||
232 | { |
||
233 | $expectation = $this->dispatcher->expects($this->exactly(count($events))) |
||
234 | ->method('dispatch'); |
||
235 | |||
236 | call_user_func_array(array($expectation, 'withConsecutive'), $events); |
||
237 | } |
||
238 | |||
239 | private function mockIndex($indexName, IndexConfig $config, $mapping = array()) |
||
240 | { |
||
241 | $this->configManager->expects($this->atLeast(1)) |
||
242 | ->method('getIndexConfiguration') |
||
243 | ->with($indexName) |
||
244 | ->will($this->returnValue($config)); |
||
245 | $index = new Index($this->elasticaClient, $indexName); |
||
246 | $this->indexManager->expects($this->any()) |
||
247 | ->method('getIndex') |
||
248 | ->with($indexName) |
||
249 | ->willReturn($index); |
||
250 | $this->mappingBuilder->expects($this->any()) |
||
251 | ->method('buildIndexMapping') |
||
252 | ->with($config) |
||
253 | ->willReturn($mapping); |
||
254 | |||
255 | return $index; |
||
256 | } |
||
257 | |||
258 | private function mockType($typeName, $indexName, TypeConfig $typeConfig, IndexConfig $indexConfig, $mapping = array()) |
||
259 | { |
||
260 | $this->configManager->expects($this->atLeast(1)) |
||
261 | ->method('getTypeConfiguration') |
||
262 | ->with($indexName, $typeName) |
||
263 | ->will($this->returnValue($typeConfig)); |
||
264 | $index = new Index($this->elasticaClient, $indexName); |
||
265 | $this->indexManager->expects($this->atLeast(2)) |
||
266 | ->method('getIndex') |
||
267 | ->with($indexName) |
||
268 | ->willReturn($index); |
||
269 | $this->configManager->expects($this->atLeast(1)) |
||
270 | ->method('getIndexConfiguration') |
||
271 | ->with($indexName) |
||
272 | ->will($this->returnValue($indexConfig)); |
||
273 | $this->mappingBuilder->expects($this->any()) |
||
274 | ->method('buildIndexMapping') |
||
275 | ->with($indexConfig) |
||
276 | ->willReturn($mapping); |
||
277 | $this->mappingBuilder->expects($this->once()) |
||
278 | ->method('buildTypeMapping') |
||
279 | ->with($typeConfig) |
||
280 | ->willReturn($mapping); |
||
281 | |||
282 | return $index; |
||
283 | } |
||
284 | |||
285 | protected function setUp() |
||
313 | } |
||
314 |
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.