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 = []) |
|
| 26 | /** |
||
| 27 | * Make the response cacheable. |
||
| 28 | * @param int|string $expires when should the request expire - either a timestamp or strtotime expression |
||
| 29 | * @return self |
||
| 30 | */ |
||
| 31 | 1 | public function cacheUntil($expires) |
|
| 41 | /** |
||
| 42 | * Prevent caching |
||
| 43 | * |
||
| 44 | * @return self |
||
| 45 | */ |
||
| 46 | public function noCache() |
||
| 58 | /** |
||
| 59 | * Set a cookie |
||
| 60 | * @param string $name the cookie name |
||
| 61 | * @param string $value the cookie value |
||
| 62 | * @param string $extra optional extra params for the cookie (semicolon delimited) |
||
| 63 | * @return self |
||
| 64 | */ |
||
| 65 | public function withCookie($name, $value, $extra = '') |
||
| 69 | /** |
||
| 70 | * Expires an existing cookie |
||
| 71 | * @param string $name the cookie name |
||
| 72 | * @param string $extra optional extra params for the cookie (semicolon delimited) |
||
| 73 | * @return self |
||
| 74 | */ |
||
| 75 | public function expireCookie($name, $extra = '') |
||
| 148 | |||
| 149 | public function withCallback(callable $callback = null) |
||
| 166 | } |
||
| 167 |