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. 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Request |
||
| 7 | { |
||
| 8 | |||
| 9 | private $lang; |
||
| 10 | private $clang; |
||
| 11 | private $page; |
||
| 12 | private $vocab; |
||
| 13 | private $vocabids; |
||
| 14 | private $uri; |
||
| 15 | private $letter; |
||
| 16 | private $model; |
||
| 17 | private $queryParams; |
||
| 18 | private $queryParamsPOST; |
||
| 19 | private $serverConstants; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Initializes the Request Object |
||
| 23 | */ |
||
| 24 | public function __construct($model) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set a GET query parameter to mock it in tests. |
||
| 52 | * @param string $paramName parameter name |
||
| 53 | * @param string $value parameter value |
||
| 54 | */ |
||
| 55 | public function setQueryParam($paramName, $value) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set a SERVER constant to mock it in tests. |
||
| 62 | * @param string $paramName parameter name |
||
| 63 | * @param string $value parameter value |
||
| 64 | */ |
||
| 65 | public function setServerConstant($paramName, $value) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return the requested GET query parameter as a string. Backslashes are stripped for security reasons. |
||
| 72 | * @param string $paramName parameter name |
||
| 73 | * @return string parameter content, or null if no parameter found |
||
| 74 | */ |
||
| 75 | public function getQueryParam($paramName) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Return the requested GET query parameter as a string, with no sanitizing. |
||
| 84 | * @param string $paramName parameter name |
||
| 85 | * @return string parameter content, or null if no parameter found |
||
| 86 | */ |
||
| 87 | public function getQueryParamRaw($paramName) |
||
| 91 | |||
| 92 | public function getQueryParamPOST($paramName) |
||
| 97 | |||
| 98 | public function getQueryParamBoolean($paramName, $default) |
||
| 106 | |||
| 107 | public function getServerConstant($paramName) |
||
| 112 | |||
| 113 | public function getLang() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sets the language variable |
||
| 120 | * @param string $lang |
||
| 121 | */ |
||
| 122 | public function setLang($lang) |
||
| 129 | |||
| 130 | public function getContentLang() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Sets the language variable |
||
| 137 | * @param string $clang |
||
| 138 | */ |
||
| 139 | public function setContentLang($clang) |
||
| 143 | |||
| 144 | private function verifyContentLang($lang) |
||
| 152 | |||
| 153 | public function getPage() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets the page id variable eg. 'groups' |
||
| 160 | * @param string $page |
||
| 161 | */ |
||
| 162 | public function setPage($page) |
||
| 169 | |||
| 170 | public function getRequestUri() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the relative page url eg. '/yso/fi/search?clang=en&q=cat' |
||
| 177 | * @return string the relative url of the page |
||
| 178 | */ |
||
| 179 | public function getLangUrl() |
||
| 183 | |||
| 184 | public function getLetter() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Sets the page id variable eg. 'B' |
||
| 191 | * @param string $letter |
||
| 192 | */ |
||
| 193 | public function setLetter($letter) |
||
| 200 | |||
| 201 | public function getURI() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Sets the uri variable |
||
| 208 | * @param string $uri |
||
| 209 | */ |
||
| 210 | public function setURI($uri) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Used to set the vocab id variable when multiple vocabularies have been chosen eg. 'lcsh+yso' |
||
| 220 | * @param string $ids |
||
| 221 | */ |
||
| 222 | public function setVocabids($ids) |
||
| 226 | |||
| 227 | public function getVocabid() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Creates a Vocabulary object |
||
| 238 | * @param string $vocabid |
||
| 239 | */ |
||
| 240 | public function setVocab($vocabid) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return Vocabulary |
||
| 253 | */ |
||
| 254 | public function getVocab() |
||
| 258 | |||
| 259 | public function getVocabList() { |
||
| 262 | |||
| 263 | public function getPlugins() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return the version of this Skosmos installation, or "unknown" if |
||
| 272 | * it cannot be determined. The version information is based on Git tags. |
||
| 273 | * @return string version |
||
| 274 | */ |
||
| 275 | public function getVersion() : string |
||
| 279 | } |
||
| 280 |