Completed
Push — master ( 0eb270...4f660e )
by
unknown
04:04 queued 01:31
created

Terms::setTermsLookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Elastica\Query;
4
5
use Elastica\Exception\InvalidException;
6
7
/**
8
 * Terms query.
9
 *
10
 * @author Nicolas Ruflin <[email protected]>
11
 * @author Roberto Nygaard <[email protected]>
12
 *
13
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
14
 */
15
class Terms extends AbstractQuery
16
{
17
    /**
18
     * @var string
19
     */
20
    private $field;
21
22
    /**
23
     * @var array<float|int|string>
24
     */
25
    private $terms;
26
27
    /**
28
     * @var string[]|null
29
     */
30
    private $lookup;
31
32
    /**
33
     * @param array<bool|float|int|string> $terms Terms list, leave empty if building a terms-lookup query
34
     */
35
    public function __construct(string $field, array $terms = [])
36
    {
37
        if (empty($field)) {
38
            throw new InvalidException('Terms field name has to be set');
39
        }
40
41
        $this->field = $field;
42
        $this->terms = $terms;
0 ignored issues
show
Documentation Bug introduced by
It seems like $terms of type array<integer,boolean|double|integer|string> is incompatible with the declared type array<integer,double|integer|string> of property $terms.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
    }
44
45
    /**
46
     * Sets terms for the query.
47
     *
48
     * @param array<bool|float|int|string> $terms
49
     */
50
    public function setTerms(array $terms): self
51
    {
52
        $this->terms = $terms;
0 ignored issues
show
Documentation Bug introduced by
It seems like $terms of type array<integer,boolean|double|integer|string> is incompatible with the declared type array<integer,double|integer|string> of property $terms.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
54
        return $this;
55
    }
56
57
    /**
58
     * Adds a single term to the list.
59
     *
60
     * @param bool|float|int|string $term
61
     */
62
    public function addTerm($term): self
63
    {
64
        if (!\is_scalar($term)) {
65
            throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be a scalar, %s given.', __METHOD__, \is_object($term) ? \get_class($term) : \gettype($term)));
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with \sprintf('Argument 1 pas...erm) : \gettype($term)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
66
        }
67
68
        $this->terms[] = $term;
69
70
        return $this;
71
    }
72
73
    public function setTermsLookup(string $index, string $id, string $path): self
74
    {
75
        $this->lookup = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('index' => $index,...> $id, 'path' => $path) of type array<string,string,{"in...ring","path":"string"}> is incompatible with the declared type array<integer,string>|null of property $lookup.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
            'index' => $index,
77
            'id' => $id,
78
            'path' => $path,
79
        ];
80
81
        return $this;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function toArray(): array
88
    {
89
        if (null !== $this->lookup && \count($this->terms)) {
90
            throw new InvalidException('Unable to build Terms query: only one of terms or lookup properties should be set');
91
        }
92
93
        if (null !== $this->lookup) {
94
            $this->setParam($this->field, $this->lookup);
95
        } else {
96
            $this->setParam($this->field, $this->terms);
97
        }
98
99
        return parent::toArray();
100
    }
101
}
102