Completed
Push — v0.10 ( f42b97...309689 )
by Simonas
8s
created

CardinalityAggregation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 93
loc 93
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setPrecisionThreshold() 4 4 1
A getPrecisionThreshold() 4 4 1
A isRehash() 4 4 1
A setRehash() 4 4 1
A getType() 4 4 1
A getArray() 18 18 2
A checkRequiredFields() 6 6 3

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
namespace ONGR\ElasticsearchBundle\DSL\Aggregation;
13
14
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
15
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;
16
17
/**
18
 * Difference values counter.
19
 */
20 View Code Duplication
class CardinalityAggregation extends AbstractAggregation
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    use MetricTrait;
23
    use ScriptAwareTrait;
24
25
    /**
26
     * @var int
27
     */
28
    private $precisionThreshold;
29
30
    /**
31
     * @var bool
32
     */
33
    private $rehash;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getArray()
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
            function ($val) {
48
                return ($val || is_bool($val));
49
            }
50
        );
51
52
        $this->checkRequiredFields($out);
53
54
        return $out;
55
    }
56
57
    /**
58
     * Precision threshold.
59
     *
60
     * @param int $precision Precision Threshold.
61
     */
62
    public function setPrecisionThreshold($precision)
63
    {
64
        $this->precisionThreshold = $precision;
65
    }
66
67
    /**
68
     * @return int
69
     */
70
    public function getPrecisionThreshold()
71
    {
72
        return $this->precisionThreshold;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78
    public function isRehash()
79
    {
80
        return $this->rehash;
81
    }
82
83
    /**
84
     * @param bool $rehash
85
     */
86
    public function setRehash($rehash)
87
    {
88
        $this->rehash = $rehash;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getType()
95
    {
96
        return 'cardinality';
97
    }
98
99
    /**
100
     * Checks if required fields are set.
101
     *
102
     * @param array $fields
103
     *
104
     * @throws \LogicException
105
     */
106
    private function checkRequiredFields($fields)
107
    {
108
        if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) {
109
            throw new \LogicException('Cardinality aggregation must have field or script set.');
110
        }
111
    }
112
}
113