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 | * @var array Request data types to treat case-insensitively |
||
| 28 | */ |
||
| 29 | protected static $caseInsensitive = array('server', 'header'); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array Request data |
||
| 33 | */ |
||
| 34 | protected $data = array( |
||
| 35 | 'get' => array(), |
||
| 36 | 'post' => array(), |
||
| 37 | 'cookie' => array(), |
||
| 38 | 'file' => array(), |
||
| 39 | 'server' => array(), |
||
| 40 | 'header' => array(), |
||
| 41 | 'session' => null |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string Request body content |
||
| 46 | */ |
||
| 47 | protected $content; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \Darya\Http\Session |
||
| 51 | */ |
||
| 52 | protected $session; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \Darya\Routing\Router Router that matched this request |
||
| 56 | */ |
||
| 57 | public $router; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \Darya\Routing\Route Route that this request was matched with |
||
| 61 | */ |
||
| 62 | public $route; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Determine whether the given data type's keys can be treated |
||
| 66 | * case-insensitively. |
||
| 67 | * |
||
| 68 | * @param string $type |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | protected static function isCaseInsensitive($type) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Prepare the given request data where necessary. |
||
| 77 | * |
||
| 78 | * Lowercases data type keys and the keys of `server` and `header` data so |
||
| 79 | * they can be treated case-insensitively. |
||
| 80 | * |
||
| 81 | * Any expected data types not satisfied will contain an empty array apart |
||
| 82 | * from `session`, which will be null. |
||
| 83 | * |
||
| 84 | * @param array $data |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | protected static function prepareData(array $data) { |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Parse the given URI and return its components. |
||
| 110 | * |
||
| 111 | * Any components not satisfied will be null instead of non-existent, so you |
||
| 112 | * can safely expect the keys 'scheme', 'host', 'port', 'user', 'pass', |
||
| 113 | * 'query' and 'fragment' to exist. |
||
| 114 | * |
||
| 115 | * @param string $url |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | protected static function parseUrl($url) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Parse the given query string and return its key value pairs. |
||
| 135 | * |
||
| 136 | * @param string $query |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | protected static function parseQuery($query) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Create a new request with the given URL, method and data. |
||
| 148 | * |
||
| 149 | * @param string $url |
||
| 150 | * @param string $method [optional] |
||
| 151 | * @param array $data [optional] |
||
| 152 | * @param Session $session [optional] |
||
| 153 | * @return Request |
||
| 154 | */ |
||
| 155 | public static function create($url, $method = 'GET', $data = array(), Session $session = null) { |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Extract HTTP request headers from a given set of $_SERVER globals. |
||
| 197 | * |
||
| 198 | * @param array $server |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public static function headersFromGlobals(array $server) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Create a new request using PHP's super globals. |
||
| 218 | * |
||
| 219 | * @param \Darya\Http\Session $session [optional] |
||
| 220 | * @return \Darya\Http\Request |
||
| 221 | */ |
||
| 222 | public static function createFromGlobals(Session $session = null) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Instantiate a new request with the given data. |
||
| 237 | * |
||
| 238 | * Expects request data in the same format as PHP superglobals. |
||
| 239 | * |
||
| 240 | * @param array $get |
||
| 241 | * @param array $post |
||
| 242 | * @param array $cookie |
||
| 243 | * @param array $file |
||
| 244 | * @param array $server |
||
| 245 | * @param array $header |
||
| 246 | */ |
||
| 247 | public function __construct(array $get, array $post, array $cookie, array $file, array $server, array $header) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Determine whether this Request has a session interface. |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function hasSession() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set the session interface for the request. Starts the session if it |
||
| 262 | * hasn't been already. |
||
| 263 | * |
||
| 264 | * @param \Darya\Http\Session $session |
||
| 265 | */ |
||
| 266 | public function setSession(Session $session = null) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Retrieve request data of the given type using the given key. |
||
| 277 | * |
||
| 278 | * If $key is not set, all request data of the given type will be returned. |
||
| 279 | * If neither $type or $key are set, all request data will be returned. |
||
| 280 | * |
||
| 281 | * @param string $type [optional] |
||
| 282 | * @param string $key [optional] |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | public function data($type = null, $key = null) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Magic method implementation that provides read-only array access to |
||
| 305 | * request data. |
||
| 306 | * |
||
| 307 | * @param string $property |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function __get($property) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Magic method implementation that provides read-only functional access |
||
| 316 | * to request data. |
||
| 317 | * |
||
| 318 | * @param string $method |
||
| 319 | * @param array $args |
||
| 320 | */ |
||
| 321 | public function __call($method, $args) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Determine whether a given parameter is set in the request's post or get |
||
| 327 | * data. |
||
| 328 | * |
||
| 329 | * @param string $key |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public function has($key) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Retrieve a parameter from either the post or get data of the request, |
||
| 338 | * checking post data if the request method is post. |
||
| 339 | * |
||
| 340 | * @param string $key |
||
| 341 | * @return mixed |
||
| 342 | */ |
||
| 343 | public function any($key = null) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Retrieve the URI of the request. |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public function uri() { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Retrieve the hostname of the request. |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function host() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Retrieve the path of the request. |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function path() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Retrieve the method of the request or determine whether the method of the |
||
| 384 | * request is the same as the one given. |
||
| 385 | * |
||
| 386 | * @param string $method [optional] |
||
| 387 | * @return string|bool |
||
| 388 | */ |
||
| 389 | public function method($method = null) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Retrieve the request body content. |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function content() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Retrieve the IP address of the client that made the request. |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function ip() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Determine whether this is an ajax Request. This is determined by 'get' or |
||
| 420 | * 'post' data having an ajax parameter set or the 'X-Requested-With' |
||
| 421 | * parameter having the 'XMLHttpRequest' value. |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | public function ajax() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Flash data with the given key to the session. |
||
| 433 | * |
||
| 434 | * @param string $key Flash data key |
||
| 435 | * @param string|array $values A single value or set of values to add |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | public function flash($key, $values) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Retrieve and clear flashed data with the given key from the session. If |
||
| 458 | * no key is given, all data is retrieved and cleared. |
||
| 459 | * |
||
| 460 | * Returns an empty array if this request has no session or flash variables |
||
| 461 | * were not found with the given key. |
||
| 462 | * |
||
| 463 | * @param string $key [optional] Flash data key |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | public function flashes($key = null) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Transforms post request data of the form entity[property][n] to the form |
||
| 490 | * entity[n][property]. |
||
| 491 | * |
||
| 492 | * @param string $key Entity key (post parameter name) |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | public function postObjectData($key = null) { |
||
| 509 | |||
| 510 | } |
||
| 511 |