ConceptSearchParameters   D
last analyzed

Complexity

Total Complexity 59

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 59
eloc 73
dl 0
loc 182
rs 4.08
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentLang() 0 3 1
A getDefaultTypeLimit() 0 9 2
A getAdditionalFields() 0 3 1
A getSchemeLimit() 0 3 1
A getVocabs() 0 9 3
A getLang() 0 6 3
B getTypeLimit() 0 15 9
A getArrayClass() 0 7 2
A getSearchLimit() 0 6 3
A getRest() 0 3 1
A getGroupLimit() 0 3 1
B getVocabIds() 0 8 7
A getSearchLang() 0 6 3
A getUnique() 0 3 1
A setVocabularies() 0 3 1
A getParentLimit() 0 3 1
A getOffset() 0 3 4
A getQueryParam() 0 3 2
A setHidden() 0 3 1
A __construct() 0 7 1
B getSearchTerm() 0 12 7
A getHidden() 0 3 1
A getQueryParamArray() 0 3 2
A setUnique() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like ConceptSearchParameters often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ConceptSearchParameters, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * ConceptSearchParameters is used for search parameters.
5
 */
6
class ConceptSearchParameters
7
{
8
    private $config;
9
    private $request;
10
    private $vocabs;
11
    private $rest;
12
    private $hidden;
13
    private $unique;
14
15
    public function __construct($request, $config, $rest = false)
16
    {
17
        $this->request = $request;
18
        $this->config = $config;
19
        $this->rest = $rest;
20
        $this->hidden = true;
21
        $this->unique = $request->getQueryParamBoolean('unique', false);
22
    }
23
24
    public function getLang()
25
    {
26
        if ($this->rest && $this->request->getQueryParam('labellang')) {
27
            return $this->request->getQueryParam('labellang');
28
        }
29
        return $this->request->getLang();
30
    }
31
32
    public function getVocabs()
33
    {
34
        if ($this->vocabs) {
35
            return $this->vocabs;
36
        }
37
        if ($this->request->getVocab()) {
38
            return array($this->request->getVocab());
39
        }
40
        return array();
41
    }
42
43
    public function getVocabIds()
44
    {
45
        if ($this->rest || $this->request->getQueryParam('vocabs')) {
46
            $vocabs = $this->rest ? $this->request->getQueryParam('vocab') : $this->request->getQueryParam('vocabs');
47
            return ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null;
48
        }
49
        $vocabs = $this->getVocabs();
50
        return isset($vocabs[0]) ? array($vocabs[0]->getId()) : null;
51
    }
52
53
    public function setVocabularies($vocabs)
54
    {
55
        $this->vocabs = $vocabs;
56
    }
57
58
    public function getArrayClass()
59
    {
60
        if (sizeof($this->getVocabs()) == 1) { // search within vocabulary
61
            $vocabs = $this->getVocabs();
62
            return $vocabs[0]->getConfig()->getArrayClassURI();
63
        }
64
        return null;
65
    }
66
67
    public function getSearchTerm(): string
68
    {
69
        $term = $this->request->getQueryParamRaw('q') !== null ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query');
70
        if ((!isset($term) || strlen(trim($term)) === 0) && $this->rest) {
71
            $term = $this->request->getQueryParamRaw('label');
72
        }
73
        $term = trim(strval($term)); // surrounding whitespace is not considered significant
74
        $term = Normalizer::normalize($term, Normalizer::FORM_C); //Normalize decomposed unicode characters #1184
75
        if ($this->rest) {
76
            return $term;
77
        }
78
        return strpos($term, "*") === false ? $term . "*" : $term; // default to prefix search
79
    }
80
81
    public function getContentLang()
82
    {
83
        return $this->request->getContentLang();
84
    }
85
86
    public function getSearchLang()
87
    {
88
        if ($this->rest) {
89
            return $this->request->getQueryParam('lang');
90
        }
91
        return $this->request->getQueryParam('anylang') ? '' : $this->getContentLang();
92
    }
93
94
    private function getDefaultTypeLimit()
95
    {
96
        $type = array('skos:Concept');
97
        if ($this->request->getVocab()) {
98
            $conf = $this->request->getVocab()->getConfig();
99
            $type[] = strval($conf->getArrayClassURI());
100
            $type[] = strval($conf->getGroupClassURI());
101
        }
102
        return array_filter($type, 'strlen');
103
    }
104
105
    public function getTypeLimit()
106
    {
107
        $type = $this->request->getQueryParam('type') !== '' ? $this->request->getQueryParam('type') : null;
108
        if ($type && strpos($type, ' ')) {
109
            $type = explode(' ', $type);
110
        } elseif ($type && strpos($type, '+')) {
111
            $type = explode('+', $type);
112
        } elseif ($type && !is_array($type)) {
113
            // if only one type param given place it into an array regardless
114
            $type = array($type);
115
        }
116
        if ($type === null) {
117
            return $this->getDefaultTypeLimit();
118
        }
119
        return $type;
120
    }
121
122
    private function getQueryParam($name)
123
    {
124
        return $this->request->getQueryParam($name) !== '' ? $this->request->getQueryParam($name) : null;
125
    }
126
127
    private function getQueryParamArray($name)
128
    {
129
        return $this->request->getQueryParam($name) ? explode(' ', urldecode($this->request->getQueryParam($name))) : [];
130
    }
131
132
    public function getGroupLimit()
133
    {
134
        return $this->getQueryParam('group');
135
    }
136
137
    public function getParentLimit()
138
    {
139
        return $this->getQueryParam('parent');
140
    }
141
142
    public function getSchemeLimit()
143
    {
144
        return $this->getQueryParamArray('scheme');
145
    }
146
147
    public function getOffset()
148
    {
149
        return ($this->request->getQueryParam('offset') && is_numeric($this->request->getQueryParam('offset')) && $this->request->getQueryParam('offset') >= 0) ? $this->request->getQueryParam('offset') : 0;
150
    }
151
152
    public function getSearchLimit()
153
    {
154
        if ($this->rest) {
155
            return ($this->request->getQueryParam('maxhits')) ? $this->request->getQueryParam('maxhits') : 0;
156
        }
157
        return $this->config->getSearchResultsSize();
158
    }
159
160
    public function getUnique()
161
    {
162
        return $this->unique;
163
    }
164
165
    public function setUnique($unique)
166
    {
167
        $this->unique = $unique;
168
    }
169
170
    public function getAdditionalFields()
171
    {
172
        return $this->getQueryParamArray('fields');
173
    }
174
175
    public function getHidden()
176
    {
177
        return $this->hidden;
178
    }
179
180
    public function setHidden($hidden)
181
    {
182
        $this->hidden = $hidden;
183
    }
184
185
    public function getRest()
186
    {
187
        return $this->rest;
188
    }
189
}
190