Completed
Push — legacy ( c3f933 )
by Simonas
95:41
created

TopHitsAggregation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
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\Sort\Sorts;
16
17
/**
18
 * Top hits aggregation.
19
 */
20
class TopHitsAggregation extends AbstractAggregation
21
{
22
    use MetricTrait;
23
24
    /**
25
     * @var int Number of top matching hits to return per bucket.
26
     */
27
    private $size;
28
29
    /**
30
     * @var int The offset from the first result you want to fetch.
31
     */
32
    private $from;
33
34
    /**
35
     * @var Sorts How the top matching hits should be sorted.
36
     */
37
    private $sort;
38
39
    /**
40
     * Constructor for top hits.
41
     *
42
     * @param string     $name Aggregation name.
43
     * @param null|int   $size Number of top matching hits to return per bucket.
44
     * @param null|int   $from The offset from the first result you want to fetch.
45
     * @param null|Sorts $sort How the top matching hits should be sorted.
46
     */
47
    public function __construct($name, $size = null, $from = null, $sort = null)
48
    {
49
        parent::__construct($name);
50
        $this->setFrom($from);
51
        $this->setSize($size);
52
        $this->setSort($sort);
0 ignored issues
show
Bug introduced by
It seems like $sort defined by parameter $sort on line 47 can be null; however, ONGR\ElasticsearchBundle...sAggregation::setSort() 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...
53
    }
54
55
    /**
56
     * Return from.
57
     *
58
     * @return int
59
     */
60
    public function getFrom()
61
    {
62
        return $this->from;
63
    }
64
65
    /**
66
     * Set from.
67
     *
68
     * @param int $from
69
     */
70
    public function setFrom($from)
71
    {
72
        $this->from = $from;
73
    }
74
75
    /**
76
     * Return sort.
77
     *
78
     * @return Sorts
79
     */
80
    public function getSort()
81
    {
82
        return $this->sort;
83
    }
84
85
    /**
86
     * Set sort.
87
     *
88
     * @param Sorts $sort
89
     */
90
    public function setSort($sort)
91
    {
92
        $this->sort = $sort;
93
    }
94
95
    /**
96
     * Set size.
97
     *
98
     * @param int $size
99
     */
100
    public function setSize($size)
101
    {
102
        $this->size = $size;
103
    }
104
105
    /**
106
     * Return size.
107
     *
108
     * @return int
109
     */
110
    public function getSize()
111
    {
112
        return $this->size;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getType()
119
    {
120
        return 'top_hits';
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getArray()
127
    {
128
        $data = new \stdClass();
129
130
        $filteredData = $this->getFilteredData();
131
        foreach ($filteredData as $key => $value) {
132
            $data->{$key} = $value;
133
        }
134
135
        return $data;
136
    }
137
138
    /**
139
     * Filters the data.
140
     *
141
     * @return array
142
     */
143
    private function getFilteredData()
144
    {
145
        $fd = array_filter(
146
            [
147
                'sort' => $this->getSort() ? $this->getSort()->toArray() : [],
148
                'size' => $this->getSize(),
149
                'from' => $this->getFrom(),
150
            ],
151
            function ($val) {
152
                return (($val || is_array($val) || ($val || is_numeric($val))));
153
            }
154
        );
155
156
        return $fd;
157
    }
158
}
159