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 |
||
| 22 | * @link https://goo.gl/yzXvqD |
||
| 23 | */ |
||
| 24 | class DiversifiedSamplerAggregation extends AbstractAggregation |
||
| 25 | { |
||
| 26 | use BucketingTrait; |
||
| 27 | |||
| 28 | public function __construct( |
||
| 29 | private string $name, |
||
|
|
|||
| 30 | private ?string $field = null, |
||
| 31 | private ?int $shardSize = null |
||
| 32 | ) { |
||
| 33 | parent::__construct($name); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function getShardSize(): int |
||
| 37 | { |
||
| 38 | return $this->shardSize; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function setShardSize(int $shardSize): static |
||
| 42 | { |
||
| 43 | $this->shardSize = $shardSize; |
||
| 44 | |||
| 45 | return $this; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function getType(): string |
||
| 49 | { |
||
| 50 | return 'diversified_sampler'; |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function getArray(): array |
||
| 54 | { |
||
| 55 | return array_filter( |
||
| 56 | [ |
||
| 57 | 'field' => $this->getField(), |
||
| 58 | 'shard_size' => $this->getShardSize(), |
||
| 59 | ] |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 |