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 | 17 | 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 | 17 | private function initMethod($method) |
|
92 | |||
93 | /** |
||
94 | * Initialize the URI. |
||
95 | * |
||
96 | * @param UriInterface|null $uri |
||
97 | * @return UriInterface the URI. |
||
98 | */ |
||
99 | 17 | private function initUri($uri) |
|
111 | |||
112 | /** |
||
113 | * Initialize the headers. |
||
114 | * |
||
115 | * @param array $headers |
||
116 | * @return array the headers. |
||
117 | */ |
||
118 | 17 | private function initHeaders($headers) |
|
122 | |||
123 | /** |
||
124 | * Initialize the headers. |
||
125 | * |
||
126 | * @param string $uri |
||
127 | * @return array the headers. |
||
128 | */ |
||
129 | 17 | private function initQueryParams($serverParams) |
|
139 | |||
140 | /** |
||
141 | * Initialize the uploaded files. |
||
142 | * |
||
143 | * @param array $files |
||
144 | * @return array the uploaded files. |
||
145 | */ |
||
146 | 17 | private function initUploadedFiles(array $files) |
|
156 | |||
157 | /** |
||
158 | * Parse uploaded files. |
||
159 | * |
||
160 | * @param array $files |
||
161 | * @return UploadedFile|array uploaded files. |
||
162 | */ |
||
163 | 17 | private function parseUploadedFiles($files) |
|
181 | |||
182 | /** |
||
183 | * Parse single uploaded file. |
||
184 | * |
||
185 | * @param array $file |
||
186 | * @return UploadedFile single uploaded file. |
||
187 | */ |
||
188 | 17 | private function parseSingleUploadedFiles(array $file) |
|
192 | |||
193 | /** |
||
194 | * Parse multiple uploaded files. |
||
195 | * |
||
196 | * @param array $files |
||
197 | * @return UploadedFiles[] multiple uploaded files. |
||
198 | */ |
||
199 | 17 | private function parseMultipleUploadedFiles(array $files) |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | 17 | public function getServerParams() |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 1 | public function getCookieParams() |
|
226 | |||
227 | /** |
||
228 | * Set the cookie params. |
||
229 | * |
||
230 | * @param array $cookieParams |
||
231 | * @return $this |
||
232 | */ |
||
233 | 1 | private function setCookieParams(array $cookieParams) |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | 1 | public function withCookieParams(array $cookieParams) |
|
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | 2 | public function getQueryParams() |
|
257 | |||
258 | /** |
||
259 | * Set the query params. |
||
260 | * |
||
261 | * @param array $queryParams |
||
262 | * @return $this |
||
263 | */ |
||
264 | 1 | private function setQueryParams(array $queryParams) |
|
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | 1 | public function withQueryParams(array $queryParams) |
|
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | 2 | public function getUploadedFiles() |
|
288 | |||
289 | /** |
||
290 | * Set the uploaded files. |
||
291 | * |
||
292 | * @param array $uploadedFiles |
||
293 | * @return $this |
||
294 | */ |
||
295 | 1 | private function setUploadedFiles(array $uploadedFiles) |
|
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | */ |
||
305 | 1 | public function withUploadedFiles(array $uploadedFiles) |
|
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | 4 | public function getParsedBody() |
|
333 | |||
334 | /** |
||
335 | * Set the parsed body. |
||
336 | * |
||
337 | * @param null|array|object $parsedBody |
||
338 | * @return $this |
||
339 | */ |
||
340 | 1 | private function setParsedBody($parsedBody) |
|
346 | |||
347 | /** |
||
348 | * {@inheritdoc} |
||
349 | */ |
||
350 | 1 | public function withParsedBody($parsedBody) |
|
356 | |||
357 | /** |
||
358 | * {@inheritdoc} |
||
359 | */ |
||
360 | 1 | public function getAttributes() |
|
364 | |||
365 | /** |
||
366 | * {@inheritdoc} |
||
367 | */ |
||
368 | 3 | public function getAttribute($name, $default = null) |
|
372 | |||
373 | /** |
||
374 | * Set the attribute. |
||
375 | * |
||
376 | * @param string $name |
||
377 | * @param mixed $value |
||
378 | * @return $this |
||
379 | */ |
||
380 | 1 | private function setAttribute($name, $value) |
|
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | 1 | public function withAttribute($name, $value) |
|
396 | |||
397 | /** |
||
398 | * Remove the attribute. |
||
399 | * |
||
400 | * @param string $name |
||
401 | * @return $this |
||
402 | */ |
||
403 | 1 | private function removeAttribute($name) |
|
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | 1 | public function withoutAttribute($name) |
|
419 | } |
||
420 |
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: