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

Filter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 63
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A setAnalyzer() 0 4 1
A getAnalyzer() 0 4 1
A addFilter() 0 12 3
A setFilters() 0 4 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