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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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() |
||
| 34 | { |
||
| 35 | if ($this->vocabs) { |
||
| 36 | return $this->vocabs; |
||
| 37 | } |
||
| 38 | if ($this->request->getVocab()) { |
||
| 39 | return array($this->request->getVocab()); |
||
| 40 | } |
||
| 41 | return array(); |
||
| 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() |
||
| 60 | { |
||
| 61 | if (sizeof($this->getVocabs()) == 1) { // search within vocabulary |
||
| 62 | $vocabs = $this->getVocabs(); |
||
| 63 | return $vocabs[0]->getConfig()->getArrayClassURI(); |
||
| 64 | } |
||
| 65 | return null; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getSearchTerm() |
||
| 69 | { |
||
| 70 | $term = $this->request->getQueryParamRaw('q') ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query'); |
||
| 71 | if (!$term && $this->rest) |
||
| 72 | $term = $this->request->getQueryParamRaw('label'); |
||
| 73 | $term = trim($term); // surrounding whitespace is not considered significant |
||
| 74 | if ($this->rest) { |
||
| 75 | return $term; |
||
| 76 | } |
||
| 77 | return strpos($term, "*") === false ? $term . "*" : $term; // default to prefix search |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getContentLang() |
||
| 83 | } |
||
| 84 | |||
| 85 | public function getSearchLang() |
||
| 86 | { |
||
| 87 | if ($this->rest) { |
||
| 88 | return $this->request->getQueryParam('lang'); |
||
| 89 | } |
||
| 90 | return $this->request->getQueryParam('anylang') ? '' : $this->getContentLang(); |
||
| 91 | } |
||
| 92 | |||
| 93 | private function getDefaultTypeLimit() |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getTypeLimit() |
||
| 105 | { |
||
| 106 | $type = $this->request->getQueryParam('type') !== '' ? $this->request->getQueryParam('type') : null; |
||
| 107 | if ($type && strpos($type, ' ')) { |
||
| 108 | $type = explode(' ', $type); |
||
| 109 | } else if ($type && strpos($type, '+')) { |
||
| 110 | $type = explode('+', $type); |
||
| 111 | } else if ($type && !is_array($type)) { |
||
| 112 | // if only one type param given place it into an array regardless |
||
| 113 | $type = array($type); |
||
| 114 | } |
||
| 115 | if ($type === null) { |
||
| 116 | return $this->getDefaultTypeLimit(); |
||
| 117 | } |
||
| 118 | return $type; |
||
| 119 | } |
||
| 120 | |||
| 121 | private function getQueryParam($name) { |
||
| 122 | return $this->request->getQueryParam($name) !== '' ? $this->request->getQueryParam($name) : null; |
||
| 123 | } |
||
| 124 | |||
| 125 | private function getQueryParamArray($name) { |
||
| 126 | return $this->request->getQueryParam($name) ? explode(' ', urldecode($this->request->getQueryParam($name))) : []; |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getGroupLimit() |
||
| 130 | { |
||
| 131 | return $this->getQueryParam('group'); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getParentLimit() |
||
| 135 | { |
||
| 136 | return $this->getQueryParam('parent'); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getSchemeLimit() |
||
| 140 | { |
||
| 141 | return $this->getQueryParamArray('scheme'); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getOffset() |
||
| 145 | { |
||
| 146 | return ($this->request->getQueryParam('offset') && is_numeric($this->request->getQueryParam('offset')) && $this->request->getQueryParam('offset') >= 0) ? $this->request->getQueryParam('offset') : 0; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getSearchLimit() |
||
| 150 | { |
||
| 151 | if ($this->rest) { |
||
| 152 | return ($this->request->getQueryParam('maxhits')) ? $this->request->getQueryParam('maxhits') : 0; |
||
| 153 | } |
||
| 154 | return $this->config->getSearchResultsSize(); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getUnique() { |
||
| 158 | return $this->unique; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function setUnique($unique) { |
||
| 162 | $this->unique = $unique; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function getAdditionalFields() { |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getHidden() { |
||
| 170 | return $this->hidden; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function setHidden($hidden) { |
||
| 174 | $this->hidden = $hidden; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getRest() { |
||
| 179 | } |
||
| 180 | } |
||
| 181 |