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 |
||
| 25 | class ServerRequest extends Request implements ServerRequestInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $attributes = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $cookieParams = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var null|array|object |
||
| 39 | */ |
||
| 40 | private $parsedBody; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $queryParams = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $serverParams; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $uploadedFiles = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $method HTTP method |
||
| 59 | * @param string|UriInterface $uri URI |
||
| 60 | * @param array $headers Request headers |
||
| 61 | * @param string|null|resource|StreamInterface $body Request body |
||
| 62 | * @param string $version Protocol version |
||
| 63 | * @param array $serverParams Typically the $_SERVER superglobal |
||
| 64 | */ |
||
| 65 | public function __construct( |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Return an UploadedFile instance array. |
||
| 80 | * |
||
| 81 | * @param array $files A array which respect $_FILES structure |
||
| 82 | * @throws InvalidArgumentException for unrecognized values |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | public static function normalizeFiles(array $files) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Create and return an UploadedFile instance from a $_FILES specification. |
||
| 107 | * |
||
| 108 | * If the specification represents an array of values, this method will |
||
| 109 | * delegate to normalizeNestedFileSpec() and return that return value. |
||
| 110 | * |
||
| 111 | * @param array $value $_FILES struct |
||
| 112 | * @return array|UploadedFileInterface |
||
| 113 | */ |
||
| 114 | private static function createUploadedFileFromSpec(array $value) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Normalize an array of file specifications. |
||
| 131 | * |
||
| 132 | * Loops through all nested files and returns a normalized array of |
||
| 133 | * UploadedFileInterface instances. |
||
| 134 | * |
||
| 135 | * @param array $files |
||
| 136 | * @return UploadedFileInterface[] |
||
| 137 | */ |
||
| 138 | private static function normalizeNestedFileSpec(array $files = []) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return a ServerRequest populated with superglobals: |
||
| 158 | * $_GET |
||
| 159 | * $_POST |
||
| 160 | * $_COOKIE |
||
| 161 | * $_FILES |
||
| 162 | * $_SERVER |
||
| 163 | * |
||
| 164 | * @return ServerRequestInterface |
||
| 165 | */ |
||
| 166 | public static function fromGlobals() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get a Uri populated with values from $_SERVER. |
||
| 185 | * |
||
| 186 | * @return UriInterface |
||
| 187 | */ |
||
| 188 | public static function getUriFromGlobals() { |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | */ |
||
| 232 | public function getServerParams() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function getUploadedFiles() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | public function withUploadedFiles(array $uploadedFiles) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | public function getCookieParams() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function withCookieParams(array $cookies) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | public function getQueryParams() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | public function withQueryParams(array $query) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function getParsedBody() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function withParsedBody($data) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | public function getAttributes() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * {@inheritdoc} |
||
| 323 | */ |
||
| 324 | public function getAttribute($attribute, $default = null) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritdoc} |
||
| 335 | */ |
||
| 336 | public function withAttribute($attribute, $value) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritdoc} |
||
| 346 | */ |
||
| 347 | public function withoutAttribute($attribute) |
||
| 358 | } |
||
| 359 |