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() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set Response Type, one of JSON, HTML or CLI |
||
| 138 | * |
||
| 139 | * @param $type |
||
| 140 | */ |
||
| 141 | public function switchType($type) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Gets the HTTP protocol version as a string |
||
| 160 | * |
||
| 161 | * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
||
| 162 | * |
||
| 163 | * @return string HTTP protocol version. |
||
| 164 | */ |
||
| 165 | 1 | public function getProtocolVersion() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Gets the response Status-Code |
||
| 172 | * |
||
| 173 | * The Status-Code is a 3-digit integer result code of the server's attempt |
||
| 174 | * to understand and satisfy the request. |
||
| 175 | * |
||
| 176 | * @return integer status code. |
||
| 177 | */ |
||
| 178 | 4 | public function getStatusCode() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Sets the status code of this response |
||
| 185 | * |
||
| 186 | * @param integer $code the 3-digit integer result code to set. |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | 5 | public function setStatusCode($code) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Gets the response Reason-Phrase, a short textual description of the Status-Code |
||
| 196 | * |
||
| 197 | * Because a Reason-Phrase is not a required element in response |
||
| 198 | * Status-Line, the Reason-Phrase value MAY be null. Implementations MAY |
||
| 199 | * choose to return the default RFC 2616 recommended reason phrase for the |
||
| 200 | * response's Status-Code. |
||
| 201 | * |
||
| 202 | * @return string|null reason phrase, or null if unknown. |
||
| 203 | */ |
||
| 204 | 2 | public function getReasonPhrase() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Sets the Reason-Phrase of the response |
||
| 211 | * |
||
| 212 | * If no Reason-Phrase is specified, implementations MAY choose to default |
||
| 213 | * to the RFC 2616 recommended reason phrase for the response's Status-Code. |
||
| 214 | * |
||
| 215 | * @param string $phrase the Reason-Phrase to set. |
||
| 216 | */ |
||
| 217 | 1 | public function setReasonPhrase($phrase) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Retrieve a header by the given case-insensitive name as a string |
||
| 224 | * |
||
| 225 | * This method returns all of the header values of the given |
||
| 226 | * case-insensitive header name as a string concatenated together using |
||
| 227 | * a comma. |
||
| 228 | * |
||
| 229 | * @param string $header case-insensitive header name. |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 4 | public function getHeader($header) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Retrieves a header by the given case-insensitive name as an array of strings |
||
| 243 | * |
||
| 244 | * @param string $header Case-insensitive header name. |
||
| 245 | * @return string[] |
||
| 246 | */ |
||
| 247 | 1 | public function getHeaderAsArray($header) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Checks if a header exists by the given case-insensitive name |
||
| 258 | * |
||
| 259 | * @param string $header case-insensitive header name. |
||
| 260 | * @return bool returns true if any header names match the given header |
||
| 261 | * name using a case-insensitive string comparison. Returns false if |
||
| 262 | * no matching header name is found in the message. |
||
| 263 | */ |
||
| 264 | 3 | public function hasHeader($header) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Sets a header, replacing any existing values of any headers with the |
||
| 271 | * same case-insensitive name |
||
| 272 | * |
||
| 273 | * The header name is case-insensitive. The header values MUST be a string |
||
| 274 | * or an array of strings. |
||
| 275 | * |
||
| 276 | * @param string $header header name |
||
| 277 | * @param string|string[] $value header value(s) |
||
| 278 | * @return void |
||
| 279 | */ |
||
| 280 | 3 | public function setHeader($header, $value) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Appends a header value for the specified header |
||
| 287 | * |
||
| 288 | * Existing values for the specified header will be maintained. The new |
||
| 289 | * value will be appended to the existing list. |
||
| 290 | * |
||
| 291 | * @param string $header header name to add |
||
| 292 | * @param string $value value of the header |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | 1 | public function addHeader($header, $value) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Remove a specific header by case-insensitive name. |
||
| 306 | * |
||
| 307 | * @param string $header HTTP header to remove |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | 1 | public function removeHeader($header) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Gets all message headers |
||
| 317 | * |
||
| 318 | * The keys represent the header name as it will be sent over the wire, and |
||
| 319 | * each value is an array of strings associated with the header. |
||
| 320 | * |
||
| 321 | * // Represent the headers as a string |
||
| 322 | * foreach ($message->getHeaders() as $name => $values) { |
||
| 323 | * echo $name . ": " . implode(", ", $values); |
||
| 324 | * } |
||
| 325 | * |
||
| 326 | * @return array returns an associative array of the message's headers. |
||
| 327 | */ |
||
| 328 | 1 | public function getHeaders() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Sets headers, replacing any headers that have already been set on the message |
||
| 335 | * |
||
| 336 | * The array keys MUST be a string. The array values must be either a |
||
| 337 | * string or an array of strings. |
||
| 338 | * |
||
| 339 | * @param array $headers Headers to set. |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | 1 | public function setHeaders(array $headers) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Merges in an associative array of headers. |
||
| 349 | * |
||
| 350 | * Each array key MUST be a string representing the case-insensitive name |
||
| 351 | * of a header. Each value MUST be either a string or an array of strings. |
||
| 352 | * For each value, the value is appended to any existing header of the same |
||
| 353 | * name, or, if a header does not already exist by the given name, then the |
||
| 354 | * header is added. |
||
| 355 | * |
||
| 356 | * @param array $headers Associative array of headers to add to the message |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | 1 | public function addHeaders(array $headers) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Remove all headers |
||
| 366 | * |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | 6 | public function removeHeaders() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Set response body |
||
| 376 | * |
||
| 377 | * @param mixed $body |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | 6 | public function setBody($body) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get response body |
||
| 387 | * |
||
| 388 | * @return Controller|Layout |
||
| 389 | */ |
||
| 390 | 2 | public function getBody() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Clear response body |
||
| 397 | * |
||
| 398 | * @return void |
||
| 399 | */ |
||
| 400 | 5 | public function clearBody() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Set Cookie |
||
| 407 | * |
||
| 408 | * @param string $name |
||
| 409 | * @param string $value |
||
| 410 | * @param int|string|\DateTime $expire |
||
| 411 | * @param string $path |
||
| 412 | * @param string $domain |
||
| 413 | * @param bool $secure |
||
| 414 | * @param bool $httpOnly |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | 5 | public function setCookie( |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Get Cookie by name |
||
| 458 | * |
||
| 459 | * @param string $name |
||
| 460 | * @return array|null |
||
| 461 | */ |
||
| 462 | 2 | public function getCookie($name) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Process Cookies to Header |
||
| 469 | * |
||
| 470 | * Set-Cookie: <name>=<value>[; <name>=<value>]... |
||
| 471 | * [; expires=<date>][; domain=<domain_name>] |
||
| 472 | * [; path=<some_path>][; secure][; httponly] |
||
| 473 | * |
||
| 474 | * @return void |
||
| 475 | */ |
||
| 476 | protected function sendCookies() |
||
| 482 | } |
||
| 483 |