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-range-aggregation.html |
||
23 | */ |
||
24 | class RangeAggregation 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 | array $ranges = [], |
||
36 | bool $keyed = false |
||
37 | ) { |
||
38 | parent::__construct($name); |
||
39 | |||
40 | $this->setField($field); |
||
41 | $this->setKeyed($keyed); |
||
42 | |||
43 | foreach ($ranges as $range) { |
||
44 | $from = isset($range['from']) ? $range['from'] : null; |
||
45 | $to = isset($range['to']) ? $range['to'] : null; |
||
46 | $key = isset($range['key']) ? $range['key'] : null; |
||
47 | $this->addRange($from, $to, $key); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | public function setKeyed(bool $keyed): static |
||
52 | { |
||
53 | $this->keyed = $keyed; |
||
54 | |||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | public function addRange(mixed $from = null, mixed $to = null, string $key = ''): static |
||
59 | { |
||
60 | $range = array_filter( |
||
61 | [ |
||
62 | 'from' => $from, |
||
63 | 'to' => $to, |
||
64 | ] |
||
65 | ); |
||
66 | |||
67 | if (!empty($key)) { |
||
68 | $range['key'] = $key; |
||
69 | } |
||
70 | |||
71 | $this->ranges[] = $range; |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | public function removeRange(int|float|null $from, int|float|null $to): bool |
||
77 | { |
||
78 | foreach ($this->ranges as $key => $range) { |
||
79 | if (array_diff_assoc(array_filter(['from' => $from, 'to' => $to]), $range) === []) { |
||
80 | unset($this->ranges[$key]); |
||
81 | |||
82 | return true; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | return false; |
||
87 | } |
||
88 | |||
89 | public function removeRangeByKey(string $key): bool |
||
90 | { |
||
91 | if ($this->keyed) { |
||
92 | foreach ($this->ranges as $rangeKey => $range) { |
||
93 | if (array_key_exists('key', $range) && $range['key'] === $key) { |
||
94 | unset($this->ranges[$rangeKey]); |
||
95 | |||
96 | return true; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return false; |
||
102 | } |
||
103 | |||
104 | public function getArray(): array |
||
105 | { |
||
106 | $data = [ |
||
107 | 'keyed' => $this->keyed, |
||
108 | 'ranges' => array_values($this->ranges), |
||
109 | ]; |
||
110 | |||
111 | if ($this->getField()) { |
||
112 | $data['field'] = $this->getField(); |
||
113 | } |
||
114 | |||
115 | return $data; |
||
116 | } |
||
117 | |||
118 | public function getType(): string |
||
119 | { |
||
120 | return 'range'; |
||
121 | } |
||
122 | } |
||
123 |