| Total Complexity | 59 |
| Total Lines | 174 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 6 | class ConceptSearchParameters |
||
| 7 | { |
||
| 8 | |||
| 9 | private $config; |
||
| 10 | private $request; |
||
| 11 | private $vocabs; |
||
| 12 | private $rest; |
||
| 13 | private $hidden; |
||
| 14 | private $unique; |
||
| 15 | |||
| 16 | public function __construct($request, $config, $rest = false) |
||
| 17 | { |
||
| 18 | $this->request = $request; |
||
| 19 | $this->config = $config; |
||
| 20 | $this->rest = $rest; |
||
| 21 | $this->hidden = true; |
||
| 22 | $this->unique = $request->getQueryParamBoolean('unique', false); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function getLang() |
||
| 26 | { |
||
| 27 | if ($this->rest && $this->request->getQueryParam('labellang')) { |
||
| 28 | return $this->request->getQueryParam('labellang'); |
||
| 29 | } |
||
| 30 | return $this->request->getLang(); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function getVocabs() |
||
| 42 | } |
||
| 43 | |||
| 44 | public function getVocabIds() |
||
| 45 | { |
||
| 46 | if ($this->rest || $this->request->getQueryParam('vocabs')) { |
||
| 47 | $vocabs = $this->rest ? $this->request->getQueryParam('vocab') : $this->request->getQueryParam('vocabs'); |
||
| 48 | return ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null; |
||
| 49 | } |
||
| 50 | $vocabs = $this->getVocabs(); |
||
| 51 | return isset($vocabs[0]) ? array($vocabs[0]->getId()) : null; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function setVocabularies($vocabs) |
||
| 55 | { |
||
| 56 | $this->vocabs = $vocabs; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getArrayClass() |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getSearchTerm() : string |
||
| 69 | { |
||
| 70 | $term = $this->request->getQueryParamRaw('q') !== null ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query'); |
||
| 71 | if ((!isset($term) || strlen(trim($term)) === 0) && $this->rest) |
||
| 72 | $term = $this->request->getQueryParamRaw('label'); |
||
| 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() |
||
| 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() |
||
| 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 | } else if ($type && strpos($type, '+')) { |
||
| 111 | $type = explode('+', $type); |
||
| 112 | } else if ($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 | return $this->request->getQueryParam($name) !== '' ? $this->request->getQueryParam($name) : null; |
||
| 124 | } |
||
| 125 | |||
| 126 | private function getQueryParamArray($name) { |
||
| 127 | return $this->request->getQueryParam($name) ? explode(' ', urldecode($this->request->getQueryParam($name))) : []; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getGroupLimit() |
||
| 131 | { |
||
| 132 | return $this->getQueryParam('group'); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getParentLimit() |
||
| 136 | { |
||
| 137 | return $this->getQueryParam('parent'); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getSchemeLimit() |
||
| 141 | { |
||
| 142 | return $this->getQueryParamArray('scheme'); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getOffset() |
||
| 146 | { |
||
| 147 | return ($this->request->getQueryParam('offset') && is_numeric($this->request->getQueryParam('offset')) && $this->request->getQueryParam('offset') >= 0) ? $this->request->getQueryParam('offset') : 0; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getSearchLimit() |
||
| 151 | { |
||
| 152 | if ($this->rest) { |
||
| 153 | return ($this->request->getQueryParam('maxhits')) ? $this->request->getQueryParam('maxhits') : 0; |
||
| 154 | } |
||
| 155 | return $this->config->getSearchResultsSize(); |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getUnique() { |
||
| 159 | return $this->unique; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function setUnique($unique) { |
||
| 163 | $this->unique = $unique; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getAdditionalFields() { |
||
| 168 | } |
||
| 169 | |||
| 170 | public function getHidden() { |
||
| 171 | return $this->hidden; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function setHidden($hidden) { |
||
| 175 | $this->hidden = $hidden; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getRest() { |
||
| 180 | } |
||
| 181 | } |
||
| 182 |