Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Request extends Session implements RequestInterface |
||
| 12 | { |
||
| 13 | |||
| 14 | protected $server; |
||
| 15 | protected $method; |
||
| 16 | protected $contentType; |
||
| 17 | protected $isCli; |
||
| 18 | protected $params; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * headers list |
||
| 22 | * |
||
| 23 | * @var Headers |
||
| 24 | */ |
||
| 25 | protected $headerManager; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * instanciate |
||
| 29 | * |
||
| 30 | */ |
||
| 31 | 41 | public function __construct() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * returns header manager |
||
| 49 | * |
||
| 50 | * @return Headers |
||
| 51 | */ |
||
| 52 | 1 | public function getHeaderManager(): HeadersInterface |
|
| 56 | |||
| 57 | /** |
||
| 58 | * returns http method |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | 41 | public function getMethod(): string |
|
| 68 | |||
| 69 | /** |
||
| 70 | * returns http param for a given key |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | 3 | public function getParams(): array |
|
| 78 | |||
| 79 | /** |
||
| 80 | * returns http param for a given key |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | 1 | public function getParam(string $key): string |
|
| 90 | |||
| 91 | /** |
||
| 92 | * returns active route |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | 1 | public function getRoute(): string |
|
| 100 | |||
| 101 | /** |
||
| 102 | * return php script filename |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | 3 | public function getFilename(): string |
|
| 110 | |||
| 111 | /** |
||
| 112 | * return request uri |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | 1 | public function getUri(): string |
|
| 122 | |||
| 123 | /** |
||
| 124 | * return request host |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 1 | public function getHost(): string |
|
| 132 | |||
| 133 | /** |
||
| 134 | * return request client ip |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | 1 | public function getIp(): string |
|
| 142 | |||
| 143 | /** |
||
| 144 | * return request content type |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | 1 | public function getContentType(): string |
|
| 152 | |||
| 153 | /** |
||
| 154 | * return request accept encoding |
||
| 155 | * |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | 1 | public function getAcceptEncoding(): string |
|
| 165 | |||
| 166 | /** |
||
| 167 | * return request headers |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | 1 | public function getHeaders(): array |
|
| 175 | |||
| 176 | /** |
||
| 177 | * return request headers |
||
| 178 | * |
||
| 179 | * @return RequestInterface |
||
| 180 | */ |
||
| 181 | 1 | protected function setHeaders(): RequestInterface |
|
| 197 | |||
| 198 | /** |
||
| 199 | * build uri from cli args |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | 1 | protected function getArgs(): string |
|
| 209 | |||
| 210 | /** |
||
| 211 | * return server value for a given key |
||
| 212 | * |
||
| 213 | * @param string $key |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 1 | protected function getServer(string $key): string |
|
| 222 | |||
| 223 | /** |
||
| 224 | * isJsonAppContentType |
||
| 225 | * |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 1 | protected function isJsonContentType(): bool |
|
| 235 | |||
| 236 | /** |
||
| 237 | * getInput |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 2 | protected function getInput(): array |
|
| 255 | |||
| 256 | /** |
||
| 257 | * set method |
||
| 258 | * essentially for testing purposes |
||
| 259 | * |
||
| 260 | * @param string $method |
||
| 261 | * @return RequestInterface |
||
| 262 | */ |
||
| 263 | 2 | protected function setMethod(string $method): RequestInterface |
|
| 268 | |||
| 269 | /** |
||
| 270 | * set true if we are running from cli |
||
| 271 | * essentially for testing purposes |
||
| 272 | * |
||
| 273 | * @param boolean $isCli |
||
| 274 | * @return RequestInterface |
||
| 275 | */ |
||
| 276 | 1 | protected function setIsCli(bool $isCli): RequestInterface |
|
| 284 | |||
| 285 | /** |
||
| 286 | * return true id sapi mode |
||
| 287 | * essentially for testing purposes |
||
| 288 | * |
||
| 289 | * @return boolean |
||
| 290 | */ |
||
| 291 | 41 | public function isCli(): bool |
|
| 295 | |||
| 296 | /** |
||
| 297 | * set content type |
||
| 298 | * |
||
| 299 | * @param string $contentType |
||
| 300 | * @return RequestInterface |
||
| 301 | */ |
||
| 302 | 2 | protected function setContentType(string $contentType = ''): RequestInterface |
|
| 309 | |||
| 310 | /** |
||
| 311 | * get params in cli mode |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | 3 | protected function getCliParams(): array |
|
| 328 | |||
| 329 | /** |
||
| 330 | * set an entry in params for key value |
||
| 331 | * |
||
| 332 | * @param string $key |
||
| 333 | * @param string $value |
||
| 334 | * @return RequestInterface |
||
| 335 | */ |
||
| 336 | 1 | protected function setParam(string $key, string $value): RequestInterface |
|
| 341 | |||
| 342 | /** |
||
| 343 | * set http params |
||
| 344 | * |
||
| 345 | * @param array $params |
||
| 346 | * @return RequestInterface |
||
| 347 | */ |
||
| 348 | 1 | protected function setParams(array $params = []): RequestInterface |
|
| 367 | } |
||
| 368 |