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://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-bucket-sampler-aggregation.html |
||
| 23 | */ |
||
| 24 | class SamplerAggregation 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 | $this->setField($field); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function getShardSize(): ?int |
||
| 39 | { |
||
| 40 | return $this->shardSize; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function setShardSize(?int $shardSize): static |
||
| 44 | { |
||
| 45 | $this->shardSize = $shardSize; |
||
| 46 | |||
| 47 | return $this; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function getType(): string |
||
| 51 | { |
||
| 52 | return 'sampler'; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getArray(): array |
||
| 56 | { |
||
| 57 | return array_filter( |
||
| 58 | [ |
||
| 59 | 'field' => $this->getField(), |
||
| 60 | 'shard_size' => $this->getShardSize(), |
||
| 61 | ] |
||
| 62 | ); |
||
| 63 | |||
| 64 | } |
||
| 65 | } |
||
| 66 |