Completed
Pull Request — master (#124)
by
unknown
02:45
created

AbstractInnerHit::setQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
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\InnerHit;
13
14
use ONGR\ElasticsearchDSL\BuilderBag;
15
use ONGR\ElasticsearchDSL\BuilderInterface;
16
use ONGR\ElasticsearchDSL\NameAwareTrait;
17
use ONGR\ElasticsearchDSL\ParametersTrait;
18
19
/**
20
 * AbstractAggregation class.
21
 */
22
abstract class AbstractInnerHit implements BuilderInterface
23
{
24
    use ParametersTrait;
25
    use NameAwareTrait;
26
27
    /**
28
     * @var string
29
     */
30
    private $path;
31
32
    /**
33
     * @var BuilderInterface
34
     */
35
    private $query;
36
37
    /**
38
     * @var BuilderBag
39
     */
40
    private $innerHits;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    abstract public function toArray();
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    abstract public function getType();
51
52
    /**
53
     * Inner hits container init.
54
     *
55
     * @param string           $name
56
     * @param string           $path
57
     * @param BuilderInterface $query
58
     */
59
    public function __construct($name, $path, BuilderInterface $query)
60
    {
61
        $this->setName($name);
62
        $this->setPath($path);
63
        $this->setQuery($query);
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getPath()
70
    {
71
        return $this->path;
72
    }
73
74
    /**
75
     * @param string $path
76
     */
77
    public function setPath($path)
78
    {
79
        $this->path = $path;
80
    }
81
82
    /**
83
     * @return BuilderInterface
84
     */
85
    public function getQuery()
86
    {
87
        return $this->query;
88
    }
89
90
    /**
91
     * @param BuilderInterface $query
92
     */
93
    public function setQuery(BuilderInterface $query)
94
    {
95
        $this->query = $query;
96
    }
97
98
    /**
99
     * Adds a sub-innerHit.
100
     *
101
     * @param AbstractInnerHit $abstractInnerHit
102
     */
103
    public function addInnerHit(AbstractInnerHit $abstractInnerHit)
104
    {
105
        if (!$this->innerHits) {
106
            $this->innerHits = new BuilderBag();
107
        }
108
109
        $this->innerHits->add($abstractInnerHit);
110
    }
111
112
    /**
113
     * Returns all sub inner hits.
114
     *
115
     * @return BuilderInterface[]
116
     */
117
    public function getInnerHits()
118
    {
119
        if ($this->innerHits) {
120
            return $this->innerHits->all();
121
        } else {
122
            return [];
123
        }
124
    }
125
126
    /**
127
     * Returns sub inner hit.
128
     * @param string $name inner hit name to return.
129
     *
130
     * @return AbstractInnerHit|null
131
     */
132
    public function getInnerHit($name)
133
    {
134
        if ($this->innerHits && $this->innerHits->has($name)) {
135
            return $this->innerHits->get($name);
136
        } else {
137
            return null;
138
        }
139
    }
140
141
    /**
142
     * Process all nested inner hits.
143
     *
144
     * @return array
145
     */
146
    public function collectNestedInnerHits()
147
    {
148
        $result = [];
149
        /** @var AbstractInnerHit $innerHit */
150
        foreach ($this->getInnerHits() as $innerHit) {
151
            $result[$innerHit->getName()] = $innerHit->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ONGR\ElasticsearchDSL\BuilderInterface as the method getName() does only exist in the following implementations of said interface: ONGR\ElasticsearchDSL\Ag...ion\AbstractAggregation, ONGR\ElasticsearchDSL\Aggregation\AvgAggregation, ONGR\ElasticsearchDSL\Ag...\CardinalityAggregation, ONGR\ElasticsearchDSL\Ag...ion\ChildrenAggregation, ONGR\ElasticsearchDSL\Ag...ateHistogramAggregation, ONGR\ElasticsearchDSL\Ag...on\DateRangeAggregation, ONGR\ElasticsearchDSL\Ag...xtendedStatsAggregation, ONGR\ElasticsearchDSL\Ag...ation\FilterAggregation, ONGR\ElasticsearchDSL\Ag...tion\FiltersAggregation, ONGR\ElasticsearchDSL\Ag...on\GeoBoundsAggregation, ONGR\ElasticsearchDSL\Ag...\GeoDistanceAggregation, ONGR\ElasticsearchDSL\Ag...\GeoHashGridAggregation, ONGR\ElasticsearchDSL\Ag...ation\GlobalAggregation, ONGR\ElasticsearchDSL\Ag...on\HistogramAggregation, ONGR\ElasticsearchDSL\Ag...on\Ipv4RangeAggregation, ONGR\ElasticsearchDSL\Aggregation\MaxAggregation, ONGR\ElasticsearchDSL\Aggregation\MinAggregation, ONGR\ElasticsearchDSL\Ag...tion\MissingAggregation, ONGR\ElasticsearchDSL\Ag...ation\NestedAggregation, ONGR\ElasticsearchDSL\Ag...centileRanksAggregation, ONGR\ElasticsearchDSL\Ag...\PercentilesAggregation, ONGR\ElasticsearchDSL\Aggregation\RangeAggregation, ONGR\ElasticsearchDSL\Ag...everseNestedAggregation, ONGR\ElasticsearchDSL\Ag...ificantTermsAggregation, ONGR\ElasticsearchDSL\Aggregation\StatsAggregation, ONGR\ElasticsearchDSL\Aggregation\SumAggregation, ONGR\ElasticsearchDSL\Aggregation\TermsAggregation, ONGR\ElasticsearchDSL\Ag...tion\TopHitsAggregation, ONGR\ElasticsearchDSL\Ag...n\ValueCountAggregation, ONGR\ElasticsearchDSL\InnerHit\AbstractInnerHit, ONGR\ElasticsearchDSL\Suggest\CompletionSuggest, ONGR\ElasticsearchDSL\Suggest\Suggest, ONGR\ElasticsearchDSL\Suggest\TermSuggest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
152
        }
153
154
        return $result;
155
    }
156
}
157