Complex classes like ServerRequest 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 ServerRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ServerRequest extends Request implements ServerRequestInterface |
||
| 23 | { |
||
| 24 | /** @var array The server parameters. */ |
||
| 25 | private $serverParams; |
||
| 26 | |||
| 27 | /** @var array The cookie parameters. */ |
||
| 28 | private $cookieParams; |
||
| 29 | |||
| 30 | /** @var array The query parameters. */ |
||
| 31 | private $queryParams; |
||
| 32 | |||
| 33 | /** @var array The post parameters. */ |
||
| 34 | private $postParams; |
||
| 35 | |||
| 36 | /** @var array The files parameters. */ |
||
| 37 | private $filesParams; |
||
| 38 | |||
| 39 | /** @var array The uploaded files. */ |
||
| 40 | private $uploadedFiles; |
||
| 41 | |||
| 42 | /** @var null|array|object The parsed body. */ |
||
| 43 | private $parsedBody; |
||
| 44 | |||
| 45 | /** @var array The attributes. */ |
||
| 46 | private $attributes; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Construct a Request object with the given method, uri, version, headers & body. |
||
| 50 | * |
||
| 51 | * @global array $_SERVER The server parameters. |
||
| 52 | * @global array $_COOKIE The cookie parameters. |
||
| 53 | * @global array $_GET The query parameters. |
||
| 54 | * @global array $_POST The post parameters. |
||
| 55 | * @global array $_FILES The files parameters. |
||
| 56 | * |
||
| 57 | * @param string $method = '' |
||
| 58 | * @param UriInterface|null $uri = null |
||
| 59 | * @param string $version = self::DEFAULT_VERSION |
||
| 60 | * @param array $headers = [] |
||
| 61 | * @param StreamInterface|null $body = null |
||
| 62 | */ |
||
| 63 | 19 | public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Initialize the method. |
||
| 82 | * |
||
| 83 | * @param string $method |
||
| 84 | * @return string the method. |
||
| 85 | */ |
||
| 86 | 19 | private function initMethod($method) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Initialize the URI. |
||
| 93 | * |
||
| 94 | * @param UriInterface|null $uri |
||
| 95 | * @return UriInterface the URI. |
||
| 96 | */ |
||
| 97 | 19 | private function initUri($uri) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Initialize the headers. |
||
| 112 | * |
||
| 113 | * @param array $headers |
||
| 114 | * @return array the headers. |
||
| 115 | */ |
||
| 116 | 19 | private function initHeaders($headers) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Initialize the headers. |
||
| 123 | * |
||
| 124 | * @param string $serverParams |
||
| 125 | * @return array the headers. |
||
| 126 | */ |
||
| 127 | 19 | private function initQueryParams($serverParams) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Initialize the uploaded files. |
||
| 140 | * |
||
| 141 | * @param array $files |
||
| 142 | * @return array the uploaded files. |
||
| 143 | */ |
||
| 144 | 19 | private function initUploadedFiles(array $files) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Parse uploaded files. |
||
| 157 | * |
||
| 158 | * @param array $files |
||
| 159 | * @return UploadedFile|array uploaded files. |
||
| 160 | */ |
||
| 161 | 19 | private function parseUploadedFiles($files) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Parse single uploaded file. |
||
| 182 | * |
||
| 183 | * @param array $file |
||
| 184 | * @return UploadedFile single uploaded file. |
||
| 185 | */ |
||
| 186 | 19 | private function parseSingleUploadedFiles(array $file) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Parse multiple uploaded files. |
||
| 193 | * |
||
| 194 | * @param array $files |
||
| 195 | * @return UploadedFiles[] multiple uploaded files. |
||
| 196 | */ |
||
| 197 | 19 | private function parseMultipleUploadedFiles(array $files) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 19 | public function getServerParams() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | 1 | public function getCookieParams() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Set the cookie params. |
||
| 227 | * |
||
| 228 | * @param array $cookieParams |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | 1 | private function setCookieParams(array $cookieParams) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | 1 | public function withCookieParams(array $cookieParams) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | 3 | public function getQueryParams() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Set the query params. |
||
| 258 | * |
||
| 259 | * @param array $queryParams |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | 1 | private function setQueryParams(array $queryParams) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | 1 | public function withQueryParams(array $queryParams) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | 2 | public function getUploadedFiles() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Set the uploaded files. |
||
| 289 | * |
||
| 290 | * @param array $uploadedFiles |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | 1 | private function setUploadedFiles(array $uploadedFiles) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | 1 | public function withUploadedFiles(array $uploadedFiles) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | 6 | public function getParsedBody() |
|
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Checks if a content type header exists with the given content type. |
||
| 330 | * |
||
| 331 | * @param string $contentType |
||
| 332 | * @return bool true if a content type header exists with the given content type. |
||
| 333 | */ |
||
| 334 | 5 | private function hasContentType($contentType) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Set the parsed body. |
||
| 347 | * |
||
| 348 | * @param null|array|object $parsedBody |
||
| 349 | * @return $this |
||
| 350 | */ |
||
| 351 | 1 | private function setParsedBody($parsedBody) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * {@inheritdoc} |
||
| 360 | */ |
||
| 361 | 1 | public function withParsedBody($parsedBody) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | 1 | public function getAttributes() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | */ |
||
| 379 | 3 | public function getAttribute($name, $default = null) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Set the attribute. |
||
| 386 | * |
||
| 387 | * @param string $name |
||
| 388 | * @param mixed $value |
||
| 389 | * @return $this |
||
| 390 | */ |
||
| 391 | 1 | private function setAttribute($name, $value) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * {@inheritdoc} |
||
| 400 | */ |
||
| 401 | 1 | public function withAttribute($name, $value) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Remove the attribute. |
||
| 410 | * |
||
| 411 | * @param string $name |
||
| 412 | * @return $this |
||
| 413 | */ |
||
| 414 | 1 | private function removeAttribute($name) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | */ |
||
| 424 | 1 | public function withoutAttribute($name) |
|
| 430 | } |
||
| 431 |