AbstractAggregation::toArray()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 6
nop 0
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;
13
14
use ONGR\ElasticsearchDSL\BuilderBag;
15
use ONGR\ElasticsearchDSL\NameAwareTrait;
16
use ONGR\ElasticsearchDSL\NamedBuilderInterface;
17
use ONGR\ElasticsearchDSL\ParametersTrait;
18
19
/**
20
 * AbstractAggregation class.
21
 */
22
abstract class AbstractAggregation implements NamedBuilderInterface
23
{
24
    use ParametersTrait;
25
    use NameAwareTrait;
26
27
    /**
28
     * @var string
29
     */
30
    private $field;
31
32
    /**
33
     * @var BuilderBag
34
     */
35
    private $aggregations;
36
37
    /**
38
     * Abstract supportsNesting method.
39
     *
40
     * @return bool
41
     */
42
    abstract protected function supportsNesting();
43
44
    /**
45
     * @return array|\stdClass
46
     */
47
    abstract protected function getArray();
48
49
    /**
50
     * Inner aggregations container init.
51
     *
52
     * @param string $name
53
     */
54
    public function __construct($name)
55
    {
56
        $this->setName($name);
57
    }
58
59
    /**
60
     * @param string $field
61
     *
62
     * @return $this
63
     */
64
    public function setField($field)
65
    {
66
        $this->field = $field;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getField()
75
    {
76
        return $this->field;
77
    }
78
79
    /**
80
     * Adds a sub-aggregation.
81
     *
82
     * @param AbstractAggregation $abstractAggregation
83
     *
84
     * @return $this
85
     */
86
    public function addAggregation(AbstractAggregation $abstractAggregation)
87
    {
88
        if (!$this->aggregations) {
89
            $this->aggregations = $this->createBuilderBag();
90
        }
91
92
        $this->aggregations->add($abstractAggregation);
93
        
94
        return $this;
95
    }
96
97
    /**
98
     * Returns all sub aggregations.
99
     *
100
     * @return BuilderBag[]|NamedBuilderInterface[]
101
     */
102
    public function getAggregations()
103
    {
104
        if ($this->aggregations) {
105
            return $this->aggregations->all();
106
        } else {
107
            return [];
108
        }
109
    }
110
111
    /**
112
     * Returns sub aggregation.
113
     * @param string $name Aggregation name to return.
114
     *
115
     * @return AbstractAggregation|NamedBuilderInterface|null
116
     */
117
    public function getAggregation($name)
118
    {
119
        if ($this->aggregations && $this->aggregations->has($name)) {
120
            return $this->aggregations->get($name);
121
        } else {
122
            return null;
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function toArray()
130
    {
131
        $array = $this->getArray();
132
        $result = [
133
            $this->getType() => is_array($array) ? $this->processArray($array) : $array,
134
        ];
135
136
        if ($this->supportsNesting()) {
137
            $nestedResult = $this->collectNestedAggregations();
138
139
            if (!empty($nestedResult)) {
140
                $result['aggregations'] = $nestedResult;
141
            }
142
        }
143
144
        return $result;
145
    }
146
147
    /**
148
     * Process all nested aggregations.
149
     *
150
     * @return array
151
     */
152
    protected function collectNestedAggregations()
153
    {
154
        $result = [];
155
        /** @var AbstractAggregation $aggregation */
156
        foreach ($this->getAggregations() as $aggregation) {
157
            $result[$aggregation->getName()] = $aggregation->toArray();
0 ignored issues
show
Bug introduced by
The method getName does only exist in ONGR\ElasticsearchDSL\NamedBuilderInterface, but not in ONGR\ElasticsearchDSL\BuilderBag.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
        }
159
160
        return $result;
161
    }
162
163
    /**
164
     * Creates BuilderBag new instance.
165
     *
166
     * @return BuilderBag
167
     */
168
    private function createBuilderBag()
169
    {
170
        return new BuilderBag();
171
    }
172
}
173