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 |
||
| 33 | class Response |
||
| 34 | { |
||
| 35 | use Options; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string HTTP protocol version |
||
| 39 | */ |
||
| 40 | protected $protocol = '1.1'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var integer response code equal to HTTP status codes |
||
| 44 | */ |
||
| 45 | protected $code = StatusCode::OK; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string|null HTTP Phrase |
||
| 49 | */ |
||
| 50 | protected $phrase; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array list of headers |
||
| 54 | */ |
||
| 55 | protected $headers = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array list of cookies |
||
| 59 | */ |
||
| 60 | protected $cookies = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var Controller |
||
| 64 | */ |
||
| 65 | protected $body; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string CLI|HTML|JSON|FILE |
||
| 69 | */ |
||
| 70 | protected $type = 'HTML'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * send |
||
| 74 | * |
||
| 75 | * @throws NotAcceptableException |
||
| 76 | */ |
||
| 77 | public function send() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set Response Type, one of JSON, HTML or CLI |
||
| 142 | * |
||
| 143 | * @param $type |
||
| 144 | */ |
||
| 145 | public function switchType($type) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Gets the HTTP protocol version as a string |
||
| 164 | * |
||
| 165 | * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
||
| 166 | * |
||
| 167 | * @return string HTTP protocol version. |
||
| 168 | */ |
||
| 169 | 1 | public function getProtocolVersion() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the response Status-Code |
||
| 176 | * |
||
| 177 | * The Status-Code is a 3-digit integer result code of the server's attempt |
||
| 178 | * to understand and satisfy the request. |
||
| 179 | * |
||
| 180 | * @return integer status code. |
||
| 181 | */ |
||
| 182 | 4 | public function getStatusCode() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Sets the status code of this response |
||
| 189 | * |
||
| 190 | * @param integer $code the 3-digit integer result code to set. |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | 5 | public function setStatusCode($code) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Gets the response Reason-Phrase, a short textual description of the Status-Code |
||
| 200 | * |
||
| 201 | * Because a Reason-Phrase is not a required element in response |
||
| 202 | * Status-Line, the Reason-Phrase value MAY be null. Implementations MAY |
||
| 203 | * choose to return the default RFC 2616 recommended reason phrase for the |
||
| 204 | * response's Status-Code. |
||
| 205 | * |
||
| 206 | * @return string|null reason phrase, or null if unknown. |
||
| 207 | */ |
||
| 208 | 2 | public function getReasonPhrase() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Sets the Reason-Phrase of the response |
||
| 215 | * |
||
| 216 | * If no Reason-Phrase is specified, implementations MAY choose to default |
||
| 217 | * to the RFC 2616 recommended reason phrase for the response's Status-Code. |
||
| 218 | * |
||
| 219 | * @param string $phrase the Reason-Phrase to set. |
||
| 220 | */ |
||
| 221 | 1 | public function setReasonPhrase($phrase) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Retrieve a header by the given case-insensitive name as a string |
||
| 228 | * |
||
| 229 | * This method returns all of the header values of the given |
||
| 230 | * case-insensitive header name as a string concatenated together using |
||
| 231 | * a comma. |
||
| 232 | * |
||
| 233 | * @param string $header case-insensitive header name. |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | 4 | public function getHeader($header) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Retrieves a header by the given case-insensitive name as an array of strings |
||
| 247 | * |
||
| 248 | * @param string $header Case-insensitive header name. |
||
| 249 | * @return string[] |
||
| 250 | */ |
||
| 251 | 1 | public function getHeaderAsArray($header) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Checks if a header exists by the given case-insensitive name |
||
| 262 | * |
||
| 263 | * @param string $header case-insensitive header name. |
||
| 264 | * @return bool returns true if any header names match the given header |
||
| 265 | * name using a case-insensitive string comparison. Returns false if |
||
| 266 | * no matching header name is found in the message. |
||
| 267 | */ |
||
| 268 | 3 | public function hasHeader($header) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Sets a header, replacing any existing values of any headers with the |
||
| 275 | * same case-insensitive name |
||
| 276 | * |
||
| 277 | * The header name is case-insensitive. The header values MUST be a string |
||
| 278 | * or an array of strings. |
||
| 279 | * |
||
| 280 | * @param string $header header name |
||
| 281 | * @param string|string[] $value header value(s) |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | 3 | public function setHeader($header, $value) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Appends a header value for the specified header |
||
| 291 | * |
||
| 292 | * Existing values for the specified header will be maintained. The new |
||
| 293 | * value will be appended to the existing list. |
||
| 294 | * |
||
| 295 | * @param string $header header name to add |
||
| 296 | * @param string $value value of the header |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | 1 | public function addHeader($header, $value) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Remove a specific header by case-insensitive name. |
||
| 310 | * |
||
| 311 | * @param string $header HTTP header to remove |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | 1 | public function removeHeader($header) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Gets all message headers |
||
| 321 | * |
||
| 322 | * The keys represent the header name as it will be sent over the wire, and |
||
| 323 | * each value is an array of strings associated with the header. |
||
| 324 | * |
||
| 325 | * // Represent the headers as a string |
||
| 326 | * foreach ($message->getHeaders() as $name => $values) { |
||
| 327 | * echo $name . ": " . implode(", ", $values); |
||
| 328 | * } |
||
| 329 | * |
||
| 330 | * @return array returns an associative array of the message's headers. |
||
| 331 | */ |
||
| 332 | 1 | public function getHeaders() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Sets headers, replacing any headers that have already been set on the message |
||
| 339 | * |
||
| 340 | * The array keys MUST be a string. The array values must be either a |
||
| 341 | * string or an array of strings. |
||
| 342 | * |
||
| 343 | * @param array $headers Headers to set. |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | 1 | public function setHeaders(array $headers) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Merges in an associative array of headers. |
||
| 353 | * |
||
| 354 | * Each array key MUST be a string representing the case-insensitive name |
||
| 355 | * of a header. Each value MUST be either a string or an array of strings. |
||
| 356 | * For each value, the value is appended to any existing header of the same |
||
| 357 | * name, or, if a header does not already exist by the given name, then the |
||
| 358 | * header is added. |
||
| 359 | * |
||
| 360 | * @param array $headers Associative array of headers to add to the message |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | 1 | public function addHeaders(array $headers) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Remove all headers |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | 6 | public function removeHeaders() |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Set response body |
||
| 380 | * |
||
| 381 | * @param mixed $body |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | 6 | public function setBody($body) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Get response body |
||
| 391 | * |
||
| 392 | * @return Controller|Layout |
||
| 393 | */ |
||
| 394 | 2 | public function getBody() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Clear response body |
||
| 401 | * |
||
| 402 | * @return void |
||
| 403 | */ |
||
| 404 | 5 | public function clearBody() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Set Cookie |
||
| 411 | * |
||
| 412 | * @param string $name |
||
| 413 | * @param string $value |
||
| 414 | * @param int|string|\DateTime $expire |
||
| 415 | * @param string $path |
||
| 416 | * @param string $domain |
||
| 417 | * @param bool $secure |
||
| 418 | * @param bool $httpOnly |
||
| 419 | * @return void |
||
| 420 | */ |
||
| 421 | 5 | public function setCookie( |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Get Cookie by name |
||
| 462 | * |
||
| 463 | * @param string $name |
||
| 464 | * @return array|null |
||
| 465 | */ |
||
| 466 | 2 | public function getCookie($name) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Process Cookies to Header |
||
| 473 | * |
||
| 474 | * Set-Cookie: <name>=<value>[; <name>=<value>]... |
||
| 475 | * [; expires=<date>][; domain=<domain_name>] |
||
| 476 | * [; path=<some_path>][; secure][; httponly] |
||
| 477 | * |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | protected function sendCookies() |
||
| 486 | } |
||
| 487 |