Completed
Push — master ( 96ab8a...466607 )
by Nicolas
03:10
created

BoolQuery   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 6.5 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 3
cbo 2
dl 8
loc 123
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addShould() 0 4 1
A addMust() 0 4 1
A addMustNot() 0 4 1
A addFilter() 0 4 1
A _addQuery() 8 8 3
A setBoost() 0 4 1
A setMinimumNumberShouldMatch() 0 6 1
A setMinimumShouldMatch() 0 4 1
A toArray() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Elastica\Query;
3
4
use Elastica\Exception\InvalidException;
5
6
/**
7
 * Bool query.
8
 *
9
 * @author Nicolas Ruflin <[email protected]>
10
 *
11
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
12
 */
13
class BoolQuery extends AbstractQuery
14
{
15
    /**
16
     * Add should part to query.
17
     *
18
     * @param \Elastica\Query\AbstractQuery|array $args Should query
19
     *
20
     * @return $this
21
     */
22
    public function addShould($args)
23
    {
24
        return $this->_addQuery('should', $args);
25
    }
26
27
    /**
28
     * Add must part to query.
29
     *
30
     * @param \Elastica\Query\AbstractQuery|array $args Must query
31
     *
32
     * @return $this
33
     */
34
    public function addMust($args)
35
    {
36
        return $this->_addQuery('must', $args);
37
    }
38
39
    /**
40
     * Add must not part to query.
41
     *
42
     * @param \Elastica\Query\AbstractQuery|array $args Must not query
43
     *
44
     * @return $this
45
     */
46
    public function addMustNot($args)
47
    {
48
        return $this->_addQuery('must_not', $args);
49
    }
50
51
    /**
52
     * Sets the filter.
53
     *
54
     * @param \Elastica\Query\AbstractQuery $filter Filter object
55
     *
56
     * @return $this
57
     */
58
    public function addFilter(AbstractQuery $filter)
59
    {
60
        return $this->addParam('filter', $filter);
61
    }
62
63
    /**
64
     * Adds a query to the current object.
65
     *
66
     * @param string                              $type Query type
67
     * @param \Elastica\Query\AbstractQuery|array $args Query
68
     *
69
     * @throws \Elastica\Exception\InvalidException If not valid query
70
     *
71
     * @return $this
72
     */
73 View Code Duplication
    protected function _addQuery($type, $args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        if (!is_array($args) && !($args instanceof AbstractQuery)) {
76
            throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractQuery');
77
        }
78
79
        return $this->addParam($type, $args);
80
    }
81
82
    /**
83
     * Sets boost value of this query.
84
     *
85
     * @param float $boost Boost value
86
     *
87
     * @return $this
88
     */
89
    public function setBoost($boost)
90
    {
91
        return $this->setParam('boost', $boost);
92
    }
93
94
    /**
95
     * Set the minimum number of of should match.
96
     *
97
     * @param int $minimumNumberShouldMatch Should match minimum
98
     *
99
     * @return $this
100
     *
101
     * @deprecated Replaced by setMinimumShouldMatch
102
     */
103
    public function setMinimumNumberShouldMatch($minimumNumberShouldMatch)
104
    {
105
        trigger_error('Deprecated: Elastica\Query::setMinimumNumberShouldMatch() is deprecated and will be removed in further Elastica releases. Use Elastica\Query::setMinimumShouldMatch() instead.', E_USER_DEPRECATED);
106
107
        return $this->setParam('minimum_number_should_match', $minimumNumberShouldMatch);
108
    }
109
110
    /**
111
     * Sets the minimum number of should clauses to match.
112
     *
113
     * @param int|string $minimum Minimum value
114
     *
115
     * @return $this
116
     */
117
    public function setMinimumShouldMatch($minimum)
118
    {
119
        return $this->setParam('minimum_should_match', $minimum);
120
    }
121
122
    /**
123
     * Converts array to an object in case no queries are added.
124
     *
125
     * @return array
126
     */
127
    public function toArray()
128
    {
129
        if (empty($this->_params)) {
130
            $this->_params = new \stdClass();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \stdClass() of type object<stdClass> is incompatible with the declared type array of property $_params.

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...
131
        }
132
133
        return parent::toArray();
134
    }
135
}
136