Completed
Push — master ( a1769b...8b06e6 )
by Adam
03:08
created

Search::term()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Lucene;
4
5
use BestServedCold\LaravelZendSearch\Search\Wildcard;
6
use ZendSearch\Lucene\Index\Term;
7
use ZendSearch\Lucene\Search\Query\Fuzzy;
8
use ZendSearch\Lucene\Search\Query\MultiTerm;
9
use ZendSearch\Lucene\Search\Query\Phrase;
10
use ZendSearch\Lucene\Search\Query\Term as QueryTerm;
11
use ZendSearch\Lucene\Search\QueryHit;
12
use ZendSearch\Lucene\Search\QueryParser;
13
14
class Search
15
{
16
    protected $index;
17
    protected $query;
18
    private $path;
19
    private $limit = 25;
20
21
    /**
22
     * Search constructor.
23
     *
24
     * @param Index $index
25
     * @param Query $query
26
     */
27 3
    public function __construct(Index $index, Query $query)
28
    {
29 3
        $this->index = $index;
30 3
        $this->query = $query;
31 3
    }
32
33
    /**
34
     * @param  $name
35
     * @param  $arguments
36
     * @return $this
37
     * @throws \BadMethodCallException
38
     */
39
    public function __call($name, $arguments)
40
    {
41
        if (method_exists($this, $name)) {
42
            return $this->$name($arguments);
43
        }
44
45
        if (method_exists($this->query, $name)) {
46
            $this->query->$name($arguments);
47
            return $this;
48
        }
49
50
        throw new \BadMethodCallException;
51
    }
52
53
    /**
54
     * @param $limit
55
     * @return $this
56
     */
57 1
    public function limit($limit)
58
    {
59 1
        $this->limit = $limit;
60 1
        return $this;
61
    }
62
63
    /**
64
     * @param bool $path
65
     * @return $this
66
     */
67 1
    public function path($path = false)
68
    {
69 1
        $this->path = $path;
70 1
        return $this;
71
    }
72
73
    /**
74
     * @param $string
75
     * @return $this
76
     */
77 1
    public function raw($string)
78
    {
79 1
        $this->query->add(QueryParser::parse($string));
80 1
        return $this;
81
    }
82
83
    /**
84
     * @param $string
85
     * @param bool   $field
86
     * @param null   $offsets
87
     * @return $this
88
     * @return $this
89
     */
90
    public function phrase($string, $field = false, $offsets = null)
91
    {
92
        $this->query->add(new Phrase(explode(' ', $string), $offsets, $field));
0 ignored issues
show
Documentation introduced by
$field is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
        return $this;
94
95
    }
96
97
    /**
98
     * @param $string
99
     * @param bool   $field
100
     * @return $this
101
     */
102
    public function fuzzy($string, $field = false)
103
    {
104
        $this->query->add(new Fuzzy($this->term($field, $string)));
105
        return $this;
106
    }
107
108
    /**
109
     * @param $string
110
     * @param bool   $field
111
     * @return Term
112
     */
113
    protected function term($string, $field = false)
114
    {
115
        return new Term(strtoupper($string), $field);
116
    }
117
118
    /**
119
     * @param $string
120
     * @param bool   $field
121
     * @param array  $options
122
     */
123
    public function wildcard($string, $field = false, $options = [ ])
124
    {
125
        $this->query->add((new Wildcard($field, $string, $options))->get());
126
    }
127
128
    /**
129
     * @param $string
130
     * @param bool|string $field
131
     * @return $this|bool
132
     * @todo  Work out why the search only works if the string is uppercase...
133
     */
134
    public function where($string, $field = false)
135
    {
136
        is_array($field)
137
            ? $this->multiTerm($this->mapWhereArray($string, $field))
138
            : $this->query->add($this->singleTerm($string, $field));
0 ignored issues
show
Bug introduced by
It seems like $field defined by parameter $field on line 134 can also be of type string; however, BestServedCold\LaravelZe...ne\Search::singleTerm() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param array $terms
145
     * @return $this
146
     */
147
    public function multiTerm(array $terms)
148
    {
149
        $multiTerm = new MultiTerm;
150
        foreach ($terms as $field => $value) {
151
            $multiTerm->addTerm($this->term($value, $field), $this->query->getSign());
0 ignored issues
show
Documentation introduced by
$field is of type integer|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
152
        }
153
154
        $this->query->add($multiTerm);
155
156
        return $this;
157
    }
158
159
    /**
160
     * @param $string
161
     * @param array  $array
162
     * @return mixed
163
     * @todo abstract this out
164
     */
165
    private function mapWhereArray($string, array $array)
166
    {
167
        return array_map(
168
            function () use ($string) {
169
                return $string;
170
            }, array_flip($array)
171
        );
172
    }
173
174
    /**
175
     * @param $string
176
     * @param $field
177
     * @return QueryTerm
178
     */
179
    public function singleTerm($string, $field = false)
180
    {
181
        return new QueryTerm($this->term($string, $field));
182
    }
183
184
    /**
185
     * @return mixed
186
     */
187
    public function hits()
188
    {
189
        return $this->mapIds($this->index->limit($this->limit)->open($this->path)->find($this->query->getBoolean()));
0 ignored issues
show
Bug introduced by
It seems like $this->index->limit($thi...s->query->getBoolean()) targeting ZendSearch\Lucene\SearchIndexInterface::find() can also be of type object<ZendSearch\Lucene\Search\QueryHit>; however, BestServedCold\LaravelZe...Lucene\Search::mapIds() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
190
    }
191
192
    /**
193
     * @param array $array
194
     * @return mixed
195
     * @todo abstract this out
196
     */
197
    private function mapIds(array $array)
198
    {
199
        return array_map(
200
            function (QueryHit $hit) {
201
                return $hit->id;
202
            }, $array
203
        );
204
    }
205
}
206