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 |
||
| 12 | class PercentilesTest extends BaseAggregationTest |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @group functional |
||
| 16 | */ |
||
| 17 | public function testConstruct(): void |
||
| 18 | { |
||
| 19 | $aggr = new Percentiles('price_percentile'); |
||
| 20 | $this->assertEquals('price_percentile', $aggr->getName()); |
||
| 21 | |||
| 22 | $aggr = new Percentiles('price_percentile', 'price'); |
||
| 23 | $this->assertEquals('price', $aggr->getParam('field')); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @group functional |
||
| 28 | */ |
||
| 29 | public function testSetField(): void |
||
| 30 | { |
||
| 31 | $aggr = new Percentiles('price_percentile'); |
||
| 32 | $aggr->setField('price'); |
||
| 33 | |||
| 34 | $this->assertEquals('price', $aggr->getParam('field')); |
||
| 35 | $this->assertInstanceOf(Percentiles::class, $aggr->setField('price')); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @group unit |
||
| 40 | */ |
||
| 41 | View Code Duplication | public function testCompression(): void |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @group unit |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function testHdr(): void |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @group functional |
||
| 84 | */ |
||
| 85 | public function testSetPercents(): void |
||
| 86 | { |
||
| 87 | $percents = [1, 2, 3]; |
||
| 88 | $aggr = new Percentiles('price_percentile'); |
||
| 89 | $aggr->setPercents($percents); |
||
| 90 | $this->assertEquals($percents, $aggr->getParam('percents')); |
||
| 91 | $this->assertInstanceOf(Percentiles::class, $aggr->setPercents($percents)); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @group functional |
||
| 96 | */ |
||
| 97 | public function testAddPercent(): void |
||
| 98 | { |
||
| 99 | $percents = [1, 2, 3]; |
||
| 100 | $aggr = new Percentiles('price_percentile'); |
||
| 101 | $aggr->setPercents($percents); |
||
| 102 | $this->assertEquals($percents, $aggr->getParam('percents')); |
||
| 103 | $aggr->addPercent(4); |
||
| 104 | $percents[] = 4; |
||
| 105 | $this->assertEquals($percents, $aggr->getParam('percents')); |
||
| 106 | $this->assertInstanceOf(Percentiles::class, $aggr->addPercent(4)); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @group functional |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function testSetScript(): void |
|
| 113 | { |
||
| 114 | $script = 'doc["load_time"].value / 20'; |
||
| 115 | $aggr = new Percentiles('price_percentile'); |
||
| 116 | $aggr->setScript($script); |
||
| 117 | $this->assertEquals($script, $aggr->getParam('script')); |
||
| 118 | $this->assertInstanceOf(Percentiles::class, $aggr->setScript($script)); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @group functional |
||
| 123 | */ |
||
| 124 | public function testActualWork(): void |
||
| 125 | { |
||
| 126 | // prepare |
||
| 127 | $index = $this->_createIndex(); |
||
| 128 | $index->addDocuments([ |
||
| 129 | new Document(1, ['price' => 100]), |
||
| 130 | new Document(2, ['price' => 200]), |
||
| 131 | new Document(3, ['price' => 300]), |
||
| 132 | new Document(4, ['price' => 400]), |
||
| 133 | new Document(5, ['price' => 500]), |
||
| 134 | new Document(6, ['price' => 600]), |
||
| 135 | new Document(7, ['price' => 700]), |
||
| 136 | new Document(8, ['price' => 800]), |
||
| 137 | new Document(9, ['price' => 900]), |
||
| 138 | new Document(10, ['price' => 1000]), |
||
| 139 | ]); |
||
| 140 | $index->refresh(); |
||
| 141 | |||
| 142 | // execute |
||
| 143 | $aggr = new Percentiles('price_percentile'); |
||
| 144 | $aggr->setField('price'); |
||
| 145 | |||
| 146 | $query = new Query(); |
||
| 147 | $query->addAggregation($aggr); |
||
| 148 | |||
| 149 | $resultSet = $index->search($query); |
||
| 150 | $aggrResult = $resultSet->getAggregation('price_percentile'); |
||
| 151 | |||
| 152 | $this->assertEquals(100.0, $aggrResult['values']['1.0']); |
||
| 153 | $this->assertEquals(100.0, $aggrResult['values']['5.0']); |
||
| 154 | $this->assertEquals(300.0, $aggrResult['values']['25.0']); |
||
| 155 | $this->assertEquals(550.0, $aggrResult['values']['50.0']); |
||
| 156 | $this->assertEquals(800.0, $aggrResult['values']['75.0']); |
||
| 157 | $this->assertEquals(1000.0, $aggrResult['values']['95.0']); |
||
| 158 | $this->assertEquals(1000.0, $aggrResult['values']['99.0']); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @group functional |
||
| 163 | */ |
||
| 164 | public function testKeyed(): void |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @group unit |
||
| 231 | */ |
||
| 232 | public function testMissing(): void |
||
| 252 | } |
||
| 253 |
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.