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 |
||
| 38 | class Request |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Ip address |
||
| 42 | * |
||
| 43 | * @var mixed |
||
| 44 | */ |
||
| 45 | protected $ip = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Http headers |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $headers = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Session has been started or not |
||
| 56 | * |
||
| 57 | * @var boolean |
||
| 58 | */ |
||
| 59 | protected $hasSessionStarted = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Class constructor |
||
| 63 | * |
||
| 64 | * Initialize Request. |
||
| 65 | */ |
||
| 66 | public function __construct() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Internal method used to retrieve values from given arrays. |
||
| 73 | * |
||
| 74 | * @param array $source |
||
| 75 | * @param mixed $key |
||
| 76 | * @param mixed $default |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | protected function getRequestParams(&$source, $key = null, $default = null) |
||
| 80 | { |
||
| 81 | // If $key is NULL, it means that the whole $source is requested |
||
| 82 | if (!isset($key) || $key == null) { |
||
| 83 | $key = array_keys($source); |
||
| 84 | } |
||
| 85 | |||
| 86 | if (is_array($key)) { |
||
| 87 | $output = []; |
||
| 88 | foreach ($key as $k) { |
||
| 89 | $output[$k] = $this->getRequestParams($source, $k); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $output; |
||
| 93 | } |
||
| 94 | |||
| 95 | if (isset($source[$key])) { |
||
| 96 | $value = $source[$key]; |
||
| 97 | } else { |
||
| 98 | return $default; |
||
| 99 | } |
||
| 100 | |||
| 101 | return $value; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Fetch an item from the GET array |
||
| 106 | * |
||
| 107 | * @param mixed $key |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | public function get($key = null, $default = null) |
||
|
|
|||
| 111 | { |
||
| 112 | return $this->getRequestParams($_GET, $key, $default); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Fetch an item from the POST array |
||
| 117 | * |
||
| 118 | * @param mixed $key |
||
| 119 | * @param mixed $default |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | public function post($key = null, $default = null) |
||
| 123 | { |
||
| 124 | $rawPostData = file_get_contents('php://input'); |
||
| 125 | $source = json_decode($rawPostData, true); |
||
| 126 | if (json_last_error() != JSON_ERROR_NONE) { |
||
| 127 | $source = $_POST; |
||
| 128 | } |
||
| 129 | |||
| 130 | return $this->getRequestParams($source, $key, $default); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Fetch an item from the SERVER array |
||
| 135 | * |
||
| 136 | * @param mixed $key |
||
| 137 | * @param mixed $default |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | public function server($key = null, $default = null) |
||
| 141 | { |
||
| 142 | return $this->getRequestParams($_SERVER, $key, $default); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set or Get cookie |
||
| 147 | * |
||
| 148 | * @param mixed $key |
||
| 149 | * @param string $value |
||
| 150 | * @param mixed $options |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | 2 | public function cookie($key = '', $value = '', $options = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Session Initialize |
||
| 232 | * |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | public function sessionInit() |
||
| 236 | { |
||
| 237 | ini_set('session.auto_start', 0); |
||
| 238 | $config = Container::get('config')->get('session'); |
||
| 239 | if ($config['adapter'] != '') { |
||
| 240 | $class = '\\Kotori\\Http\\Session\\' . ucfirst($config['adapter']); |
||
| 241 | if (!class_exists($class) || !session_set_save_handler(new $class($config))) { |
||
| 242 | throw new NotFoundException('error session handler:' . $class); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | session_name('KOTORI_SESSID'); |
||
| 247 | if (isset($config['auto_start']) && $config['auto_start']) { |
||
| 248 | session_start(); |
||
| 249 | $this->hasSessionStarted = true; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Start a Session |
||
| 255 | * |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | 2 | public function sessionStart() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Set or Get session |
||
| 271 | * |
||
| 272 | * @param mixed $key |
||
| 273 | * @param string $value |
||
| 274 | * @return mixed |
||
| 275 | */ |
||
| 276 | 2 | public function session($key = '', $value = '') |
|
| 277 | { |
||
| 278 | 2 | $this->sessionStart(); |
|
| 279 | |||
| 280 | 2 | if (is_null($key)) { |
|
| 281 | if (empty($_SESSION)) { |
||
| 282 | return null; |
||
| 283 | } |
||
| 284 | |||
| 285 | unset($_SESSION); |
||
| 286 | 2 | } elseif ('' === $key) { |
|
| 287 | // Get All Session |
||
| 288 | return $_SESSION; |
||
| 289 | } |
||
| 290 | |||
| 291 | 2 | if ('' === $value) { |
|
| 292 | 2 | return isset($_SESSION[$key]) ? $_SESSION[$key] : null; |
|
| 293 | } else { |
||
| 294 | 2 | if (is_null($value)) { |
|
| 295 | 1 | unset($_SESSION[$key]); |
|
| 296 | } else { |
||
| 297 | 2 | $_SESSION[$key] = $value; |
|
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | 2 | return null; |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Is HTTPS? |
||
| 306 | * |
||
| 307 | * Determines if the application is accessed via an encrypted |
||
| 308 | * (HTTPS) connection. |
||
| 309 | * |
||
| 310 | * @return boolean |
||
| 311 | */ |
||
| 312 | 2 | public function isSecure() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Base URL |
||
| 327 | * |
||
| 328 | * Returns base url |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | 1 | public function getBaseUrl() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Returns Client Ip Address |
||
| 346 | * |
||
| 347 | * @param int $type |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 1 | public function getClientIp($type = 0) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Returns Http Host |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | 1 | public function getHostName() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Return specified header |
||
| 418 | * |
||
| 419 | * @param string $name |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | 1 | public function getHeader($name) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Detect whether request method is GET |
||
| 454 | * |
||
| 455 | * @return boolean |
||
| 456 | */ |
||
| 457 | public function isGet() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Detect whether request method is POST |
||
| 464 | * |
||
| 465 | * @return boolean |
||
| 466 | */ |
||
| 467 | public function isPost() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Detect whether request method is PUT |
||
| 474 | * |
||
| 475 | * @return boolean |
||
| 476 | */ |
||
| 477 | public function isPut() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Detect whether request method is PATCH |
||
| 484 | * |
||
| 485 | * @return boolean |
||
| 486 | */ |
||
| 487 | public function isPatch() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Detect whether request method is DELETE |
||
| 494 | * |
||
| 495 | * @return boolean |
||
| 496 | */ |
||
| 497 | public function isDelete() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Detect whether request method is OPTIONS |
||
| 504 | * |
||
| 505 | * @return boolean |
||
| 506 | */ |
||
| 507 | public function isOptions() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Detect whether request method is AJAX |
||
| 514 | * |
||
| 515 | * @return boolean |
||
| 516 | */ |
||
| 517 | public function isAjax() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Is CLI? |
||
| 524 | * |
||
| 525 | * Test to see if a request was made from the command line. |
||
| 526 | * |
||
| 527 | * @return boolean |
||
| 528 | */ |
||
| 529 | 4 | public function isCli() |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Detect whether user agent is Mobile |
||
| 536 | * |
||
| 537 | * @return boolean |
||
| 538 | */ |
||
| 539 | 1 | public function isMobile() |
|
| 548 | } |
||
| 549 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: