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 |
||
| 24 | class Request |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Request data types to be treated case-insensitively. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected static $caseInsensitive = array('server', 'header'); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The request data. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $data = array( |
||
| 39 | 'get' => array(), |
||
| 40 | 'post' => array(), |
||
| 41 | 'cookie' => array(), |
||
| 42 | 'file' => array(), |
||
| 43 | 'server' => array(), |
||
| 44 | 'header' => array(), |
||
| 45 | 'session' => null |
||
| 46 | ); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Request body content. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $content; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The session that belongs to this request. |
||
| 57 | * |
||
| 58 | * @var \Darya\Http\Session |
||
| 59 | */ |
||
| 60 | protected $session; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The router that matched this request. |
||
| 64 | * |
||
| 65 | * @var \Darya\Routing\Router |
||
| 66 | */ |
||
| 67 | public $router; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The route that this request was matched with. |
||
| 71 | * |
||
| 72 | * @var \Darya\Routing\Route |
||
| 73 | */ |
||
| 74 | public $route; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Determine whether the given data type's keys can be treated |
||
| 78 | * case-insensitively. |
||
| 79 | * |
||
| 80 | * @param string $type |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | protected static function isCaseInsensitive($type) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Prepare the given request data where necessary. |
||
| 90 | * |
||
| 91 | * Lowercases data type keys and the keys of `server` and `header` data so |
||
| 92 | * they can be treated case-insensitively. |
||
| 93 | * |
||
| 94 | * Any expected data types not satisfied will contain an empty array apart |
||
| 95 | * from `session`, which will be null. |
||
| 96 | * |
||
| 97 | * @param array $data |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | protected static function prepareData(array $data) |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Parse the given URI and return its components. |
||
| 124 | * |
||
| 125 | * Any components not satisfied will be null instead of non-existent, so you |
||
| 126 | * can safely expect the keys 'scheme', 'host', 'port', 'user', 'pass', |
||
| 127 | * 'query' and 'fragment' to exist. |
||
| 128 | * |
||
| 129 | * @param string $url |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | protected static function parseUrl($url) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Parse the given query string and return its key value pairs. |
||
| 150 | * |
||
| 151 | * @param string $query |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | protected static function parseQuery($query) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Create a new request with the given URL, method and data. |
||
| 164 | * |
||
| 165 | * @param string $url |
||
| 166 | * @param string $method [optional] |
||
| 167 | * @param array $data [optional] |
||
| 168 | * @param Session $session [optional] |
||
| 169 | * @return Request |
||
| 170 | */ |
||
| 171 | public static function create($url, $method = 'GET', $data = array(), Session $session = null) |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Extract HTTP request headers from a given set of $_SERVER globals. |
||
| 214 | * |
||
| 215 | * @param array $server |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public static function headersFromGlobals(array $server) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Create a new request using PHP's super globals. |
||
| 236 | * |
||
| 237 | * @param \Darya\Http\Session $session [optional] |
||
| 238 | * @return \Darya\Http\Request |
||
| 239 | */ |
||
| 240 | public static function createFromGlobals(Session $session = null) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Instantiate a new request with the given data. |
||
| 256 | * |
||
| 257 | * Expects request data in the same format as PHP superglobals. |
||
| 258 | * |
||
| 259 | * @param array $get |
||
| 260 | * @param array $post |
||
| 261 | * @param array $cookie |
||
| 262 | * @param array $file |
||
| 263 | * @param array $server |
||
| 264 | * @param array $header |
||
| 265 | */ |
||
| 266 | public function __construct(array $get, array $post, array $cookie, array $file, array $server, array $header) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Determine whether this Request has a session interface. |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | public function hasSession() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Set the session interface for the request. Starts the session if it |
||
| 283 | * hasn't been already. |
||
| 284 | * |
||
| 285 | * @param \Darya\Http\Session $session |
||
| 286 | */ |
||
| 287 | public function setSession(Session $session = null) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Retrieve request data of the given type using the given key. |
||
| 299 | * |
||
| 300 | * If $key is not set, all request data of the given type will be returned. |
||
| 301 | * |
||
| 302 | * If neither $type or $key are set, all request data will be returned. |
||
| 303 | * |
||
| 304 | * If a $default value is given along with $key, it is returned if $key is |
||
| 305 | * not set in the data of the given $type. |
||
| 306 | * |
||
| 307 | * @param string $type [optional] |
||
| 308 | * @param string $key [optional] |
||
| 309 | * @param mixed $default [optional] |
||
| 310 | * @return mixed |
||
| 311 | */ |
||
| 312 | public function data($type = null, $key = null, $default = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Dynamically retrieve all request data of the given type. |
||
| 333 | * |
||
| 334 | * @param string $property |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | public function __get($property) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Dynamically retrieve request data. |
||
| 344 | * |
||
| 345 | * @param string $method |
||
| 346 | * @param array $arguments |
||
| 347 | */ |
||
| 348 | public function __call($method, $arguments) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Determine whether a given parameter is set in the request's post or get |
||
| 357 | * data. |
||
| 358 | * |
||
| 359 | * @param string $key |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | public function has($key) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Retrieve a parameter from either the post or get data of the request, |
||
| 369 | * checking post data if the request method is post. |
||
| 370 | * |
||
| 371 | * @param string $key |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | public function any($key = null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Retrieve the URI of the request. |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function uri() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Retrieve the hostname of the request. |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function host() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Retrieve the path of the request. |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | public function path() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Retrieve the method of the request or determine whether the method of the |
||
| 419 | * request is the same as the one given. |
||
| 420 | * |
||
| 421 | * @param string $method [optional] |
||
| 422 | * @return string|bool |
||
| 423 | */ |
||
| 424 | public function method($method = null) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Retrieve the request body content. |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | public function content() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Retrieve the IP address of the client that made the request. |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function ip() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Determine whether this is an ajax Request. This is determined by 'get' or |
||
| 458 | * 'post' data having an ajax parameter set or the 'X-Requested-With' |
||
| 459 | * parameter having the 'XMLHttpRequest' value. |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | public function ajax() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Flash data with the given key to the session. |
||
| 472 | * |
||
| 473 | * @param string $key Flash data key |
||
| 474 | * @param string|array $values A single value or set of values to add |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | public function flash($key, $values) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Retrieve and clear flashed data with the given key from the session. If |
||
| 498 | * no key is given, all data is retrieved and cleared. |
||
| 499 | * |
||
| 500 | * Returns an empty array if this request has no session or flash variables |
||
| 501 | * were not found with the given key. |
||
| 502 | * |
||
| 503 | * @param string $key [optional] Flash data key |
||
| 504 | * @return array |
||
| 505 | */ |
||
| 506 | public function flashes($key = null) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Transforms post request data of the form entity[property][n] to the form |
||
| 531 | * entity[n][property]. |
||
| 532 | * |
||
| 533 | * @param string $key Entity key (post parameter name) |
||
| 534 | * @return array |
||
| 535 | */ |
||
| 536 | public function postObjectData($key = null) |
||
| 551 | } |
||
| 552 |