Completed
Pull Request — master (#348)
by
unknown
09:42 queued 08:20
created

RangeAggregation   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 148
Duplicated Lines 8.78 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 13
loc 148
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
15
16
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
17
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
18
19
/**
20
 * Class representing RangeAggregation.
21
 *
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 = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
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