Complex classes like AbstractResponse 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 AbstractResponse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class AbstractResponse |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Properties |
||
| 28 | */ |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The default response HTTP status code |
||
| 32 | * |
||
| 33 | * @type int |
||
| 34 | */ |
||
| 35 | protected static $default_status_code = 200; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The HTTP version of the response |
||
| 39 | * |
||
| 40 | * @type string |
||
| 41 | */ |
||
| 42 | protected $protocol_version = '1.1'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The response body |
||
| 46 | * |
||
| 47 | * @type string |
||
| 48 | */ |
||
| 49 | protected $body; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * HTTP response status |
||
| 53 | * |
||
| 54 | * @type HttpStatus |
||
| 55 | */ |
||
| 56 | protected $status; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * HTTP response headers |
||
| 60 | * |
||
| 61 | * @type HeaderDataCollection |
||
| 62 | */ |
||
| 63 | protected $headers; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * HTTP response cookies |
||
| 67 | * |
||
| 68 | * @type ResponseCookieDataCollection |
||
| 69 | */ |
||
| 70 | protected $cookies; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Whether or not the response is "locked" from |
||
| 74 | * any further modification |
||
| 75 | * |
||
| 76 | * @type boolean |
||
| 77 | */ |
||
| 78 | protected $locked = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Whether or not the response has been sent |
||
| 82 | * |
||
| 83 | * @type boolean |
||
| 84 | */ |
||
| 85 | protected $sent = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Whether the response has been chunked or not |
||
| 89 | * |
||
| 90 | * @type boolean |
||
| 91 | */ |
||
| 92 | public $chunked = false; |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Methods |
||
| 97 | */ |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Constructor |
||
| 101 | * |
||
| 102 | * Create a new AbstractResponse object with a dependency injected Headers instance |
||
| 103 | * |
||
| 104 | * @param string $body The response body's content |
||
| 105 | * @param int $status_code The status code |
||
| 106 | * @param array $headers The response header "hash" |
||
| 107 | */ |
||
| 108 | public function __construct($body = '', $status_code = null, array $headers = array()) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get (or set) the HTTP protocol version |
||
| 122 | * |
||
| 123 | * Simply calling this method without any arguments returns the current protocol version. |
||
| 124 | * Calling with an integer argument, however, attempts to set the protocol version to what |
||
| 125 | * was provided by the argument. |
||
| 126 | * |
||
| 127 | * @param string $protocol_version |
||
| 128 | * @return string|AbstractResponse |
||
| 129 | */ |
||
| 130 | public function protocolVersion($protocol_version = null) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get (or set) the response's body content |
||
| 146 | * |
||
| 147 | * Simply calling this method without any arguments returns the current response body. |
||
| 148 | * Calling with an argument, however, sets the response body to what was provided by the argument. |
||
| 149 | * |
||
| 150 | * @param string $body The body content string |
||
| 151 | * @return string|AbstractResponse |
||
| 152 | */ |
||
| 153 | public function body($body = null) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the status object |
||
| 169 | * |
||
| 170 | * @return \Klein\HttpStatus |
||
| 171 | */ |
||
| 172 | public function status() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Returns the headers collection |
||
| 179 | * |
||
| 180 | * @return HeaderDataCollection |
||
| 181 | */ |
||
| 182 | public function headers() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the cookies collection |
||
| 189 | * |
||
| 190 | * @return ResponseCookieDataCollection |
||
| 191 | */ |
||
| 192 | public function cookies() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get (or set) the HTTP response code |
||
| 199 | * |
||
| 200 | * Simply calling this method without any arguments returns the current response code. |
||
| 201 | * Calling with an integer argument, however, attempts to set the response code to what |
||
| 202 | * was provided by the argument. |
||
| 203 | * |
||
| 204 | * @param int $code The HTTP status code to send |
||
| 205 | * @return int|AbstractResponse |
||
| 206 | */ |
||
| 207 | public function code($code = null) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Prepend a string to the response's content body |
||
| 223 | * |
||
| 224 | * @param string $content The string to prepend |
||
| 225 | * @return AbstractResponse |
||
| 226 | */ |
||
| 227 | public function prepend($content) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Append a string to the response's content body |
||
| 239 | * |
||
| 240 | * @param string $content The string to append |
||
| 241 | * @return AbstractResponse |
||
| 242 | */ |
||
| 243 | public function append($content) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Check if the response is locked |
||
| 255 | * |
||
| 256 | * @return boolean |
||
| 257 | */ |
||
| 258 | public function isLocked() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Require that the response is unlocked |
||
| 265 | * |
||
| 266 | * Throws an exception if the response is locked, |
||
| 267 | * preventing any methods from mutating the response |
||
| 268 | * when its locked |
||
| 269 | * |
||
| 270 | * @throws LockedResponseException If the response is locked |
||
| 271 | * @return AbstractResponse |
||
| 272 | */ |
||
| 273 | public function requireUnlocked() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Lock the response from further modification |
||
| 284 | * |
||
| 285 | * @return AbstractResponse |
||
| 286 | */ |
||
| 287 | public function lock() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Unlock the response from further modification |
||
| 296 | * |
||
| 297 | * @return AbstractResponse |
||
| 298 | */ |
||
| 299 | public function unlock() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Generates an HTTP compatible status header line string |
||
| 308 | * |
||
| 309 | * Creates the string based off of the response's properties |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | protected function httpStatusLine() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Send our HTTP headers |
||
| 320 | * |
||
| 321 | * @param boolean $cookies_also Whether or not to also send the cookies after sending the normal headers |
||
| 322 | * @param boolean $override Whether or not to override the check if headers have already been sent |
||
| 323 | * @return AbstractResponse |
||
| 324 | */ |
||
| 325 | public function sendHeaders($cookies_also = true, $override = false) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Send our HTTP response cookies |
||
| 348 | * |
||
| 349 | * @param boolean $override Whether or not to override the check if headers have already been sent |
||
| 350 | * @return AbstractResponse |
||
| 351 | */ |
||
| 352 | public function sendCookies($override = false) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Send our body's contents |
||
| 377 | * |
||
| 378 | * @return AbstractResponse |
||
| 379 | */ |
||
| 380 | public function sendBody() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Send the response and lock it |
||
| 389 | * |
||
| 390 | * @param boolean $override Whether or not to override the check if the response has already been sent |
||
| 391 | * @throws ResponseAlreadySentException If the response has already been sent |
||
| 392 | * @return AbstractResponse |
||
| 393 | */ |
||
| 394 | public function send($override = false) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Check if the response has been sent |
||
| 420 | * |
||
| 421 | * @return boolean |
||
| 422 | */ |
||
| 423 | public function isSent() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Enable response chunking |
||
| 430 | * |
||
| 431 | * @link https://github.com/chriso/klein.php/wiki/Response-Chunking |
||
| 432 | * @link http://bit.ly/hg3gHb |
||
| 433 | * @return AbstractResponse |
||
| 434 | */ |
||
| 435 | public function chunk() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Sets a response header |
||
| 456 | * |
||
| 457 | * @param string $key The name of the HTTP response header |
||
| 458 | * @param mixed $value The value to set the header with |
||
| 459 | * @return AbstractResponse |
||
| 460 | */ |
||
| 461 | public function header($key, $value) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Sets a response cookie |
||
| 470 | * |
||
| 471 | * @param string $key The name of the cookie |
||
| 472 | * @param string $value The value to set the cookie with |
||
| 473 | * @param int $expiry The time that the cookie should expire |
||
| 474 | * @param string $path The path of which to restrict the cookie |
||
| 475 | * @param string $domain The domain of which to restrict the cookie |
||
| 476 | * @param boolean $secure Flag of whether the cookie should only be sent over a HTTPS connection |
||
| 477 | * @param boolean $httponly Flag of whether the cookie should only be accessible over the HTTP protocol |
||
| 478 | * @return AbstractResponse |
||
| 479 | */ |
||
| 480 | public function cookie( |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Tell the browser not to cache the response |
||
| 503 | * |
||
| 504 | * @return AbstractResponse |
||
| 505 | */ |
||
| 506 | public function noCache() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Redirects the request to another URL |
||
| 516 | * |
||
| 517 | * @param string $url The URL to redirect to |
||
| 518 | * @param int $code The HTTP status code to use for redirection |
||
| 519 | * @return AbstractResponse |
||
| 520 | */ |
||
| 521 | public function redirect($url, $code = 302) |
||
| 529 | } |
||
| 530 |