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 |
||
| 23 | class ServerRequest extends Request implements ServerRequestInterface |
||
| 24 | { |
||
| 25 | /** @var array The server parameters. */ |
||
| 26 | private $serverParams; |
||
| 27 | |||
| 28 | /** @var array The cookie parameters. */ |
||
| 29 | private $cookieParams; |
||
| 30 | |||
| 31 | /** @var array The query parameters. */ |
||
| 32 | private $queryParams; |
||
| 33 | |||
| 34 | /** @var array The post parameters. */ |
||
| 35 | private $postParams; |
||
| 36 | |||
| 37 | /** @var array The files parameters. */ |
||
| 38 | private $filesParams; |
||
| 39 | |||
| 40 | /** @var array The uploaded files. */ |
||
| 41 | private $uploadedFiles; |
||
| 42 | |||
| 43 | /** @var null|array|object The parsed body. */ |
||
| 44 | private $parsedBody; |
||
| 45 | |||
| 46 | /** @var array The attributes. */ |
||
| 47 | private $attributes; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Construct a Request object with the given method, uri, version, headers & body. |
||
| 51 | * |
||
| 52 | * @global array $_SERVER The server parameters. |
||
| 53 | * @global array $_COOKIE The cookie parameters. |
||
| 54 | * @global array $_GET The query parameters. |
||
| 55 | * @global array $_POST The post parameters. |
||
| 56 | * @global array $_FILES The files parameters. |
||
| 57 | * |
||
| 58 | * @param string $method = '' |
||
| 59 | * @param UriInterface|null $uri = null |
||
| 60 | * @param string $version = self::DEFAULT_VERSION |
||
| 61 | * @param array $headers = [] |
||
| 62 | * @param StreamInterface|null $body = null |
||
| 63 | */ |
||
| 64 | 16 | public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Initialize the method. |
||
| 84 | * |
||
| 85 | * @param string $method |
||
| 86 | * @return string the method. |
||
| 87 | */ |
||
| 88 | 16 | private function initMethod($method) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Initialize the URI. |
||
| 95 | * |
||
| 96 | * @param UriInterface|null $uri |
||
| 97 | * @return UriInterface the URI. |
||
| 98 | */ |
||
| 99 | 16 | private function initUri($uri) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Initialize the headers. |
||
| 114 | * |
||
| 115 | * @param array $headers |
||
| 116 | * @return array the headers. |
||
| 117 | */ |
||
| 118 | 16 | private function initHeaders($headers) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Initialize the uploaded files. |
||
| 125 | * |
||
| 126 | * @param array $files |
||
| 127 | * @return array the uploaded files. |
||
| 128 | */ |
||
| 129 | 16 | private function initUploadedFiles(array $files) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Parse uploaded files. |
||
| 142 | * |
||
| 143 | * @param array $files |
||
| 144 | * @return UploadedFile|array uploaded files. |
||
| 145 | */ |
||
| 146 | 16 | private function parseUploadedFiles($files) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Parse single uploaded file. |
||
| 167 | * |
||
| 168 | * @param array $file |
||
| 169 | * @return UploadedFile single uploaded file. |
||
| 170 | */ |
||
| 171 | 16 | private function parseSingleUploadedFiles(array $file) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Parse multiple uploaded files. |
||
| 178 | * |
||
| 179 | * @param array $files |
||
| 180 | * @return UploadedFiles[] multiple uploaded files. |
||
| 181 | */ |
||
| 182 | 16 | private function parseMultipleUploadedFiles(array $files) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | 16 | public function getServerParams() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | 1 | public function getCookieParams() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Set the cookie params. |
||
| 212 | * |
||
| 213 | * @param array $cookieParams |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | 1 | private function setCookieParams(array $cookieParams) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | 1 | public function withCookieParams(array $cookieParams) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | 1 | public function getQueryParams() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Set the query params. |
||
| 243 | * |
||
| 244 | * @param array $queryParams |
||
| 245 | * @return $this |
||
| 246 | */ |
||
| 247 | 1 | private function setQueryParams(array $queryParams) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | 1 | public function withQueryParams(array $queryParams) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | 2 | public function getUploadedFiles() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Set the uploaded files. |
||
| 274 | * |
||
| 275 | * @param array $uploadedFiles |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | 1 | private function setUploadedFiles(array $uploadedFiles) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | 1 | public function withUploadedFiles(array $uploadedFiles) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * {@inheritdoc} |
||
| 297 | */ |
||
| 298 | 4 | public function getParsedBody() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Set the parsed body. |
||
| 319 | * |
||
| 320 | * @param null|array|object $parsedBody |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | 1 | private function setParsedBody($parsedBody) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | 1 | public function withParsedBody($parsedBody) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | 1 | public function getAttributes() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | 3 | public function getAttribute($name, $default = null) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Set the attribute. |
||
| 358 | * |
||
| 359 | * @param string $name |
||
| 360 | * @param mixed $value |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | 1 | private function setAttribute($name, $value) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * {@inheritdoc} |
||
| 372 | */ |
||
| 373 | 1 | public function withAttribute($name, $value) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Remove the attribute. |
||
| 382 | * |
||
| 383 | * @param string $name |
||
| 384 | * @return $this |
||
| 385 | */ |
||
| 386 | 1 | private function removeAttribute($name) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * {@inheritdoc} |
||
| 395 | */ |
||
| 396 | 1 | public function withoutAttribute($name) |
|
| 402 | } |
||
| 403 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: