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 |
||
| 37 | class Request extends AbstractProxy |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @const string HTTP METHOD constant names |
||
| 41 | */ |
||
| 42 | const METHOD_OPTIONS = 'OPTIONS'; |
||
| 43 | const METHOD_GET = 'GET'; |
||
| 44 | const METHOD_HEAD = 'HEAD'; |
||
| 45 | const METHOD_PATCH = 'PATCH'; |
||
| 46 | const METHOD_POST = 'POST'; |
||
| 47 | const METHOD_PUT = 'PUT'; |
||
| 48 | const METHOD_DELETE = 'DELETE'; |
||
| 49 | const METHOD_TRACE = 'TRACE'; |
||
| 50 | const METHOD_CONNECT = 'CONNECT'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Init instance |
||
| 54 | * |
||
| 55 | * @throws ComponentException |
||
| 56 | */ |
||
| 57 | protected static function initInstance() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get Request Uri |
||
| 64 | * |
||
| 65 | * @return UriInterface |
||
| 66 | */ |
||
| 67 | 28 | public static function getRequestUri() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Retrieve a member of the $_SERVER super global |
||
| 74 | * |
||
| 75 | * If no $key is passed, returns the entire $_SERVER array. |
||
| 76 | * |
||
| 77 | * @param string $key |
||
| 78 | * @param string $default Default value to use if key not found |
||
| 79 | * @return string Returns null if key does not exist |
||
| 80 | */ |
||
| 81 | 4 | public static function getServer($key = null, $default = null) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Retrieve a member of the $_COOKIE super global |
||
| 88 | * |
||
| 89 | * If no $key is passed, returns the entire $_COOKIE array. |
||
| 90 | * |
||
| 91 | * @param string $key |
||
| 92 | * @param string $default Default value to use if key not found |
||
| 93 | * @return string Returns null if key does not exist |
||
| 94 | */ |
||
| 95 | public static function getCookie($key = null, $default = null) |
||
| 99 | /** |
||
| 100 | * Retrieve a member of the $_ENV super global |
||
| 101 | * |
||
| 102 | * If no $key is passed, returns the entire $_ENV array. |
||
| 103 | * |
||
| 104 | * @param string $key |
||
| 105 | * @param string $default Default value to use if key not found |
||
| 106 | * @return string Returns null if key does not exist |
||
| 107 | */ |
||
| 108 | public static function getEnv($key = null, $default = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Search for a header value |
||
| 115 | * |
||
| 116 | * @param string $header |
||
| 117 | * @param mixed $default |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | 8 | public static function getHeader($header, $default = null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Access values contained in the superglobals as public members |
||
| 127 | * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV |
||
| 128 | * |
||
| 129 | * @param string $key |
||
| 130 | * @param null $default |
||
| 131 | * @return mixed |
||
| 132 | * @link http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
||
| 133 | */ |
||
| 134 | 76 | public static function getParam($key, $default = null) |
|
| 135 | { |
||
| 136 | switch (true) { |
||
| 137 | 76 | case ($params = self::getInstance()->getQueryParams()) && isset($params[$key]): |
|
| 138 | 3 | return $params[$key]; |
|
| 139 | 74 | case ($params = self::getInstance()->getParsedBody()) && isset($params[$key]): |
|
| 140 | return $params[$key]; |
||
| 141 | 74 | case ($params = self::getInstance()->getCookieParams()) && isset($params[$key]): |
|
| 142 | return $params[$key]; |
||
| 143 | 74 | case ($params = self::getInstance()->getServerParams()) && isset($params[$key]): |
|
| 144 | return $params[$key]; |
||
| 145 | 74 | case isset($_ENV[$key]): |
|
| 146 | return $_ENV[$key]; |
||
| 147 | default: |
||
| 148 | 74 | return $default; |
|
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get all params from GET and POST or PUT |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | 39 | public static function getParams() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get uploaded file |
||
| 167 | * |
||
| 168 | * @param string $name |
||
| 169 | * @return \Zend\Diactoros\UploadedFile |
||
| 170 | */ |
||
| 171 | public static function getFile($name) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the client's IP address |
||
| 178 | * |
||
| 179 | * @param bool $checkProxy |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public static function getClientIp($checkProxy = true) |
||
| 183 | { |
||
| 184 | if ($checkProxy && self::getServer('HTTP_CLIENT_IP') != null) { |
||
| 185 | $ip = self::getServer('HTTP_CLIENT_IP'); |
||
| 186 | } else { |
||
| 187 | if ($checkProxy && self::getServer('HTTP_X_FORWARDED_FOR') != null) { |
||
| 188 | $ip = self::getServer('HTTP_X_FORWARDED_FOR'); |
||
| 189 | } else { |
||
| 190 | $ip = self::getServer('REMOTE_ADDR'); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | return $ip; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get module |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | 39 | public static function getModule() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Get controller |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | 39 | public static function getController() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Check CLI |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public static function isCli() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Check HTTP |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | 1 | public static function isHttp() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Is this a GET method request? |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public static function isGet() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Is this a POST method request? |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public static function isPost() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Is this a PUT method request? |
||
| 258 | * |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | public static function isPut() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Is this a DELETE method request? |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public static function isDelete() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Is the request a Javascript XMLHttpRequest? |
||
| 278 | * |
||
| 279 | * Should work with Prototype/Script.aculo.us, possibly others. |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 4 | public static function isXmlHttpRequest() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Get Accept MIME Type |
||
| 290 | * |
||
| 291 | * @param array $allowTypes |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | 1 | public static function getAccept($allowTypes = []) |
|
| 343 | } |
||
| 344 |