| Total Complexity | 57 | 
| Total Lines | 312 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 6 | class Request | ||
| 7 | { | ||
| 8 | private $lang; | ||
| 9 | private $clang; | ||
| 10 | private $page; | ||
| 11 | private $vocab; | ||
| 12 | private $vocabids; | ||
| 13 | private $uri; | ||
| 14 | private $letter; | ||
| 15 | private $model; | ||
| 16 | private $queryParams; | ||
| 17 | private $queryParamsPOST; | ||
| 18 | private $serverConstants; | ||
| 19 | private $cookies; | ||
| 20 | |||
| 21 | /** | ||
| 22 | * Initializes the Request Object | ||
| 23 | */ | ||
| 24 | public function __construct($model) | ||
| 25 |     { | ||
| 26 | $this->model = $model; | ||
| 27 | |||
| 28 | // Store GET parameters in a local array, so we can mock them in tests. | ||
| 29 | // We do not apply any filters at this point. | ||
| 30 | $this->queryParams = []; | ||
| 31 |         foreach (filter_input_array(INPUT_GET) ?: [] as $key => $val) { | ||
| 32 | $this->queryParams[$key] = $val; | ||
| 33 | } | ||
| 34 | |||
| 35 | // Store POST parameters in a local array, so we can mock them in tests. | ||
| 36 | // We do not apply any filters at this point. | ||
| 37 | $this->queryParamsPOST = []; | ||
| 38 |         foreach (filter_input_array(INPUT_POST) ?: [] as $key => $val) { | ||
| 39 | $this->queryParamsPOST[$key] = $val; | ||
| 40 | } | ||
| 41 | |||
| 42 | // Store SERVER parameters in a local array, so we can mock them in tests. | ||
| 43 | // We do not apply any filters at this point. | ||
| 44 | $this->serverConstants = []; | ||
| 45 |         foreach (filter_input_array(INPUT_SERVER) ?: [] as $key => $val) { | ||
| 46 | $this->serverConstants[$key] = $val; | ||
| 47 | } | ||
| 48 | |||
| 49 | // Store cookies in a local array, so we can mock them in tests. | ||
| 50 | // We do not apply any filters at this point. | ||
| 51 | $this->cookies = []; | ||
| 52 |         foreach (filter_input_array(INPUT_COOKIE) ?: [] as $key => $val) { | ||
| 53 | $this->cookies[$key] = $val; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | /** | ||
| 58 | * Set a GET query parameter to mock it in tests. | ||
| 59 | * @param string $paramName parameter name | ||
| 60 | * @param string $value parameter value | ||
| 61 | */ | ||
| 62 | public function setQueryParam($paramName, $value) | ||
| 63 |     { | ||
| 64 | $this->queryParams[$paramName] = $value; | ||
| 65 | } | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Set a SERVER constant to mock it in tests. | ||
| 69 | * @param string $paramName parameter name | ||
| 70 | * @param string $value parameter value | ||
| 71 | */ | ||
| 72 | public function setServerConstant($paramName, $value) | ||
| 73 |     { | ||
| 74 | $this->serverConstants[$paramName] = $value; | ||
| 75 | } | ||
| 76 | |||
| 77 | /** | ||
| 78 | * Set a cookie to mock it in tests. | ||
| 79 | * @param string $paramName parameter name | ||
| 80 | * @param string $value parameter value | ||
| 81 | */ | ||
| 82 | public function setCookie($paramName, $value) | ||
| 83 |     { | ||
| 84 | $this->cookies[$paramName] = $value; | ||
| 85 | } | ||
| 86 | |||
| 87 | /** | ||
| 88 | * Return the requested GET query parameter as a string. Backslashes are stripped for security reasons. | ||
| 89 | * @param string $paramName parameter name | ||
| 90 | * @return string parameter content, or null if no parameter found | ||
| 91 | */ | ||
| 92 | public function getQueryParam($paramName) | ||
| 93 |     { | ||
| 94 |         if (!isset($this->queryParams[$paramName])) { | ||
| 95 | return null; | ||
| 96 | } | ||
| 97 | $val = filter_var($this->queryParams[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS); | ||
|  | |||
| 98 |         return ($val !== null ? str_replace('\\', '', $val) : null); | ||
| 99 | } | ||
| 100 | |||
| 101 | /** | ||
| 102 | * Return the requested GET query parameter as a string, with no sanitizing. | ||
| 103 | * @param string $paramName parameter name | ||
| 104 | * @return string parameter content, or null if no parameter found | ||
| 105 | */ | ||
| 106 | public function getQueryParamRaw($paramName) | ||
| 107 |     { | ||
| 108 | return isset($this->queryParams[$paramName]) ? $this->queryParams[$paramName] : null; | ||
| 109 | } | ||
| 110 | |||
| 111 | public function getQueryParamPOST($paramName) | ||
| 117 | } | ||
| 118 | |||
| 119 | public function getQueryParamBoolean($paramName, $default) | ||
| 120 |     { | ||
| 121 | $val = $this->getQueryParamRaw($paramName); | ||
| 122 |         if ($val !== null) { | ||
| 123 | $val = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); | ||
| 124 | } | ||
| 125 | return ($val !== null) ? $val : $default; | ||
| 126 | } | ||
| 127 | |||
| 128 | public function getServerConstant($paramName) | ||
| 129 |     { | ||
| 130 |         if (!isset($this->serverConstants[$paramName])) { | ||
| 131 | return null; | ||
| 132 | } | ||
| 133 | return filter_var($this->serverConstants[$paramName], FILTER_SANITIZE_ADD_SLASHES); | ||
| 134 | } | ||
| 135 | |||
| 136 | public function getCookie($paramName) | ||
| 137 |     { | ||
| 138 |         if (!isset($this->cookies[$paramName])) { | ||
| 139 | return null; | ||
| 140 | } | ||
| 141 | return filter_var($this->cookies[$paramName], FILTER_SANITIZE_FULL_SPECIAL_CHARS); | ||
| 142 | } | ||
| 143 | |||
| 144 | public function getLang() | ||
| 145 |     { | ||
| 146 | return $this->lang; | ||
| 147 | } | ||
| 148 | |||
| 149 | /** | ||
| 150 | * Sets the language variable | ||
| 151 | * @param string $lang | ||
| 152 | */ | ||
| 153 | public function setLang($lang) | ||
| 154 |     { | ||
| 155 |         if ($lang !== '') { | ||
| 156 | $this->lang = $lang; | ||
| 157 | } | ||
| 158 | |||
| 159 | } | ||
| 160 | |||
| 161 | public function getContentLang() | ||
| 162 |     { | ||
| 163 | return $this->clang; | ||
| 164 | } | ||
| 165 | |||
| 166 | /** | ||
| 167 | * Sets the language variable | ||
| 168 | * @param string $clang | ||
| 169 | */ | ||
| 170 | public function setContentLang($clang) | ||
| 171 |     { | ||
| 172 | $this->clang = $this->verifyContentLang($clang); | ||
| 173 | } | ||
| 174 | |||
| 175 | private function verifyContentLang($lang) | ||
| 176 |     { | ||
| 177 |         if ($this->vocab) { | ||
| 178 | return $this->vocab->verifyVocabularyLanguage($lang); | ||
| 179 | } | ||
| 180 | |||
| 181 | return $lang; | ||
| 182 | } | ||
| 183 | |||
| 184 | public function getPage() | ||
| 185 |     { | ||
| 186 | return $this->page; | ||
| 187 | } | ||
| 188 | |||
| 189 | /** | ||
| 190 | * Sets the page id variable eg. 'groups' | ||
| 191 | * @param string $page | ||
| 192 | */ | ||
| 193 | public function setPage($page) | ||
| 194 |     { | ||
| 195 |         if ($page !== '') { | ||
| 196 | $this->page = $page; | ||
| 197 | } | ||
| 198 | |||
| 199 | } | ||
| 200 | |||
| 201 | public function getRequestUri() | ||
| 202 |     { | ||
| 203 |         return $this->getServerConstant('HTTP_HOST') . $this->getServerConstant('REQUEST_URI'); | ||
| 204 | } | ||
| 205 | |||
| 206 | /** | ||
| 207 | * Returns the relative page url eg. '/yso/fi/search?clang=en&q=cat' | ||
| 208 | * @param string $newlang new UI language to set | ||
| 209 | * @return string the relative url of the page | ||
| 210 | */ | ||
| 211 | public function getLangUrl($newlang = null) | ||
| 212 |     { | ||
| 213 |         $script_name = str_replace('/src/index.php', '', $this->getServerConstant('SCRIPT_NAME')); | ||
| 214 |         $langurl = substr(str_replace($script_name, '', strval($this->getServerConstant('REQUEST_URI'))), 1); | ||
| 215 |         if ($newlang !== null) { | ||
| 216 |             $langurl = preg_replace("#^(.*/)?{$this->lang}/#", "$1{$newlang}/", $langurl); | ||
| 217 | } | ||
| 218 | // make sure that the resulting URL isn't interpreted as an absolute URL | ||
| 219 |         $langurl = str_replace(":", "", $langurl); | ||
| 220 | return $langurl; | ||
| 221 | } | ||
| 222 | |||
| 223 | public function getLetter() | ||
| 226 | } | ||
| 227 | |||
| 228 | /** | ||
| 229 | * Sets the page id variable eg. 'B' | ||
| 230 | * @param string $letter | ||
| 231 | */ | ||
| 232 | public function setLetter($letter) | ||
| 233 |     { | ||
| 234 |         if ($letter !== '') { | ||
| 235 | $this->letter = $letter; | ||
| 236 | } | ||
| 237 | |||
| 238 | } | ||
| 239 | |||
| 240 | public function getURI() | ||
| 241 |     { | ||
| 242 | return $this->uri; | ||
| 243 | } | ||
| 244 | |||
| 245 | /** | ||
| 246 | * Sets the uri variable | ||
| 247 | * @param string $uri | ||
| 248 | */ | ||
| 249 | public function setURI($uri) | ||
| 250 |     { | ||
| 251 |         if ($uri !== '') { | ||
| 252 | $this->uri = rtrim(strval($uri)); | ||
| 253 | } | ||
| 254 | |||
| 255 | } | ||
| 256 | |||
| 257 | /** | ||
| 258 | * Used to set the vocab id variable when multiple vocabularies have been chosen eg. 'lcsh+yso' | ||
| 259 | * @param string $ids | ||
| 260 | */ | ||
| 261 | public function setVocabids($ids) | ||
| 262 |     { | ||
| 263 | $this->vocabids = $ids; | ||
| 264 | } | ||
| 265 | |||
| 266 | public function getVocabid() | ||
| 267 |     { | ||
| 268 |         if ($this->vocabids) { | ||
| 269 | return $this->vocabids; | ||
| 270 | } | ||
| 271 | |||
| 272 | return isset($this->vocab) ? $this->vocab->getId() : ''; | ||
| 273 | } | ||
| 274 | |||
| 275 | /** | ||
| 276 | * Creates a Vocabulary object | ||
| 277 | * @param string $vocabid | ||
| 278 | */ | ||
| 279 | public function setVocab($vocabid) | ||
| 280 |     { | ||
| 281 |         if (strpos($vocabid, ' ') !== false) { // if there are multiple vocabularies just storing the string | ||
| 282 | $this->setVocabids($vocabid); | ||
| 283 |         } else { | ||
| 284 | $this->vocab = $this->model->getVocabulary($vocabid); | ||
| 285 | } | ||
| 286 | |||
| 287 | } | ||
| 288 | |||
| 289 | /** | ||
| 290 | * @return Vocabulary | ||
| 291 | */ | ||
| 292 | public function getVocab() | ||
| 293 |     { | ||
| 294 | return $this->vocab; | ||
| 295 | } | ||
| 296 | |||
| 297 | public function getVocabList() | ||
| 300 | } | ||
| 301 | |||
| 302 | public function getPlugins() | ||
| 303 |     { | ||
| 304 |         if ($this->vocab) { | ||
| 305 | return $this->vocab->getConfig()->getPluginRegister(); | ||
| 306 | } | ||
| 307 | return new PluginRegister($this->model->getConfig()->getGlobalPlugins()); | ||
| 308 | } | ||
| 309 | |||
| 310 | /** | ||
| 311 | * Return the version of this Skosmos installation, or "unknown" if | ||
| 312 | * it cannot be determined. The version information is based on Git tags. | ||
| 313 | * @return string version | ||
| 314 | */ | ||
| 315 | public function getVersion(): string | ||
| 318 | } | ||
| 319 | } | ||
| 320 |