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 | 18 | 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 | 18 | private function initMethod($method) |
|
90 | |||
91 | /** |
||
92 | * Initialize the URI. |
||
93 | * |
||
94 | * @param UriInterface|null $uri |
||
95 | * @return UriInterface the URI. |
||
96 | */ |
||
97 | 18 | private function initUri($uri) |
|
109 | |||
110 | /** |
||
111 | * Initialize the headers. |
||
112 | * |
||
113 | * @param array $headers |
||
114 | * @return array the headers. |
||
115 | */ |
||
116 | 18 | private function initHeaders($headers) |
|
120 | |||
121 | /** |
||
122 | * Initialize the headers. |
||
123 | * |
||
124 | * @param string $serverParams |
||
125 | * @return array the headers. |
||
126 | */ |
||
127 | 18 | private function initQueryParams($serverParams) |
|
137 | |||
138 | /** |
||
139 | * Initialize the uploaded files. |
||
140 | * |
||
141 | * @param array $files |
||
142 | * @return array the uploaded files. |
||
143 | */ |
||
144 | 18 | private function initUploadedFiles(array $files) |
|
154 | |||
155 | /** |
||
156 | * Parse uploaded files. |
||
157 | * |
||
158 | * @param array $files |
||
159 | * @return UploadedFile|array uploaded files. |
||
160 | */ |
||
161 | 18 | private function parseUploadedFiles($files) |
|
179 | |||
180 | /** |
||
181 | * Parse single uploaded file. |
||
182 | * |
||
183 | * @param array $file |
||
184 | * @return UploadedFile single uploaded file. |
||
185 | */ |
||
186 | 18 | 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 | 18 | private function parseMultipleUploadedFiles(array $files) |
|
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 18 | 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 | * @throws \JsonException |
||
313 | */ |
||
314 | 5 | public function getParsedBody() |
|
327 | |||
328 | |||
329 | /** |
||
330 | * Checks if a content type header exists with the given content type. |
||
331 | * |
||
332 | * @param string $contentType |
||
333 | * @return bool true if a content type header exists with the given content type. |
||
334 | */ |
||
335 | 4 | private function hasContentType($contentType) |
|
345 | |||
346 | /** |
||
347 | * Set the parsed body. |
||
348 | * |
||
349 | * @param null|array|object $parsedBody |
||
350 | * @return $this |
||
351 | */ |
||
352 | 1 | private function setParsedBody($parsedBody) |
|
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | 1 | public function withParsedBody($parsedBody) |
|
368 | |||
369 | /** |
||
370 | * {@inheritdoc} |
||
371 | */ |
||
372 | 1 | public function getAttributes() |
|
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | 3 | public function getAttribute($name, $default = null) |
|
384 | |||
385 | /** |
||
386 | * Set the attribute. |
||
387 | * |
||
388 | * @param string $name |
||
389 | * @param mixed $value |
||
390 | * @return $this |
||
391 | */ |
||
392 | 1 | private function setAttribute($name, $value) |
|
398 | |||
399 | /** |
||
400 | * {@inheritdoc} |
||
401 | */ |
||
402 | 1 | public function withAttribute($name, $value) |
|
408 | |||
409 | /** |
||
410 | * Remove the attribute. |
||
411 | * |
||
412 | * @param string $name |
||
413 | * @return $this |
||
414 | */ |
||
415 | 1 | private function removeAttribute($name) |
|
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | 1 | public function withoutAttribute($name) |
|
431 | } |
||
432 |