Search::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Laravel;
4
5
use BestServedCold\LaravelZendSearch\Lucene\Query;
6
use BestServedCold\LaravelZendSearch\Lucene\Search as LuceneSearch;
7
8
/**
9
 * Class Search
10
 * @package BestServedCold\LaravelZendSearch\Laravel
11
 */
12
class Search extends LuceneSearch
13
{
14
    use EloquentTrait;
15
16
    private $hits = [];
17
18
    /**
19
     * Search constructor.
20
     * @param Index $index
21
     * @param Query $query
22
     */
23 5
    public function __construct(Index $index, Query $query)
24
    {
25 5
        parent::__construct($index, $query);
26 5
        $this->path(config('search.index.path'));
27 5
    }
28
29
    /**
30
     * @return mixed
31
     */
32 1
    public function get()
33
    {
34 1
        return $this->model->whereIn(
35 1
            $this->key,
36 1
            $this->hits()
37 1
        )->get();
38
    }
39
40
    /**
41
     * @param $id
42
     * @return $this
43
     */
44 1
    public function findId($id)
45
    {
46 1
        $this->match((int) $id, 'xref_id');
47 1
        return $this;
48
    }
49
50
    /**
51
     * Hits
52
     *
53
     * We store the hits, no point in running the query twice.  This also allows us to set the UID once for the model
54
     * table name.  A new search instance is required to run against another model.
55
     *
56
     * @param  boolean $forEloquent
57
     * @return mixed
58
     */
59 2
    public function hits($forEloquent = true)
60
    {
61 2
        if (!empty($this->hits)) {
62 1
            return $this->hits;
63
        }
64
65 1
        $this->match(base64_encode($this->uid), 'uid');
66
67 1
        $this->hits = parent::hits($forEloquent);
0 ignored issues
show
Documentation Bug introduced by
It seems like parent::hits($forEloquent) can also be of type object<ZendSearch\Lucene\Search\QueryHit>. However, the property $hits is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
68 1
        return $this->hits;
69
    }
70
71
    /**
72
     * @param $string
73
     * @return $this
74
     */
75 1
    public function find($string)
76
    {
77 1
        $this->where($string);
78 1
        return $this;
79
    }
80
}
81