Completed
Push — master ( e5cca7...d44234 )
by Adam
03:05 queued 13s
created

Index::open()   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
use ZendSearch\Lucene\Analysis\Analyzer\Analyzer;
6
use ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8Num\CaseInsensitive;
7
use ZendSearch\Lucene\Lucene;
8
use ZendSearch\Exception\ExceptionInterface;
9
use ZendSearch\Lucene\Index as LuceneIndex;
10
use ZendSearch\Lucene\SearchIndexInterface;
11
12
/**
13
 * Class Index
14
 * @package BestServedCold\LaravelZendSearch\Lucene
15
 */
16
class Index
17
{
18
    /**
19
     * @var LuceneIndex
20
     */
21
    private $index;
22
23
    /**
24
     * @var string
25
     */
26
    protected $path;
27
28
    /**
29
     * Open
30
     *
31
     * @param  bool $path
32
     * @param  bool $forceCreate
33
     * @return $this
34
     * @throws ExceptionInterface
35
     * @throws \Exception
36
     */
37 4
    public function open($path = false, $forceCreate = true)
38
    {
39 4
        $this->path = $path ? $path : $this->path;
0 ignored issues
show
Documentation Bug introduced by
It seems like $path ? $path : $this->path can also be of type boolean. However, the property $path is declared as type string. 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...
40 4
        Analyzer::setDefault(new CaseInsensitive);
41
42 4
        $this->index = $this->index($this->path(), $forceCreate);
43 2
        return $this;
44
    }
45
46
    /**
47
     * @param integer $limit
48
     * @return $this
49
     */
50 1
    public function limit($limit)
51
    {
52 1
        Lucene::setResultSetLimit($limit);
53 1
        return $this;
54
    }
55
56
    /**
57
     * Index
58
     *
59
     * Extends the Lucene "open" method to create the index if it doesn't exist.
60
     *
61
     * @param  string|boolean $path
62
     * @param  boolean        $forceCreate
63
     * @return SearchIndexInterface
64
     * @throws \Exception
65
     */
66 3
    private function index($path, $forceCreate = true)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
67 1
    {
68
        try {
69 3
            $index = Lucene::open($path);
70 3
        } catch (ExceptionInterface $error) {
71 3
            if ($forceCreate) {
72 2
                $index = Lucene::create($path);
73 2
            } else {
74 1
                throw $error;
75
            }
76
        }
77
        
78 2
        return $index;
79
    }
80
81
    /**
82
     * @param $path
83
     */
84 3
    public function setPath($path)
85
    {
86 3
        $this->path = $path;
87 3
    }
88
89
    /**
90
     * @return string
91
     * @throws \Exception
92
     */
93 4
    protected function path()
94
    {
95 4
        if (!$this->path) {
96 1
            throw new \Exception('No path specified nor config variable set.');
97
        }
98
99 3
        return $this->path;
100
    }
101
102
    /**
103
     * @return LuceneIndex
104
     */
105 1
    public function get()
106
    {
107 1
        return $this->index;
108
    }
109
}
110