Completed
Push — master ( 62f765...d71e72 )
by Simonas
15:54
created

TopHitsAggregation::addSort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 Simonas Šerlinskas
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
     * Set from.
70
     *
71
     * @param int $from
72
     */
73
    public function setFrom($from)
74
    {
75
        $this->from = $from;
76
    }
77
78
    /**
79
     * @return BuilderInterface[]
80
     */
81
    public function getSorts()
82
    {
83
        return $this->sorts;
84
    }
85
86
    /**
87
     * @param BuilderInterface[] $sorts
88
     */
89
    public function setSorts(array $sorts)
90
    {
91
        $this->sorts = $sorts;
92
    }
93
94
    /**
95
     * Add sort.
96
     *
97
     * @param BuilderInterface $sort
98
     */
99
    public function addSort($sort)
100
    {
101
        $this->sorts[] = $sort;
102
    }
103
104
    /**
105
     * Set size.
106
     *
107
     * @param int $size
108
     */
109
    public function setSize($size)
110
    {
111
        $this->size = $size;
112
    }
113
114
    /**
115
     * Return size.
116
     *
117
     * @return int
118
     */
119
    public function getSize()
120
    {
121
        return $this->size;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getType()
128
    {
129
        return 'top_hits';
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getArray()
136
    {
137
        $sortsOutput = [];
138
        $addedSorts = array_filter($this->getSorts());
139
        if ($addedSorts) {
0 ignored issues
show
Bug Best Practice introduced by Simonas Šerlinskas
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...
140
            foreach ($addedSorts as $sort) {
141
                $sortsOutput[] = $sort->toArray();
142
            }
143
        } else {
144
            $sortsOutput = null;
145
        }
146
147
        $output = array_filter(
148
            [
149
                'sort' => $sortsOutput,
150
                'size' => $this->getSize(),
151
                'from' => $this->getFrom(),
152
            ],
153
            function ($val) {
154
                return (($val || is_array($val) || ($val || is_numeric($val))));
155
            }
156
        );
157
158
        return empty($output) ? new \stdClass() : $output;
159
    }
160
161
    /**
162
     * @deprecated sorts now is a container, use `getSorts()`instead.
163
     * Return sort.
164
     *
165
     * @return BuilderInterface
166
     */
167
    public function getSort()
168
    {
169
        if (isset($this->sorts[0])) {
170
            return $this->sorts[0];
171
        }
172
173
        return null;
174
    }
175
176
    /**
177
     * @deprecated sorts now is a container, use `addSort()`instead.
178
     *
179
     * Set sort.
180
     *
181
     * @param BuilderInterface $sort
182
     */
183
    public function setSort(BuilderInterface $sort)
184
    {
185
        $this->sort = $sort;
0 ignored issues
show
Bug introduced by Simonas Šerlinskas
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...
186
    }
187
}
188