Complex classes like Response 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 Response, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Response extends AbstractResponse |
||
| 28 | { |
||
| 29 | use HttpStatusTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @const string |
||
| 33 | */ |
||
| 34 | const HTTP_VERSION_10 = '1.0'; |
||
| 35 | const HTTP_VERSION_11 = '1.1'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var SetCookieHeaderBag |
||
| 39 | */ |
||
| 40 | public $cookies; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $chunkLength; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | protected $chunkDelayResponse; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor. |
||
| 54 | * |
||
| 55 | * @param string $content |
||
| 56 | * @param int $statusCode |
||
| 57 | * @param array $headers |
||
| 58 | * @param array $cookies |
||
| 59 | */ |
||
| 60 | public function __construct($content = '', $statusCode = 200, array $headers = array(), array $cookies = array()) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Prepare Response base on Request. |
||
| 71 | * |
||
| 72 | * @param Request $request |
||
| 73 | * |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | public function prepare(Request $request) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set response as chunked. |
||
| 99 | * |
||
| 100 | * @param int $length Content split length. |
||
| 101 | * @param int $delay Delay response in microseconds. |
||
| 102 | * |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function setChunkedTransferEncoding($length = 76, $delay = 1000000) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set response as not modified. |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function setNotModified() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Check if response is no cache. |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function isNoCache() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Send response to client. |
||
| 158 | * |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | public function send() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Send response header. |
||
| 185 | * |
||
| 186 | * @return $this |
||
| 187 | */ |
||
| 188 | public function sendHeaders() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Check if current response is chunked. |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function isChunked() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Cast Response as string representation. |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function __toString() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Add charset if content type exist. |
||
| 240 | */ |
||
| 241 | protected function computeContentType() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get computed content with specific encoding. |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | protected function computeContent() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get calculated cache control header. |
||
| 286 | * |
||
| 287 | * @return CacheControlHeader |
||
| 288 | */ |
||
| 289 | protected function computeCacheControl() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Add content type header from request if not defined. |
||
| 309 | * |
||
| 310 | * @param Request $request |
||
| 311 | */ |
||
| 312 | private function fixContentType(Request $request) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Fix content length header. |
||
| 327 | * |
||
| 328 | * @param Request $request |
||
| 329 | */ |
||
| 330 | private function fixContentLength(Request $request) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Send extra expire info if needed. |
||
| 347 | */ |
||
| 348 | private function fixExpire() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Fix http encoding header. |
||
| 364 | */ |
||
| 365 | private function fixEncoding() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Send http headers. |
||
| 379 | */ |
||
| 380 | private function createHeaders() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Send http cookie headers. |
||
| 391 | */ |
||
| 392 | private function createCookieHeaders() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Send chunk content. |
||
| 401 | * |
||
| 402 | * @param string $content |
||
| 403 | */ |
||
| 404 | private function sendChunkContent($content) |
||
| 417 | } |
||
| 418 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: