| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Elastica\Test\Aggregation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Elastica\Aggregation\GeohashGrid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Elastica\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Elastica\Index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Elastica\Mapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Elastica\Query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class GeohashGridTest extends BaseAggregationTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @group functional | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 19 |  | View Code Duplication |     public function testGeohashGridAggregationWithNumericalPrecision(): void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $agg = new GeohashGrid('hash', 'location'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $agg->setPrecision(3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $query = new Query(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $query->addAggregation($agg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $results = $this->_getIndexForTest()->search($query)->getAggregation('hash'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->assertEquals(2, $results['buckets'][0]['doc_count']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->assertEquals(1, $results['buckets'][1]['doc_count']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @group functional | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 35 |  | View Code Duplication |     public function testGeohashGridAggregationWithDistancePrecision(): void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $agg = new GeohashGrid('hash', 'location'); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $agg->setPrecision('100km'); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $query = new Query(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $query->addAggregation($agg); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $results = $this->_getIndexForTest()->search($query)->getAggregation('hash'); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $this->assertEquals(2, $results['buckets'][0]['doc_count']); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertEquals(1, $results['buckets'][1]['doc_count']); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @group functional | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function testGeohashGridAggregationWithNotAllowedPrecision(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->expectException(\TypeError::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $agg = new GeohashGrid('hash', 'location'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $agg->setPrecision(1.5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $query = new Query(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $query->addAggregation($agg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->_createIndex()->search($query)->getAggregation('hash'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |     protected function _getIndexForTest(): Index | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $index = $this->_createIndex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $index->setMapping(new Mapping([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'location' => ['type' => 'geo_point'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $index->addDocuments([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             new Document(1, ['location' => ['lat' => 32.849437, 'lon' => -117.271732]]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             new Document(2, ['location' => ['lat' => 32.798320, 'lon' => -117.246648]]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             new Document(3, ['location' => ['lat' => 37.782439, 'lon' => -122.392560]]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $index->refresh(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  | 
            
                        
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.