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 |
||
| 15 | class FiltersTest extends BaseAggregationTest |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @group unit |
||
| 19 | */ |
||
| 20 | View Code Duplication | public function testToArrayUsingNamedFilters(): void |
|
|
|
|||
| 21 | { |
||
| 22 | $expected = [ |
||
| 23 | 'filters' => [ |
||
| 24 | 'filters' => [ |
||
| 25 | '' => [ |
||
| 26 | 'term' => ['color' => ''], |
||
| 27 | ], |
||
| 28 | '0' => [ |
||
| 29 | 'term' => ['color' => '0'], |
||
| 30 | ], |
||
| 31 | 'blue' => [ |
||
| 32 | 'term' => ['color' => 'blue'], |
||
| 33 | ], |
||
| 34 | 'red' => [ |
||
| 35 | 'term' => ['color' => 'red'], |
||
| 36 | ], |
||
| 37 | ], |
||
| 38 | ], |
||
| 39 | 'aggs' => [ |
||
| 40 | 'avg_price' => ['avg' => ['field' => 'price']], |
||
| 41 | ], |
||
| 42 | ]; |
||
| 43 | |||
| 44 | $agg = new Filters('by_color'); |
||
| 45 | |||
| 46 | $agg->addFilter(new Term(['color' => '']), ''); |
||
| 47 | $agg->addFilter(new Term(['color' => '0']), '0'); |
||
| 48 | $agg->addFilter(new Term(['color' => 'blue']), 'blue'); |
||
| 49 | $agg->addFilter(new Term(['color' => 'red']), 'red'); |
||
| 50 | |||
| 51 | $avg = new Avg('avg_price'); |
||
| 52 | $avg->setField('price'); |
||
| 53 | $agg->addAggregation($avg); |
||
| 54 | |||
| 55 | $this->assertEquals($expected, $agg->toArray()); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @group unit |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function testMixNamedAndAnonymousFilters(): void |
|
| 62 | { |
||
| 63 | $this->expectException(\Elastica\Exception\InvalidException::class); |
||
| 64 | $this->expectExceptionMessage('Mix named and anonymous keys are not allowed'); |
||
| 65 | |||
| 66 | $agg = new Filters('by_color'); |
||
| 67 | $agg->addFilter(new Term(['color' => '0']), '0'); |
||
| 68 | $agg->addFilter(new Term(['color' => '0'])); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @group unit |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function testMixAnonymousAndNamedFilters(): void |
|
| 75 | { |
||
| 76 | $this->expectException(\Elastica\Exception\InvalidException::class); |
||
| 77 | $this->expectExceptionMessage('Mix named and anonymous keys are not allowed'); |
||
| 78 | |||
| 79 | $agg = new Filters('by_color'); |
||
| 80 | |||
| 81 | $agg->addFilter(new Term(['color' => '0'])); |
||
| 82 | $agg->addFilter(new Term(['color' => '0']), '0'); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @group unit |
||
| 87 | */ |
||
| 88 | public function testToArrayUsingAnonymousFilters(): void |
||
| 89 | { |
||
| 90 | $expected = [ |
||
| 91 | 'filters' => [ |
||
| 92 | 'filters' => [ |
||
| 93 | [ |
||
| 94 | 'term' => ['color' => 'blue'], |
||
| 95 | ], |
||
| 96 | [ |
||
| 97 | 'term' => ['color' => 'red'], |
||
| 98 | ], |
||
| 99 | ], |
||
| 100 | ], |
||
| 101 | 'aggs' => [ |
||
| 102 | 'avg_price' => ['avg' => ['field' => 'price']], |
||
| 103 | ], |
||
| 104 | ]; |
||
| 105 | |||
| 106 | $agg = new Filters('by_color'); |
||
| 107 | |||
| 108 | $agg->addFilter(new Term(['color' => 'blue'])); |
||
| 109 | $agg->addFilter(new Term(['color' => 'red'])); |
||
| 110 | |||
| 111 | $avg = new Avg('avg_price'); |
||
| 112 | $avg->setField('price'); |
||
| 113 | $agg->addAggregation($avg); |
||
| 114 | |||
| 115 | $this->assertEquals($expected, $agg->toArray()); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @group unit |
||
| 120 | */ |
||
| 121 | public function testToArrayUsingOtherBucket(): void |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @group functional |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function testFilterAggregation(): void |
|
| 149 | { |
||
| 150 | $agg = new Filters('by_color'); |
||
| 151 | $agg->addFilter(new Term(['color' => 'blue']), 'blue'); |
||
| 152 | $agg->addFilter(new Term(['color' => 'red']), 'red'); |
||
| 153 | |||
| 154 | $avg = new Avg('avg_price'); |
||
| 155 | $avg->setField('price'); |
||
| 156 | $agg->addAggregation($avg); |
||
| 157 | |||
| 158 | $query = new Query(); |
||
| 159 | $query->addAggregation($agg); |
||
| 160 | |||
| 161 | $results = $this->_getIndexForTest()->search($query)->getAggregation('by_color'); |
||
| 162 | |||
| 163 | $resultsForBlue = $results['buckets']['blue']; |
||
| 164 | $resultsForRed = $results['buckets']['red']; |
||
| 165 | |||
| 166 | $this->assertEquals(2, $resultsForBlue['doc_count']); |
||
| 167 | $this->assertEquals(1, $resultsForRed['doc_count']); |
||
| 168 | |||
| 169 | $this->assertEquals((5 + 8) / 2, $resultsForBlue['avg_price']['value']); |
||
| 170 | $this->assertEquals(1, $resultsForRed['avg_price']['value']); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @group functional |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function testSetOtherBucket(): void |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @group functional |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function testSetOtherBucketKey(): void |
|
| 229 | |||
| 230 | View Code Duplication | protected function _getIndexForTest(): Index |
|
| 231 | { |
||
| 232 | $index = $this->_createIndex('filter'); |
||
| 233 | |||
| 234 | $index->addDocuments([ |
||
| 245 | } |
||
| 246 |
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.