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 | 19 | public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
80 | |||
81 | /** |
||
82 | * Initialize the method. |
||
83 | * |
||
84 | * @param string $method |
||
85 | * @return string the method. |
||
86 | */ |
||
87 | 19 | private function initMethod($method) |
|
91 | |||
92 | /** |
||
93 | * Initialize the URI. |
||
94 | * |
||
95 | * @param UriInterface|null $uri |
||
96 | * @return UriInterface the URI. |
||
97 | */ |
||
98 | 19 | private function initUri($uri) |
|
110 | |||
111 | /** |
||
112 | * Initialize the headers. |
||
113 | * |
||
114 | * @param array $headers |
||
115 | * @return array the headers. |
||
116 | */ |
||
117 | 19 | private function initHeaders($headers) |
|
121 | |||
122 | /** |
||
123 | * Initialize the headers. |
||
124 | * |
||
125 | * @param string $uri |
||
126 | * @return array the headers. |
||
127 | */ |
||
128 | 19 | private function initQueryParams($serverParams) |
|
138 | |||
139 | /** |
||
140 | * Initialize the uploaded files. |
||
141 | * |
||
142 | * @param array $files |
||
143 | * @return array the uploaded files. |
||
144 | */ |
||
145 | 19 | private function initUploadedFiles(array $files) |
|
155 | |||
156 | /** |
||
157 | * Parse uploaded files. |
||
158 | * |
||
159 | * @param array $files |
||
160 | * @return UploadedFile|array uploaded files. |
||
161 | */ |
||
162 | 19 | private function parseUploadedFiles($files) |
|
180 | |||
181 | /** |
||
182 | * Parse single uploaded file. |
||
183 | * |
||
184 | * @param array $file |
||
185 | * @return UploadedFile single uploaded file. |
||
186 | */ |
||
187 | 19 | private function parseSingleUploadedFiles(array $file) |
|
191 | |||
192 | /** |
||
193 | * Parse multiple uploaded files. |
||
194 | * |
||
195 | * @param array $files |
||
196 | * @return UploadedFiles[] multiple uploaded files. |
||
197 | */ |
||
198 | 19 | private function parseMultipleUploadedFiles(array $files) |
|
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | 19 | public function getServerParams() |
|
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | 1 | public function getCookieParams() |
|
225 | |||
226 | /** |
||
227 | * Set the cookie params. |
||
228 | * |
||
229 | * @param array $cookieParams |
||
230 | * @return $this |
||
231 | */ |
||
232 | 1 | private function setCookieParams(array $cookieParams) |
|
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | 1 | public function withCookieParams(array $cookieParams) |
|
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | 2 | public function getQueryParams() |
|
256 | |||
257 | /** |
||
258 | * Set the query params. |
||
259 | * |
||
260 | * @param array $queryParams |
||
261 | * @return $this |
||
262 | */ |
||
263 | 1 | private function setQueryParams(array $queryParams) |
|
269 | |||
270 | /** |
||
271 | * {@inheritdoc} |
||
272 | */ |
||
273 | 1 | public function withQueryParams(array $queryParams) |
|
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | 2 | public function getUploadedFiles() |
|
287 | |||
288 | /** |
||
289 | * Set the uploaded files. |
||
290 | * |
||
291 | * @param array $uploadedFiles |
||
292 | * @return $this |
||
293 | */ |
||
294 | 1 | private function setUploadedFiles(array $uploadedFiles) |
|
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | 1 | public function withUploadedFiles(array $uploadedFiles) |
|
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | 6 | public function getParsedBody() |
|
330 | |||
331 | /** |
||
332 | * Checks if a content type header exists with the given content type. |
||
333 | * |
||
334 | * @param string $contentType |
||
335 | * @return true if a content type header exists with the given content type. |
||
336 | */ |
||
337 | 5 | private function hasContentType($contentType) |
|
347 | |||
348 | /** |
||
349 | * Set the parsed body. |
||
350 | * |
||
351 | * @param null|array|object $parsedBody |
||
352 | * @return $this |
||
353 | */ |
||
354 | 1 | private function setParsedBody($parsedBody) |
|
360 | |||
361 | /** |
||
362 | * {@inheritdoc} |
||
363 | */ |
||
364 | 1 | public function withParsedBody($parsedBody) |
|
370 | |||
371 | /** |
||
372 | * {@inheritdoc} |
||
373 | */ |
||
374 | 1 | public function getAttributes() |
|
378 | |||
379 | /** |
||
380 | * {@inheritdoc} |
||
381 | */ |
||
382 | 3 | public function getAttribute($name, $default = null) |
|
386 | |||
387 | /** |
||
388 | * Set the attribute. |
||
389 | * |
||
390 | * @param string $name |
||
391 | * @param mixed $value |
||
392 | * @return $this |
||
393 | */ |
||
394 | 1 | private function setAttribute($name, $value) |
|
400 | |||
401 | /** |
||
402 | * {@inheritdoc} |
||
403 | */ |
||
404 | 1 | public function withAttribute($name, $value) |
|
410 | |||
411 | /** |
||
412 | * Remove the attribute. |
||
413 | * |
||
414 | * @param string $name |
||
415 | * @return $this |
||
416 | */ |
||
417 | 1 | private function removeAttribute($name) |
|
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | 1 | public function withoutAttribute($name) |
|
433 | } |
||
434 |
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: