Completed
Push — master ( 8e10e8...309ab5 )
by Adam
02:51
created

Index::open()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2.0185
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
        var_dump($this->path());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->path()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
43 3
        $this->index = $this->index($this->path(), $forceCreate);
44
        return $this;
45
    }
46
47
    /**
48
     * @param integer $limit
49
     * @return $this
50
     */
51 1
    public function limit($limit)
52
    {
53 1
        Lucene::setResultSetLimit($limit);
54 1
        return $this;
55
    }
56
57
    /**
58
     * Index
59
     *
60
     * Extends the Lucene "open" method to create the index if it doesn't exist.
61
     *
62
     * @param  string|boolean $path
63
     * @param  boolean        $forceCreate
64
     * @return SearchIndexInterface
65
     * @throws \Exception
66
     */
67 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...
68
    {
69
        try {
70 3
            $index = Lucene::open($path);
71 3
        } catch (ExceptionInterface $error) {
72
            if ($forceCreate) {
73
                $index = Lucene::create($path);
74
            } else {
75
                throw $error;
76
            }
77
        }
78
        
79
        return $index;
80
    }
81
82
    /**
83
     * @param $path
84
     */
85 3
    public function setPath($path)
86
    {
87 3
        $this->path = $path;
88 3
    }
89
90
    /**
91
     * @return string
92
     * @throws \Exception
93
     */
94 4
    protected function path()
95
    {
96 4
        if (!$this->path) {
97 1
            throw new \Exception('No path specified nor config variable set.');
98
        }
99
100 3
        return $this->path;
101
    }
102
103
    /**
104
     * @return LuceneIndex
105
     */
106
    public function get()
107
    {
108
        return $this->index;
109
    }
110
}
111