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 |
||
14 | class CardinalityTest extends BaseAggregationTest |
||
15 | { |
||
16 | /** |
||
17 | * @group functional |
||
18 | */ |
||
19 | View Code Duplication | public function testCardinalityAggregation(): void |
|
|
|||
20 | { |
||
21 | $agg = new Cardinality('cardinality'); |
||
22 | $agg->setField('color'); |
||
23 | |||
24 | $query = new Query(); |
||
25 | $query->addAggregation($agg); |
||
26 | $results = $this->_getIndexForTest()->search($query)->getAggregation('cardinality'); |
||
27 | |||
28 | $this->assertEquals(3, $results['value']); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @group functional |
||
33 | */ |
||
34 | View Code Duplication | public function testCardinalityAggregationWithMissing(): void |
|
46 | |||
47 | public function validPrecisionThresholdProvider() |
||
56 | |||
57 | /** |
||
58 | * @dataProvider validPrecisionThresholdProvider |
||
59 | * @group unit |
||
60 | */ |
||
61 | public function testPrecisionThreshold(int $threshold): void |
||
69 | |||
70 | /** |
||
71 | * @dataProvider validRehashProvider |
||
72 | * @group unit |
||
73 | * |
||
74 | * @param bool $rehash |
||
75 | */ |
||
76 | public function testRehash($rehash): void |
||
84 | |||
85 | public function validRehashProvider() |
||
92 | |||
93 | View Code Duplication | protected function _getIndexForTest(): Index |
|
114 | } |
||
115 |
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.