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 DateRangeTest extends BaseAggregationTest |
||
16 | { |
||
17 | /** |
||
18 | * @group unit |
||
19 | */ |
||
20 | View Code Duplication | public function testSetTimezone(): void |
|
|
|||
21 | { |
||
22 | $agg = (new DateRange('name')) |
||
23 | ->setField('date') |
||
24 | ->setTimezone('CET') |
||
25 | ; |
||
26 | |||
27 | $expected = [ |
||
28 | 'date_range' => [ |
||
29 | 'field' => 'date', |
||
30 | 'time_zone' => 'CET', |
||
31 | ], |
||
32 | ]; |
||
33 | |||
34 | $this->assertEquals($expected, $agg->toArray()); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @group functional |
||
39 | */ |
||
40 | public function testDateRangeAggregation(): void |
||
58 | |||
59 | /** |
||
60 | * @group functional |
||
61 | */ |
||
62 | View Code Duplication | public function testDateRangeKeyedAggregation(): void |
|
79 | |||
80 | /** |
||
81 | * @group functional |
||
82 | */ |
||
83 | public function testDateRangeSetFormat(): void |
||
96 | |||
97 | /** |
||
98 | * @group functional |
||
99 | */ |
||
100 | public function testDateRangeSetFormatAccordingToFormatTargetField(): void |
||
120 | |||
121 | View Code Duplication | protected function _getIndexForTest(): Index |
|
138 | } |
||
139 |
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.