Completed
Push — master ( d54b75...0e24ba )
by Adam
03:00
created

Search::fuzzy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
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
     * @param Index $index
24
     * @param Query $query
25
     */
26 3
    public function __construct(Index $index, Query $query)
27
    {
28 3
        $this->index = $index;
29 3
        $this->query = $query;
30 3
    }
31
32
    /**
33
     * @param  $name
34
     * @param  $arguments
35
     * @return $this
36
     * @throws \BadMethodCallException
37
     */
38
    public function __call($name, $arguments)
39
    {
40
        if (method_exists($this, $name)) {
41
            return $this->$name($arguments);
42
        }
43
44
        if (method_exists($this->query, $name)) {
45
            $this->query->$name($arguments);
46
            return $this;
47
        }
48
49
        throw new \BadMethodCallException;
50
    }
51
52
    /**
53
     * @param $limit
54
     * @return $this
55
     */
56 1
    public function limit($limit)
57
    {
58 1
        $this->limit = $limit;
59 1
        return $this;
60
    }
61
62
    /**
63
     * @param bool $path
64
     * @return $this
65
     */
66 1
    public function path($path = false)
67
    {
68 1
        $this->path = $path;
69 1
        return $this;
70
    }
71
72
    /**
73
     * @param $string
74
     */
75 1
    public function raw($string)
76
    {
77 1
        $this->query->add(QueryParser::parse($string));
78 1
        return $this;
79
    }
80
81
    /**
82
     * @param $string
83
     * @param bool $field
84
     */
85
    public function phrase($string, $field = false, $offsets = null)
86
    {
87
        $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...
88
        return $this;
89
90
    }
91
92
    /**
93
     * @param $string
94
     * @param bool $field
95
     */
96
    public function fuzzy($string, $field = false)
97
    {
98
        $this->query->add(new Fuzzy($this->term($field, $string)));
99
        return $this;
100
    }
101
102
    /**
103
     * @param $string
104
     * @param bool $field
105
     * @return Term
106
     */
107
    protected function term($string, $field = false)
108
    {
109
        return new Term(strtoupper($string), $field);
110
    }
111
112
    /**
113
     * @param $string
114
     * @param bool $field
115
     * @param array $options
116
     */
117
    public function wildcard($string, $field = false, $options = [ ])
118
    {
119
        $this->query->add((new Wildcard($field, $string, $options))->get());
120
    }
121
122
    /**
123
     * @param $string
124
     * @param string $field
125
     * @todo  Work out why the search only works if the string is uppercase...
126
     * @return $this|bool
127
     */
128
    public function where($string, $field = false)
129
    {
130
        is_array($field)
131
            ? $this->multiTerm($this->mapWhereArray($string, $field))
132
            : $this->query->add($this->singleTerm($string, $field));
0 ignored issues
show
Bug introduced by
It seems like $field defined by parameter $field on line 128 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...
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param array $terms
139
     * @return $this
140
     */
141
    public function multiTerm(array $terms)
142
    {
143
        $multiTerm = new MultiTerm;
144
        foreach ($terms as $field => $value) {
145
            $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...
146
        }
147
148
        $this->query->add($multiTerm);
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param $string
155
     * @param array $array
156
     * @return mixed
157
     * @todo abstract this out
158
     */
159
    private function mapWhereArray($string, array $array)
160
    {
161
        return array_map(function() use ($string) {
162
            return $string;
163
        }, array_flip($array));
164
    }
165
166
    /**
167
     * @param $string
168
     * @param $field
169
     * @return QueryTerm
170
     */
171
    public function singleTerm($string, $field = false)
172
    {
173
        return new QueryTerm($this->term($string, $field));
174
    }
175
176
    /**
177
     * @return mixed
178
     */
179
    public function hits()
180
    {
181
        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...
182
    }
183
184
    /**
185
     * @param array $array
186
     * @return mixed
187
     * @todo abstract this out
188
     */
189
    private function mapIds(array $array)
190
    {
191
        return array_map(function(QueryHit $hit) {
192
            return $hit->id;
193
        }, $array);
194
    }
195
}
196