Completed
Push — master ( 61efdf...7a2c62 )
by Simonas
01:38
created

src/Aggregation/AbstractAggregation.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    public function setField($field)
63
    {
64
        $this->field = $field;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getField()
71
    {
72
        return $this->field;
73
    }
74
75
    /**
76
     * Adds a sub-aggregation.
77
     *
78
     * @param AbstractAggregation $abstractAggregation
79
     *
80
     * @return $this
81
     */
82
    public function addAggregation(AbstractAggregation $abstractAggregation)
83
    {
84
        if (!$this->aggregations) {
85
            $this->aggregations = $this->createBuilderBag();
86
        }
87
88
        $this->aggregations->add($abstractAggregation);
89
        
90
        return $this;
91
    }
92
93
    /**
94
     * Returns all sub aggregations.
95
     *
96
     * @return BuilderBag[]|NamedBuilderInterface[]
97
     */
98
    public function getAggregations()
99
    {
100
        if ($this->aggregations) {
101
            return $this->aggregations->all();
102
        } else {
103
            return [];
104
        }
105
    }
106
107
    /**
108
     * Returns sub aggregation.
109
     * @param string $name Aggregation name to return.
110
     *
111
     * @return AbstractAggregation|NamedBuilderInterface|null
112
     */
113
    public function getAggregation($name)
114
    {
115
        if ($this->aggregations && $this->aggregations->has($name)) {
116
            return $this->aggregations->get($name);
117
        } else {
118
            return null;
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function toArray()
126
    {
127
        $array = $this->getArray();
128
        $result = [
129
            $this->getType() => is_array($array) ? $this->processArray($array) : $array,
130
        ];
131
132
        if ($this->supportsNesting()) {
133
            $nestedResult = $this->collectNestedAggregations();
134
135
            if (!empty($nestedResult)) {
136
                $result['aggregations'] = $nestedResult;
137
            }
138
        }
139
140
        return $result;
141
    }
142
143
    /**
144
     * Process all nested aggregations.
145
     *
146
     * @return array
147
     */
148
    protected function collectNestedAggregations()
149
    {
150
        $result = [];
151
        /** @var AbstractAggregation $aggregation */
152
        foreach ($this->getAggregations() as $aggregation) {
153
            $result[$aggregation->getName()] = $aggregation->toArray();
0 ignored issues
show
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...
154
        }
155
156
        return $result;
157
    }
158
159
    /**
160
     * Creates BuilderBag new instance.
161
     *
162
     * @return BuilderBag
163
     */
164
    private function createBuilderBag()
165
    {
166
        return new BuilderBag();
167
    }
168
}
169