Completed
Pull Request — master (#154)
by
unknown
03:42
created

NestedInnerHit   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 11
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 105
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPath() 0 4 1
A setPath() 0 4 1
A getQuery() 0 4 1
A setQuery() 0 4 1
A getType() 0 4 1
A toArray() 0 12 2
A getPathType() 0 14 3
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\BuilderInterface;
15
use ONGR\ElasticsearchDSL\NameAwareTrait;
16
use ONGR\ElasticsearchDSL\ParametersTrait;
17
use ONGR\ElasticsearchDSL\Search;
18
19
/**
20
 * Represents Elasticsearch top level nested inner hits.
21
 *
22
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html
23
 */
24
class NestedInnerHit implements BuilderInterface
25
{
26
    use ParametersTrait;
27
    use NameAwareTrait;
28
29
    /**
30
     * @var string
31
     */
32
    private $path;
33
34
    /**
35
     * @var BuilderInterface
36
     */
37
    private $query;
38
39
    /**
40
     * Inner hits container init.
41
     *
42
     * @param string $name
43
     * @param string $path
44
     * @param Search $query
45
     */
46
    public function __construct($name, $path, Search $query = null)
47
    {
48
        $this->setName($name);
49
        $this->setPath($path);
50
        $this->setQuery($query);
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getPath()
57
    {
58
        return $this->path;
59
    }
60
61
    /**
62
     * @param string $path
63
     */
64
    public function setPath($path)
65
    {
66
        $this->path = $path;
67
    }
68
69
    /**
70
     * @return BuilderInterface
71
     */
72
    public function getQuery()
73
    {
74
        return $this->query;
75
    }
76
77
    /**
78
     * @param Search $query
79
     */
80
    public function setQuery(Search $query = null)
81
    {
82
        $this->query = $query;
0 ignored issues
show
Documentation Bug introduced by
It seems like $query can also be of type object<ONGR\ElasticsearchDSL\Search>. However, the property $query is declared as type object<ONGR\ElasticsearchDSL\BuilderInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getType()
89
    {
90
        return 'nested';
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function toArray()
97
    {
98
        $out = $this->getQuery() ? $this->getQuery()->toArray() : new \stdClass();
99
100
        $out = [
101
            $this->getPathType() => [
102
                $this->getPath() => $out ,
103
            ],
104
        ];
105
106
        return $out;
107
    }
108
109
    /**
110
     * Returns 'path' for nested and 'type' for parent inner hits
111
     *
112
     * @return null|string
113
     */
114
    private function getPathType()
115
    {
116
        switch ($this->getType()) {
117
            case 'nested':
118
                $type = 'path';
119
                break;
120
            case 'parent':
121
                $type = 'type';
122
                break;
123
            default:
124
                $type = null;
125
        }
126
        return $type;
127
    }
128
}
129