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 uploaded files. |
||
114 | * |
||
115 | * @param array $files |
||
116 | * @return array the uploaded files. |
||
117 | */ |
||
118 | 16 | private function initUploadedFiles(array $files) |
|
128 | |||
129 | /** |
||
130 | * Parse uploaded files. |
||
131 | * |
||
132 | * @param array $files |
||
133 | * @return UploadedFile|array uploaded files. |
||
134 | */ |
||
135 | 16 | private function parseUploadedFiles($files) |
|
153 | |||
154 | /** |
||
155 | * Parse single uploaded file. |
||
156 | * |
||
157 | * @param array $file |
||
158 | * @return UploadedFile single uploaded file. |
||
159 | */ |
||
160 | 16 | private function parseSingleUploadedFiles(array $file) |
|
164 | |||
165 | /** |
||
166 | * Parse multiple uploaded files. |
||
167 | * |
||
168 | * @param array $files |
||
169 | * @return UploadedFiles[] multiple uploaded files. |
||
170 | */ |
||
171 | 16 | private function parseMultipleUploadedFiles(array $files) |
|
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 16 | public function getServerParams() |
|
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | 1 | public function getCookieParams() |
|
198 | |||
199 | /** |
||
200 | * Set the cookie params. |
||
201 | * |
||
202 | * @param array $cookieParams |
||
203 | * @return $this |
||
204 | */ |
||
205 | 1 | private function setCookieParams(array $cookieParams) |
|
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | 1 | public function withCookieParams(array $cookieParams) |
|
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | 1 | public function getQueryParams() |
|
229 | |||
230 | /** |
||
231 | * Set the query params. |
||
232 | * |
||
233 | * @param array $queryParams |
||
234 | * @return $this |
||
235 | */ |
||
236 | 1 | private function setQueryParams(array $queryParams) |
|
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | 1 | public function withQueryParams(array $queryParams) |
|
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | 2 | public function getUploadedFiles() |
|
260 | |||
261 | /** |
||
262 | * Set the uploaded files. |
||
263 | * |
||
264 | * @param array $uploadedFiles |
||
265 | * @return $this |
||
266 | */ |
||
267 | 1 | private function setUploadedFiles(array $uploadedFiles) |
|
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | 1 | public function withUploadedFiles(array $uploadedFiles) |
|
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | 4 | public function getParsedBody() |
|
305 | |||
306 | /** |
||
307 | * Set the parsed body. |
||
308 | * |
||
309 | * @param null|array|object $parsedBody |
||
310 | * @return $this |
||
311 | */ |
||
312 | 1 | private function setParsedBody($parsedBody) |
|
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | 1 | public function withParsedBody($parsedBody) |
|
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | 1 | public function getAttributes() |
|
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | */ |
||
340 | 3 | public function getAttribute($name, $default = null) |
|
344 | |||
345 | /** |
||
346 | * Set the attribute. |
||
347 | * |
||
348 | * @param string $name |
||
349 | * @param mixed $value |
||
350 | * @return $this |
||
351 | */ |
||
352 | 1 | private function setAttribute($name, $value) |
|
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | 1 | public function withAttribute($name, $value) |
|
368 | |||
369 | /** |
||
370 | * Remove the attribute. |
||
371 | * |
||
372 | * @param string $name |
||
373 | * @return $this |
||
374 | */ |
||
375 | 1 | private function removeAttribute($name) |
|
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | */ |
||
385 | 1 | public function withoutAttribute($name) |
|
391 | } |
||
392 |
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: