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 |
||
| 32 | class Response |
||
| 33 | { |
||
| 34 | use Options; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string HTTP protocol version |
||
| 38 | */ |
||
| 39 | protected $protocol = '1.1'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var integer response code equal to HTTP status codes |
||
| 43 | */ |
||
| 44 | protected $code = StatusCode::OK; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string|null HTTP Phrase |
||
| 48 | */ |
||
| 49 | protected $phrase; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array list of headers |
||
| 53 | */ |
||
| 54 | protected $headers = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array list of cookies |
||
| 58 | */ |
||
| 59 | protected $cookies = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Controller |
||
| 63 | */ |
||
| 64 | protected $body; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string CLI|HTML|JSON|FILE |
||
| 68 | */ |
||
| 69 | protected $type = 'HTML'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * send |
||
| 73 | * |
||
| 74 | * @throws NotAcceptableException |
||
| 75 | * @throws \InvalidArgumentException |
||
| 76 | */ |
||
| 77 | 1 | public function send(): void |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get response type |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 3 | public function getType(): string |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Set Response Type, one of JSON, HTML or CLI |
||
| 148 | * |
||
| 149 | * @param $type |
||
| 150 | */ |
||
| 151 | 3 | public function setType($type): void |
|
| 152 | { |
||
| 153 | // switch statement by content type |
||
| 154 | 3 | switch ($type) { |
|
| 155 | 3 | case 'JSON': |
|
| 156 | 2 | $this->setHeader('Content-Type', 'application/json'); |
|
| 157 | 2 | break; |
|
| 158 | 1 | case 'CLI': |
|
| 159 | 1 | case 'FILE': |
|
| 160 | case 'HTML': |
||
| 161 | default: |
||
| 162 | 1 | break; |
|
| 163 | } |
||
| 164 | |||
| 165 | 3 | $this->type = $type; |
|
| 166 | 3 | } |
|
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the HTTP protocol version as a string |
||
| 171 | * |
||
| 172 | * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
||
| 173 | * |
||
| 174 | * @return string HTTP protocol version. |
||
| 175 | */ |
||
| 176 | 1 | public function getProtocolVersion(): string |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the response Status-Code |
||
| 183 | * |
||
| 184 | * The Status-Code is a 3-digit integer result code of the server's attempt |
||
| 185 | * to understand and satisfy the request. |
||
| 186 | * |
||
| 187 | * @return integer status code. |
||
| 188 | */ |
||
| 189 | 5 | public function getStatusCode(): int |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Sets the status code of this response |
||
| 196 | * |
||
| 197 | * @param integer $code the 3-digit integer result code to set. |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | 5 | public function setStatusCode($code): void |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Gets the response Reason-Phrase, a short textual description of the Status-Code |
||
| 208 | * |
||
| 209 | * Because a Reason-Phrase is not a required element in response |
||
| 210 | * Status-Line, the Reason-Phrase value MAY be null. Implementations MAY |
||
| 211 | * choose to return the default RFC 2616 recommended reason phrase for the |
||
| 212 | * response's Status-Code. |
||
| 213 | * |
||
| 214 | * @return string|null reason phrase, or null if unknown. |
||
| 215 | */ |
||
| 216 | 2 | public function getReasonPhrase(): ?string |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Sets the Reason-Phrase of the response |
||
| 223 | * |
||
| 224 | * If no Reason-Phrase is specified, implementations MAY choose to default |
||
| 225 | * to the RFC 2616 recommended reason phrase for the response's Status-Code. |
||
| 226 | * |
||
| 227 | * @param string $phrase the Reason-Phrase to set. |
||
| 228 | */ |
||
| 229 | 1 | public function setReasonPhrase($phrase): void |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Retrieve a header by the given case-insensitive name as a string |
||
| 236 | * |
||
| 237 | * This method returns all of the header values of the given |
||
| 238 | * case-insensitive header name as a string concatenated together using |
||
| 239 | * a comma. |
||
| 240 | * |
||
| 241 | * @param string $header case-insensitive header name. |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | 15 | public function getHeader($header): string |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Retrieves a header by the given case-insensitive name as an array of strings |
||
| 255 | * |
||
| 256 | * @param string $header Case-insensitive header name. |
||
| 257 | * |
||
| 258 | * @return string[] |
||
| 259 | */ |
||
| 260 | 1 | public function getHeaderAsArray($header): array |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Checks if a header exists by the given case-insensitive name |
||
| 270 | * |
||
| 271 | * @param string $header case-insensitive header name. |
||
| 272 | * |
||
| 273 | * @return bool returns true if any header names match the given header |
||
| 274 | * name using a case-insensitive string comparison. Returns false if |
||
| 275 | * no matching header name is found in the message. |
||
| 276 | */ |
||
| 277 | 14 | public function hasHeader($header): bool |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Sets a header, replacing any existing values of any headers with the |
||
| 284 | * same case-insensitive name |
||
| 285 | * |
||
| 286 | * The header name is case-insensitive. The header values MUST be a string |
||
| 287 | * or an array of strings. |
||
| 288 | * |
||
| 289 | * @param string $header header name |
||
| 290 | * @param string|string[] $value header value(s) |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | 15 | public function setHeader($header, $value): void |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Appends a header value for the specified header |
||
| 301 | * |
||
| 302 | * Existing values for the specified header will be maintained. The new |
||
| 303 | * value will be appended to the existing list. |
||
| 304 | * |
||
| 305 | * @param string $header header name to add |
||
| 306 | * @param string $value value of the header |
||
| 307 | * |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | 1 | public function addHeader($header, $value): void |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Remove a specific header by case-insensitive name. |
||
| 321 | * |
||
| 322 | * @param string $header HTTP header to remove |
||
| 323 | * |
||
| 324 | * @return void |
||
| 325 | */ |
||
| 326 | 1 | public function removeHeader($header): void |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Gets all message headers |
||
| 333 | * |
||
| 334 | * The keys represent the header name as it will be sent over the wire, and |
||
| 335 | * each value is an array of strings associated with the header. |
||
| 336 | * |
||
| 337 | * // Represent the headers as a string |
||
| 338 | * foreach ($message->getHeaders() as $name => $values) { |
||
| 339 | * echo $name . ": " . implode(", ", $values); |
||
| 340 | * } |
||
| 341 | * |
||
| 342 | * @return array returns an associative array of the message's headers. |
||
| 343 | */ |
||
| 344 | 2 | public function getHeaders(): array |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Sets headers, replacing any headers that have already been set on the message |
||
| 351 | * |
||
| 352 | * The array keys MUST be a string. The array values must be either a |
||
| 353 | * string or an array of strings. |
||
| 354 | * |
||
| 355 | * @param array $headers Headers to set. |
||
| 356 | * |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | 1 | public function setHeaders(array $headers): void |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Merges in an associative array of headers. |
||
| 366 | * |
||
| 367 | * Each array key MUST be a string representing the case-insensitive name |
||
| 368 | * of a header. Each value MUST be either a string or an array of strings. |
||
| 369 | * For each value, the value is appended to any existing header of the same |
||
| 370 | * name, or, if a header does not already exist by the given name, then the |
||
| 371 | * header is added. |
||
| 372 | * |
||
| 373 | * @param array $headers Associative array of headers to add to the message |
||
| 374 | * |
||
| 375 | * @return void |
||
| 376 | */ |
||
| 377 | 1 | public function addHeaders(array $headers): void |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Remove all headers |
||
| 384 | * |
||
| 385 | * @return void |
||
| 386 | */ |
||
| 387 | 6 | public function removeHeaders(): void |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Set response body |
||
| 394 | * |
||
| 395 | * @param mixed $body |
||
| 396 | * |
||
| 397 | * @return void |
||
| 398 | */ |
||
| 399 | 9 | public function setBody($body): void |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Get response body |
||
| 406 | * |
||
| 407 | * @return Controller|Layout |
||
| 408 | */ |
||
| 409 | 3 | public function getBody() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Clear response body |
||
| 416 | * |
||
| 417 | * @return void |
||
| 418 | */ |
||
| 419 | 5 | public function clearBody(): void |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Set Cookie |
||
| 426 | * |
||
| 427 | * @param string $name |
||
| 428 | * @param string $value |
||
| 429 | * @param int|string|\DateTime $expire |
||
| 430 | * @param string $path |
||
| 431 | * @param string $domain |
||
| 432 | * @param bool $secure |
||
| 433 | * @param bool $httpOnly |
||
| 434 | * |
||
| 435 | * @return void |
||
| 436 | * @throws \InvalidArgumentException |
||
| 437 | */ |
||
| 438 | 5 | public function setCookie( |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Get Cookie by name |
||
| 479 | * |
||
| 480 | * @param string $name |
||
| 481 | * |
||
| 482 | * @return array|null |
||
| 483 | */ |
||
| 484 | 2 | public function getCookie($name): ?array |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Process Cookies to Header |
||
| 491 | * |
||
| 492 | * Set-Cookie: <name>=<value>[; <name>=<value>]... |
||
| 493 | * [; expires=<date>][; domain=<domain_name>] |
||
| 494 | * [; path=<some_path>][; secure][; httponly] |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | 1 | protected function sendCookies(): void |
|
| 504 | } |
||
| 505 |