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 |
||
| 8 | class Response extends PSRResponse |
||
| 9 | { |
||
| 10 | protected $callback = null; |
||
| 11 | |||
| 12 | 1 | public function __construct($status = 200, string $body = null, array $headers = []) |
|
| 29 | /** |
||
| 30 | * Make the response cacheable. |
||
| 31 | * @param int|string $expires when should the request expire - either a timestamp or strtotime expression |
||
| 32 | * @return self |
||
| 33 | */ |
||
| 34 | 1 | public function cacheUntil($expires) |
|
| 44 | /** |
||
| 45 | * Prevent caching |
||
| 46 | * |
||
| 47 | * @return self |
||
| 48 | */ |
||
| 49 | public function noCache() |
||
| 61 | /** |
||
| 62 | * Set a cookie |
||
| 63 | * @param string $name the cookie name |
||
| 64 | * @param string $value the cookie value |
||
| 65 | * @param string $extra optional extra params for the cookie (semicolon delimited) |
||
| 66 | * @return self |
||
| 67 | */ |
||
| 68 | public function withCookie($name, $value, $extra = '') |
||
| 72 | /** |
||
| 73 | * Expires an existing cookie |
||
| 74 | * @param string $name the cookie name |
||
| 75 | * @param string $extra optional extra params for the cookie (semicolon delimited) |
||
| 76 | * @return self |
||
| 77 | */ |
||
| 78 | public function expireCookie($name, $extra = '') |
||
| 151 | |||
| 152 | public function withCallback(callable $callback = null) |
||
| 169 | } |
||
| 170 |