Completed
Pull Request — master (#348)
by
unknown
01:15
created

PercentilesAggregation   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 23.28 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 27
loc 116
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\Metric;
15
16
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
17
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait;
18
use ONGR\ElasticsearchDSL\ScriptAwareTrait;
19
20
/**
21
 * Class representing PercentilesAggregation.
22
 *
23
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
24
 */
25
class PercentilesAggregation extends AbstractAggregation
26
{
27
    use MetricTrait;
28
29
    use ScriptAwareTrait;
30
31
    public function __construct(
32
        private string $name,
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_PRIVATE, expecting T_VARIABLE
Loading history...
33
        private ?string $field = null,
34
        private ?array $percents = null,
35
        ?string $script = null,
36
        ?int $compression = null
37
    ) {
38
        parent::__construct($name);
39
40
        $this->setField($field);
41
        $this->setPercents($percents);
42
        $this->setScript($script);
43
        $this->setCompression($compression);
44
    }
45
46
    public function getPercents(): ?array
47
    {
48
        return $this->percents;
49
    }
50
51
    public function setPercents(?array $percents): static
52
    {
53
        $this->percents = $percents;
54
55
        return $this;
56
    }
57
58
    public function getCompression(): ?int
59
    {
60
        return $this->compression;
61
    }
62
63
    public function setCompression(?int $compression): static
64
    {
65
        $this->compression = $compression;
66
67
        return $this;
68
    }
69
70
    public function getType(): string
71
    {
72
        return 'percentiles';
73
    }
74
75
    public function getArray(): array
76
    {
77
        $out = array_filter(
78
            [
79
                'compression' => $this->getCompression(),
80
                'percents' => $this->getPercents(),
81
                'field' => $this->getField(),
82
                'script' => $this->getScript(),
83
            ],
84
            fn(mixed $val): bool => $val || is_numeric($val)
85
        );
86
87
        $this->isRequiredParametersSet($out);
88
89
        return $out;
90
    }
91
92
    private function isRequiredParametersSet(array $a): void
93
    {
94
        if (!array_key_exists('field', $a) && !array_key_exists('script', $a)) {
95
            throw new \LogicException('Percentiles aggregation must have field or script set.');
96
        }
97
    }
98
}
99