Completed
Branch master (3a11a5)
by Adam
06:08 queued 02:35
created

Search::where()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Lucene;
4
5
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\Wildcard;
11
use ZendSearch\Lucene\Search\Query\Term as QueryTerm;
12
use ZendSearch\Lucene\Search\QueryHit;
13
use ZendSearch\Lucene\Search\QueryParser;
14
15
/**
16
 * Class Search
17
 * @package BestServedCold\LaravelZendSearch\Lucene
18
 */
19
class Search
20
{
21
    protected $index;
22
    protected $query;
23
    private $path;
24
    private $limit = 25;
25
26
    /**
27
     * Search constructor.
28
     *
29
     * @param Index $index
30
     * @param Query $query
31
     */
32 6
    public function __construct(Index $index, Query $query)
33
    {
34 6
        $this->index = $index;
35 6
        $this->query = $query;
36 6
    }
37
38
    /**
39
     * @param  $name
40
     * @param  $arguments
41
     * @return $this
42
     * @throws \BadMethodCallException
43
     */
44 2
    public function __call($name, $arguments)
45
    {
46 2
        if (method_exists($this->query, $name)) {
47 1
            $this->query->$name($arguments);
48 1
            return $this;
49
        }
50
51 1
        throw new \BadMethodCallException;
52
    }
53
54
    /**
55
     * @param $limit
56
     * @return $this
57
     */
58 1
    public function limit($limit)
59
    {
60 1
        $this->limit = $limit;
61 1
        return $this;
62
    }
63
64
    /**
65
     * @param bool $path
66
     * @return $this
67
     */
68 1
    public function path($path = false)
69
    {
70 1
        $this->path = $path;
71 1
        return $this;
72
    }
73
74
    /**
75
     * @param $string
76
     * @return $this
77
     */
78 1
    public function raw($string)
79
    {
80 1
        $this->query->add(QueryParser::parse($string));
81 1
        return $this;
82
    }
83
84
    /**
85
     * @param $string
86
     * @param bool   $field
87
     * @param null   $offsets
88
     * @return $this
89
     * @return $this
90
     */
91 1
    public function phrase($string, $field = false, $offsets = null)
92
    {
93 1
        $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...
94 1
        return $this;
95
96
    }
97
98
    /**
99
     * @param $string
100
     * @param bool   $field
101
     * @return $this
102
     */
103 1
    public function fuzzy($string, $field = false)
104
    {
105 1
        $this->query->add(new Fuzzy($this->term($field, $string)));
106 1
        return $this;
107
    }
108
109
    /**
110
     * @param $string
111
     * @param bool   $field
112
     * @return Term
113
     */
114 1
    protected function term($string, $field = false)
115
    {
116 1
        return new Term(strtoupper($string), $field);
117
    }
118
119
    /**
120
     * @param $string
121
     * @param bool   $field
122
     * @param array  $options
123
     */
124 1
    public function wildcard($string, $field = false, $options = [ ])
125
    {
126 1
        $this->query->add(new Wildcard($this->term($field, $string), $options));
0 ignored issues
show
Unused Code introduced by
The call to Wildcard::__construct() has too many arguments starting with $options.

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...
127 1
        return $this;
128
    }
129
130
    /**
131
     * @param $string
132
     * @param bool|string $field
133
     * @return $this|bool
134
     * @todo  Work out why the search only works if the string is uppercase...
135
     */
136 1
    public function where($string, $field = false)
137
    {
138 1
        is_array($field)
139 1
            ? $this->multiTerm($this->mapWhereArray($string, $field))
140 1
            : $this->query->add($this->singleTerm($string, $field));
0 ignored issues
show
Bug introduced by
It seems like $field defined by parameter $field on line 136 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...
141
142 1
        return $this;
143
    }
144
145
    /**
146
     * @param array $terms
147
     * @return $this
148
     */
149
    public function multiTerm(array $terms)
150
    {
151
        $multiTerm = new MultiTerm;
152
        foreach ($terms as $field => $value) {
153
            $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...
154
        }
155
156
        $this->query->add($multiTerm);
157
158
        return $this;
159
    }
160
161
    /**
162
     * @param $string
163
     * @param array  $array
164
     * @return mixed
165
     * @todo abstract this out
166
     */
167
    private function mapWhereArray($string, array $array)
168
    {
169
        return array_map(
170
            function() use ($string) {
171
                return $string;
172
            }, array_flip($array)
173
        );
174
    }
175
176
    /**
177
     * @param $string
178
     * @param $field
179
     * @return QueryTerm
180
     */
181 1
    public function singleTerm($string, $field = false)
182
    {
183 1
        return new QueryTerm($this->term($string, $field));
184
    }
185
186
    /**
187
     * @return mixed
188
     */
189
    public function hits()
190
    {
191
        return $this->mapIds(
192
            $this->index->limit($this->limit)->open($this->path)->get()->find($this->query->getBoolean())
0 ignored issues
show
Bug introduced by
It seems like $this->index->limit($thi...s->query->getBoolean()) targeting ZendSearch\Lucene\Index::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...
193
        );
194
    }
195
196
    /**
197
     * @param array $array
198
     * @return mixed
199
     * @todo abstract this out
200
     */
201
    private function mapIds(array $array)
202
    {
203
        return array_map(
204
            function(QueryHit $hit) {
205
                return $hit->id;
206
            }, $array
207
        );
208
    }
209
}
210