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 CompositeTest extends BaseAggregationTest |
||
15 | { |
||
16 | /** |
||
17 | * @group unit |
||
18 | */ |
||
19 | public function testSize(): void |
||
32 | |||
33 | /** |
||
34 | * @group unit |
||
35 | */ |
||
36 | public function testAddSource(): void |
||
56 | |||
57 | /** |
||
58 | * @group unit |
||
59 | */ |
||
60 | public function testAddAfter(): void |
||
73 | |||
74 | /** |
||
75 | * @group functional |
||
76 | */ |
||
77 | public function testCompositeNoAfterAggregation(): void |
||
114 | |||
115 | /** |
||
116 | * @group functional |
||
117 | */ |
||
118 | View Code Duplication | public function testCompositeWithSizeAggregation(): void |
|
|
|||
119 | { |
||
120 | $composite = new Composite('products'); |
||
121 | $composite->setSize(2); |
||
122 | $composite->addSource((new Terms('color'))->setField('color.keyword')); |
||
123 | |||
124 | $query = new Query(); |
||
125 | $query->addAggregation($composite); |
||
126 | |||
127 | $results = $this->_getIndexForTest()->search($query)->getAggregation('products'); |
||
128 | $expected = [ |
||
129 | 'after_key' => [ |
||
130 | 'color' => 'green', |
||
131 | ], |
||
132 | 'buckets' => [ |
||
133 | [ |
||
134 | 'key' => [ |
||
135 | 'color' => 'blue', |
||
136 | ], |
||
137 | 'doc_count' => 2, |
||
138 | ], |
||
139 | [ |
||
140 | 'key' => [ |
||
141 | 'color' => 'green', |
||
142 | ], |
||
143 | 'doc_count' => 1, |
||
144 | ], |
||
145 | ], |
||
146 | ]; |
||
147 | |||
148 | $this->assertEquals($expected, $results); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @group functional |
||
153 | */ |
||
154 | public function testCompositeWithAfterAggregation(): void |
||
180 | |||
181 | /** |
||
182 | * @group functional |
||
183 | */ |
||
184 | View Code Duplication | public function testCompositeWithNullAfter(): void |
|
217 | |||
218 | View Code Duplication | protected function _getIndexForTest(): Index |
|
233 | } |
||
234 |
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.