Completed
Pull Request — master (#12)
by Adam
03:18
created

Filter::setAnalyzer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Lucene;
4
5
use BestServedCold\LaravelZendSearch\Lucene\Filter\Factory;
6
use ZendSearch\Lucene\Analysis\Analyzer\Analyzer;
7
use ZendSearch\Lucene\Analysis\Analyzer\Common\AbstractCommon;
8
use ZendSearch\Lucene\Analysis\Analyzer\Common\TextNum\CaseInsensitive;
9
10
/**
11
 * Class Filter
12
 *
13
 * @package BestServedCold\LaravelZendSearch\Lucene
14
 */
15
class Filter
16
{
17
    /**
18
     * @var null|AbstractCommon
19
     */
20
    private static $analyzer = null;
21
22
    /**
23
     * @var array
24
     */
25
    private static $filters = [];
26
27
    /**
28
     * Filter constructor.
29
     *
30
     * @param CaseInsensitive $caseInsensitive
31
     */
32 29
    public function __construct(CaseInsensitive $caseInsensitive)
33
    {
34 29
        self::$analyzer = self::$analyzer ?: $caseInsensitive;
35 29
    }
36
37
    /**
38
     * @param AbstractCommon $analyzer
39
     */
40 1
    public static function setAnalyzer(AbstractCommon $analyzer)
41
    {
42 1
        self::$analyzer = $analyzer;
43 1
    }
44
45
    /**
46
     * @return null|AbstractCommon|CaseInsensitive
47
     */
48 2
    public static function getAnalyzer()
49
    {
50 2
        return self::$analyzer;
51
    }
52
53
    /**
54
     * @param string $filter
55
     * @param mixed  $option
56
     */
57 22
    public static function addFilter($filter, $option)
58
    {
59 22
        if (self::$analyzer === null) {
60 1
            self::$analyzer = new CaseInsensitive;
61 1
        }
62
63 22
        if (!in_array($filter, self::$filters)) {
64 1
            self::$analyzer->addFilter(Factory::getFilter($filter, $option));
65 1
            self::$filters[] = $filter;
66 1
        }
67
68 22
    }
69
70
    /**
71
     * @return void
72
     */
73 7
    public function setFilters()
74
    {
75 7
        Analyzer::setDefault(self::$analyzer);
0 ignored issues
show
Bug introduced by
It seems like self::$analyzer can be null; however, setDefault() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
76 7
    }
77
}
78