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 |
||
| 36 | class Request |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * Ip address |
||
| 40 | * |
||
| 41 | * @var mixed |
||
| 42 | */ |
||
| 43 | protected $_ip = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Class constructor |
||
| 47 | * |
||
| 48 | * Initialize Request. |
||
| 49 | * |
||
| 50 | * @return void |
||
|
|
|||
| 51 | */ |
||
| 52 | 9 | public function __construct() |
|
| 53 | { |
||
| 54 | 9 | Hook::listen(__CLASS__); |
|
| 55 | 9 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Internal method used to retrieve values from given arrays. |
||
| 59 | * |
||
| 60 | * @param array $source |
||
| 61 | * @param mixed $key |
||
| 62 | * @param mixed $default |
||
| 63 | * @return mixed |
||
| 64 | */ |
||
| 65 | protected function getRequestParams(&$source, $key = null, $default = null) |
||
| 66 | { |
||
| 67 | // If $key is NULL, it means that the whole $source is requested |
||
| 68 | if (!isset($key) || $key == null) { |
||
| 69 | $key = array_keys($source); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (is_array($key)) { |
||
| 73 | $output = []; |
||
| 74 | foreach ($key as $k) { |
||
| 75 | $output[$k] = $this->getRequestParams($source, $k); |
||
| 76 | } |
||
| 77 | |||
| 78 | return $output; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (isset($source[$key])) { |
||
| 82 | $value = $source[$key]; |
||
| 83 | } else { |
||
| 84 | return $default; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $value; |
||
| 88 | |||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Fetch an item from the GET array |
||
| 93 | * |
||
| 94 | * @param mixed $key |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | public function get($key = null, $default = null) |
||
| 98 | { |
||
| 99 | return $this->getRequestParams($_GET, $key, $default); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Fetch an item from the POST array |
||
| 104 | * |
||
| 105 | * @param mixed $key |
||
| 106 | * @param mixed $default |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | public function post($key = null, $default = null) |
||
| 110 | { |
||
| 111 | $rawPostData = file_get_contents('php://input'); |
||
| 112 | $source = json_decode($rawPostData, true); |
||
| 113 | if (json_last_error() != JSON_ERROR_NONE) { |
||
| 114 | $source = $_POST; |
||
| 115 | } |
||
| 116 | |||
| 117 | return $this->getRequestParams($source, $key, $default); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Fetch an item from the SERVER array |
||
| 122 | * |
||
| 123 | * @param mixed $key |
||
| 124 | * @param mixed $default |
||
| 125 | * @return mixed |
||
| 126 | */ |
||
| 127 | public function server($key = null, $default = null) |
||
| 128 | { |
||
| 129 | return $this->getRequestParams($_SERVER, $key, $default); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Set or Get cookie |
||
| 134 | * |
||
| 135 | * @param mixed $key |
||
| 136 | * @param string $value |
||
| 137 | * @param mixed $options |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | 2 | public function cookie($key = '', $value = '', $options = null) |
|
| 141 | { |
||
| 142 | $defaultOptions = [ |
||
| 143 | 2 | 'prefix' => '', |
|
| 144 | 'expire' => 86400, |
||
| 145 | 'path' => '/', |
||
| 146 | 'secure' => false, |
||
| 147 | 'httponly' => false, |
||
| 148 | ]; |
||
| 149 | |||
| 150 | 2 | if (!is_null($options)) { |
|
| 151 | if (is_numeric($options)) { |
||
| 152 | $options = ['expire' => $options]; |
||
| 153 | } elseif (is_string($options)) { |
||
| 154 | parse_str($options, $options); |
||
| 155 | } |
||
| 156 | |||
| 157 | $options = array_merge($defaultOptions, array_change_key_case($options)); |
||
| 158 | } |
||
| 159 | |||
| 160 | 2 | if (!empty($options['httponly'])) { |
|
| 161 | ini_set('session.cookie_httponly', 1); |
||
| 162 | } |
||
| 163 | |||
| 164 | 2 | if (is_null($key)) { |
|
| 165 | if (empty($_COOKIE)) { |
||
| 166 | return null; |
||
| 167 | } |
||
| 168 | |||
| 169 | $prefix = empty($value) ? $options['prefix'] : $value; |
||
| 170 | if (!empty($prefix)) { |
||
| 171 | foreach ($_COOKIE as $key => $val) { |
||
| 172 | if (0 === stripos($key, $prefix)) { |
||
| 173 | setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
||
| 174 | unset($_COOKIE[$key]); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return null; |
||
| 180 | 2 | } elseif ('' === $key) { |
|
| 181 | // Get All Cookie |
||
| 182 | return $_COOKIE; |
||
| 183 | } |
||
| 184 | |||
| 185 | 2 | $key = $options['prefix'] . str_replace('.', '_', $key); |
|
| 186 | 2 | if ('' === $value) { |
|
| 187 | 2 | if (isset($_COOKIE[$key])) { |
|
| 188 | 1 | $value = $_COOKIE[$key]; |
|
| 189 | 1 | if (0 === strpos($value, 'kotori:')) { |
|
| 190 | $value = substr($value, 6); |
||
| 191 | return array_map('urldecode', json_decode(MAGIC_QUOTES_GPC ? stripslashes($value) : $value, true)); |
||
| 192 | } else { |
||
| 193 | 1 | return $value; |
|
| 194 | } |
||
| 195 | } else { |
||
| 196 | 1 | return null; |
|
| 197 | } |
||
| 198 | } else { |
||
| 199 | 2 | if (is_null($value)) { |
|
| 200 | 1 | setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
|
| 201 | 1 | unset($_COOKIE[$key]); // Delete Cookie |
|
| 202 | } else { |
||
| 203 | // Set Cookie |
||
| 204 | 2 | if (is_array($value)) { |
|
| 205 | $value = 'kotori:' . json_encode(array_map('urlencode', $value)); |
||
| 206 | } |
||
| 207 | |||
| 208 | 2 | $expire = !empty($options['expire']) ? time() + intval($options['expire']) : 0; |
|
| 209 | 2 | setcookie($key, $value, $expire, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
|
| 210 | 2 | $_COOKIE[$key] = $value; |
|
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | 2 | return null; |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Set or Get session |
||
| 219 | * |
||
| 220 | * @param mixed $key |
||
| 221 | * @param string $value |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | 2 | public function session($key = '', $value = '') |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Is HTTPS? |
||
| 252 | * |
||
| 253 | * Determines if the application is accessed via an encrypted |
||
| 254 | * (HTTPS) connection. |
||
| 255 | * |
||
| 256 | * @return boolean |
||
| 257 | */ |
||
| 258 | 2 | public function isSecure() |
|
| 259 | { |
||
| 260 | 2 | if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { |
|
| 261 | return true; |
||
| 262 | 2 | } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) { |
|
| 263 | return true; |
||
| 264 | 2 | } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') { |
|
| 265 | return true; |
||
| 266 | } |
||
| 267 | |||
| 268 | 2 | return false; |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Base URL |
||
| 273 | * |
||
| 274 | * Returns base url |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 1 | public function getBaseUrl() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Returns Client Ip Address |
||
| 292 | * |
||
| 293 | * @param int $type |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | 1 | public function getClientIp($type = 0) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Returns Http Host |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | 1 | public function getHostName() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Detect whether request method is GET |
||
| 364 | * |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | public function isGet() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Detect whether request method is POST |
||
| 374 | * |
||
| 375 | * @return boolean |
||
| 376 | */ |
||
| 377 | public function isPost() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Detect whether request method is PUT |
||
| 384 | * |
||
| 385 | * @return boolean |
||
| 386 | */ |
||
| 387 | public function isPut() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Detect whether request method is AJAX |
||
| 394 | * |
||
| 395 | * @return boolean |
||
| 396 | */ |
||
| 397 | public function isAjax() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Is CLI? |
||
| 404 | * |
||
| 405 | * Test to see if a request was made from the command line. |
||
| 406 | * |
||
| 407 | * @return boolean |
||
| 408 | */ |
||
| 409 | 3 | public function isCli() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Detect whether user agent is Mobile |
||
| 416 | * |
||
| 417 | * @return boolean |
||
| 418 | */ |
||
| 419 | 1 | public function isMobile() |
|
| 428 | } |
||
| 429 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.