TopHitsAggregation   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getFrom() 0 4 1
A setFrom() 0 6 1
A getSorts() 0 4 1
A setSorts() 0 6 1
A addSort() 0 4 1
A setSize() 0 6 1
A getSize() 0 4 1
A getType() 0 4 1
B getArray() 0 25 7
A getSort() 0 8 2
A setSort() 0 6 1
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\ElasticsearchDSL\Aggregation\Metric;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait;
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
18
/**
19
 * Top hits aggregation.
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
22
 */
23
class TopHitsAggregation extends AbstractAggregation
24
{
25
    use MetricTrait;
26
27
    /**
28
     * @var int Number of top matching hits to return per bucket.
29
     */
30
    private $size;
31
32
    /**
33
     * @var int The offset from the first result you want to fetch.
34
     */
35
    private $from;
36
37
    /**
38
     * @var BuilderInterface[] How the top matching hits should be sorted.
39
     */
40
    private $sorts = [];
41
42
    /**
43
     * Constructor for top hits.
44
     *
45
     * @param string                $name Aggregation name.
46
     * @param null|int              $size Number of top matching hits to return per bucket.
47
     * @param null|int              $from The offset from the first result you want to fetch.
48
     * @param null|BuilderInterface $sort How the top matching hits should be sorted.
49
     */
50
    public function __construct($name, $size = null, $from = null, $sort = null)
51
    {
52
        parent::__construct($name);
53
        $this->setFrom($from);
54
        $this->setSize($size);
55
        $this->addSort($sort);
0 ignored issues
show
Bug introduced by
It seems like $sort defined by parameter $sort on line 50 can be null; however, ONGR\ElasticsearchDSL\Ag...sAggregation::addSort() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
56
    }
57
58
    /**
59
     * Return from.
60
     *
61
     * @return int
62
     */
63
    public function getFrom()
64
    {
65
        return $this->from;
66
    }
67
68
    /**
69
     * @param int $from
70
     *
71
     * @return $this
72
     */
73
    public function setFrom($from)
74
    {
75
        $this->from = $from;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return BuilderInterface[]
82
     */
83
    public function getSorts()
84
    {
85
        return $this->sorts;
86
    }
87
88
    /**
89
     * @param BuilderInterface[] $sorts
90
     *
91
     * @return $this
92
     */
93
    public function setSorts(array $sorts)
94
    {
95
        $this->sorts = $sorts;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Add sort.
102
     *
103
     * @param BuilderInterface $sort
104
     */
105
    public function addSort($sort)
106
    {
107
        $this->sorts[] = $sort;
108
    }
109
110
    /**
111
     * @param int $size
112
     *
113
     * @return $this
114
     */
115
    public function setSize($size)
116
    {
117
        $this->size = $size;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Return size.
124
     *
125
     * @return int
126
     */
127
    public function getSize()
128
    {
129
        return $this->size;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getType()
136
    {
137
        return 'top_hits';
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getArray()
144
    {
145
        $sortsOutput = [];
146
        $addedSorts = array_filter($this->getSorts());
147
        if ($addedSorts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $addedSorts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
148
            foreach ($addedSorts as $sort) {
149
                $sortsOutput[] = $sort->toArray();
150
            }
151
        } else {
152
            $sortsOutput = null;
153
        }
154
155
        $output = array_filter(
156
            [
157
                'sort' => $sortsOutput,
158
                'size' => $this->getSize(),
159
                'from' => $this->getFrom(),
160
            ],
161
            function ($val) {
162
                return (($val || is_array($val) || ($val || is_numeric($val))));
163
            }
164
        );
165
166
        return empty($output) ? new \stdClass() : $output;
167
    }
168
169
    /**
170
     * @deprecated sorts now is a container, use `getSorts()`instead.
171
     * Return sort.
172
     *
173
     * @return BuilderInterface
174
     */
175
    public function getSort()
176
    {
177
        if (isset($this->sorts[0])) {
178
            return $this->sorts[0];
179
        }
180
181
        return null;
182
    }
183
184
    /**
185
     * @deprecated sorts now is a container, use `addSort()`instead.
186
     *
187
     * @param BuilderInterface $sort
188
     *
189
     * @return $this
190
     */
191
    public function setSort(BuilderInterface $sort)
192
    {
193
        $this->sort = $sort;
0 ignored issues
show
Bug introduced by
The property sort does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
194
195
        return $this;
196
    }
197
}
198