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/current/search-aggregations-bucket-daterange-aggregation.html |
||
| 23 | */ |
||
| 24 | class DateRangeAggregation extends AbstractAggregation |
||
| 25 | { |
||
| 26 | use BucketingTrait; |
||
| 27 | |||
| 28 | private array $ranges = []; |
||
|
|
|||
| 29 | |||
| 30 | private bool $keyed = false; |
||
| 31 | |||
| 32 | public function __construct( |
||
| 33 | private string $name, |
||
| 34 | private ?string $field = null, |
||
| 35 | private ?string $format = null, |
||
| 36 | array $ranges = [], |
||
| 37 | bool $keyed = false |
||
| 38 | ) { |
||
| 39 | parent::__construct($name); |
||
| 40 | |||
| 41 | $this->setField($field); |
||
| 42 | $this->setFormat($format); |
||
| 43 | $this->setKeyed($keyed); |
||
| 44 | foreach ($ranges as $range) { |
||
| 45 | $from = isset($range['from']) ? $range['from'] : null; |
||
| 46 | $to = isset($range['to']) ? $range['to'] : null; |
||
| 47 | $key = isset($range['key']) ? $range['key'] : null; |
||
| 48 | $this->addRange($from, $to, $key); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | public function setKeyed($keyed): static |
||
| 53 | { |
||
| 54 | $this->keyed = $keyed; |
||
| 55 | |||
| 56 | return $this; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getFormat(): string |
||
| 60 | { |
||
| 61 | return $this->format; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function setFormat($format): static |
||
| 65 | { |
||
| 66 | $this->format = $format; |
||
| 67 | |||
| 68 | return $this; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function addRange(?string $from = null, ?string $to = null, ?string $key = null): static |
||
| 72 | { |
||
| 73 | $range = array_filter( |
||
| 74 | [ |
||
| 75 | 'from' => $from, |
||
| 76 | 'to' => $to, |
||
| 77 | 'key' => $key, |
||
| 78 | ], |
||
| 79 | fn(mixed $v): bool => !is_null($v) |
||
| 80 | ); |
||
| 81 | |||
| 82 | if (empty($range)) { |
||
| 83 | throw new \LogicException('Either from or to must be set. Both cannot be null.'); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->ranges[] = $range; |
||
| 87 | |||
| 88 | return $this; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function getArray(): array |
||
| 92 | { |
||
| 93 | if (!$this->getField() || !$this->getFormat() || empty($this->ranges)) { |
||
| 94 | throw new \LogicException('Date range aggregation must have field, format set and range added.'); |
||
| 95 | } |
||
| 96 | |||
| 97 | return [ |
||
| 98 | 'format' => $this->getFormat(), |
||
| 99 | 'field' => $this->getField(), |
||
| 100 | 'ranges' => $this->ranges, |
||
| 101 | 'keyed' => $this->keyed, |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getType(): string |
||
| 106 | { |
||
| 107 | return 'date_range'; |
||
| 108 | } |
||
| 109 | } |
||
| 110 |