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

CardinalityAggregation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 18
loc 99
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
 * Difference values counter.
22
 *
23
 * @link http://goo.gl/tG7ciG
24
 */
25
class CardinalityAggregation extends AbstractAggregation
26
{
27
    use MetricTrait;
28
29
    use ScriptAwareTrait;
30
31
    private ?int $precisionThreshold = null;
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 '?', expecting T_FUNCTION or T_CONST
Loading history...
32
33
    private ?bool $rehash = null;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getArray(): array
39
    {
40
        $out = array_filter(
41
            [
42
                'field' => $this->getField(),
43
                'script' => $this->getScript(),
44
                'precision_threshold' => $this->getPrecisionThreshold(),
45
                'rehash' => $this->isRehash(),
46
            ],
47
            fn(mixed $val): bool => $val || is_bool($val)
48
        );
49
50
        $this->checkRequiredFields($out);
51
52
        return $out;
53
    }
54
55
    public function setPrecisionThreshold(?int $precision): static
56
    {
57
        $this->precisionThreshold = $precision;
58
59
        return $this;
60
    }
61
62
    public function getPrecisionThreshold(): ?int
63
    {
64
        return $this->precisionThreshold;
65
    }
66
67
    public function isRehash(): ?bool
68
    {
69
        return $this->rehash;
70
    }
71
72
    public function setRehash(?bool $rehash): static
73
    {
74
        $this->rehash = $rehash;
75
76
        return $this;
77
    }
78
79
    public function getType(): string
80
    {
81
        return 'cardinality';
82
    }
83
84
    private function checkRequiredFields(array $fields): void
85
    {
86
        if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) {
87
            throw new \LogicException('Cardinality aggregation must have field or script set.');
88
        }
89
    }
90
}
91