Completed
Push — master ( 16a564...d98303 )
by
unknown
14s
created

SearchQuery   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 128
Duplicated Lines 14.06 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
lcom 2
cbo 1
dl 18
loc 128
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A search() 9 9 2
A fuzzysearch() 9 9 2
A inClass() 0 7 1
A filter() 0 6 3
A exclude() 0 6 3
A start() 0 4 1
A limit() 0 4 1
A page() 0 5 1
A isfiltered() 0 4 4
A __toString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\FullTextSearch\Search\Queries;
4
5
use SilverStripe\View\ViewableData;
6
use stdClass;
7
8
/**
9
 * Represents a search query
10
 *
11
 * API very much still in flux.
12
 */
13
class SearchQuery extends ViewableData
14
{
15
    public static $missing = null;
16
    public static $present = null;
17
18
    public static $default_page_size = 10;
19
20
    /** These are public, but only for index & variant access - API users should not manually access these */
21
22
    public $search = [];
23
24
    public $classes = [];
25
26
    public $require = [];
27
    public $exclude = [];
28
29
    protected $start = 0;
30
    protected $limit = -1;
31
32
    /** These are the API functions */
33
34
    public function __construct()
35
    {
36
        if (self::$missing === null) {
37
            self::$missing = new stdClass();
38
        }
39
        if (self::$present === null) {
40
            self::$present = new stdClass();
41
        }
42
    }
43
44
    /**
45
     * @param string $text   Search terms. Exact format (grouping, boolean expressions, etc.) depends on
46
     *                       the search implementation.
47
     * @param array  $fields Limits the search to specific fields (using composite field names)
48
     * @param array  $boost  Map of composite field names to float values. The higher the value,
49
     *                       the more important the field gets for relevancy.
50
     */
51 View Code Duplication
    public function search($text, $fields = null, $boost = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $this->search[] = [
54
            'text' => $text,
55
            'fields' => $fields ? (array) $fields : null,
56
            'boost' => $boost,
57
            'fuzzy' => false
58
        ];
59
    }
60
61
    /**
62
     * Similar to {@link search()}, but uses stemming and other similarity algorithms
63
     * to find the searched terms. For example, a term "fishing" would also likely find results
64
     * containing "fish" or "fisher". Depends on search implementation.
65
     *
66
     * @param string $text   See {@link search()}
67
     * @param array  $fields See {@link search()}
68
     * @param array  $boost  See {@link search()}
69
     */
70 View Code Duplication
    public function fuzzysearch($text, $fields = null, $boost = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $this->search[] = [
73
            'text' => $text,
74
            'fields' => $fields ? (array) $fields : null,
75
            'boost' => $boost,
76
            'fuzzy' => true
77
        ];
78
    }
79
80
    public function inClass($class, $includeSubclasses = true)
81
    {
82
        $this->classes[] = [
83
            'class' => $class,
84
            'includeSubclasses' => $includeSubclasses
85
        ];
86
    }
87
88
    /**
89
     * Similar to {@link search()}, but typically used to further narrow down
90
     * based on other facets which don't influence the field relevancy.
91
     *
92
     * @param string $field  Composite name of the field
93
     * @param mixed  $values Scalar value, array of values, or an instance of SearchQuery_Range
94
     */
95
    public function filter($field, $values)
96
    {
97
        $requires = isset($this->require[$field]) ? $this->require[$field] : [];
98
        $values = is_array($values) ? $values : [$values];
99
        $this->require[$field] = array_merge($requires, $values);
100
    }
101
102
    /**
103
     * Excludes results which match these criteria, inverse of {@link filter()}.
104
     *
105
     * @param string $field
106
     * @param mixed $values
107
     */
108
    public function exclude($field, $values)
109
    {
110
        $excludes = isset($this->exclude[$field]) ? $this->exclude[$field] : [];
111
        $values = is_array($values) ? $values : [$values];
112
        $this->exclude[$field] = array_merge($excludes, $values);
113
    }
114
115
    public function start($start)
116
    {
117
        $this->start = $start;
118
    }
119
120
    public function limit($limit)
121
    {
122
        $this->limit = $limit;
123
    }
124
125
    public function page($page)
126
    {
127
        $this->start = $page * self::$default_page_size;
0 ignored issues
show
Documentation Bug introduced by
It seems like $page * self::$default_page_size can also be of type double. However, the property $start is declared as type integer. 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...
128
        $this->limit = self::$default_page_size;
129
    }
130
131
    public function isfiltered()
132
    {
133
        return $this->search || $this->classes || $this->require || $this->exclude;
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->search of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $this->classes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $this->require of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $this->exclude of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
134
    }
135
136
    public function __toString()
137
    {
138
        return "Search Query\n";
139
    }
140
}
141