Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | final class Request |
||
40 | { |
||
41 | use ProxyTrait; |
||
42 | |||
43 | /** |
||
44 | * @const string HTTP content types |
||
45 | */ |
||
46 | public const TYPE_ANY = '*/*'; |
||
47 | public const TYPE_HTML = 'text/html'; |
||
48 | public const TYPE_JSON = 'application/json'; |
||
49 | |||
50 | /** |
||
51 | * @var array|null Accepted type |
||
52 | */ |
||
53 | static private $accept; |
||
54 | |||
55 | /** |
||
56 | * @var array|null Accepted languages |
||
57 | */ |
||
58 | static private $language; |
||
59 | |||
60 | /** |
||
61 | * Init instance |
||
62 | * |
||
63 | * @throws ComponentException |
||
64 | */ |
||
65 | private static function initInstance() |
||
69 | |||
70 | /** |
||
71 | * Retrieve a member of the $_GET super global |
||
72 | * |
||
73 | * If no $key is passed, returns the entire $_GET array. |
||
74 | * |
||
75 | * @param string $key |
||
76 | * @param string $default Default value to use if key not found |
||
77 | * |
||
78 | * @return string Returns null if key does not exist |
||
79 | */ |
||
80 | 75 | public static function getQuery($key = null, $default = null) |
|
84 | |||
85 | /** |
||
86 | * Retrieve a member of the $_POST super global |
||
87 | * |
||
88 | * If no $key is passed, returns the entire $_POST array. |
||
89 | * |
||
90 | * @param string $key |
||
91 | * @param string $default Default value to use if key not found |
||
92 | * |
||
93 | * @return string Returns null if key does not exist |
||
94 | */ |
||
95 | 65 | public static function getPost($key = null, $default = null) |
|
99 | |||
100 | /** |
||
101 | * Retrieve a member of the $_SERVER super global |
||
102 | * |
||
103 | * If no $key is passed, returns the entire $_SERVER array. |
||
104 | * |
||
105 | * @param string $key |
||
106 | * @param string $default Default value to use if key not found |
||
107 | * |
||
108 | * @return string Returns null if key does not exist |
||
109 | */ |
||
110 | 8 | public static function getServer($key = null, $default = null) |
|
114 | |||
115 | /** |
||
116 | * Retrieve a member of the $_COOKIE super global |
||
117 | * |
||
118 | * If no $key is passed, returns the entire $_COOKIE array. |
||
119 | * |
||
120 | * @param string $key |
||
121 | * @param string $default Default value to use if key not found |
||
122 | * |
||
123 | * @return string Returns null if key does not exist |
||
124 | */ |
||
125 | 1 | public static function getCookie($key = null, $default = null) |
|
129 | |||
130 | /** |
||
131 | * Retrieve a member of the $_ENV super global |
||
132 | * |
||
133 | * If no $key is passed, returns the entire $_ENV array. |
||
134 | * |
||
135 | * @param string $key |
||
136 | * @param string $default Default value to use if key not found |
||
137 | * |
||
138 | * @return string Returns null if key does not exist |
||
139 | */ |
||
140 | 1 | public static function getEnv($key = null, $default = null) |
|
144 | |||
145 | /** |
||
146 | * Search for a header value |
||
147 | * |
||
148 | * @param string $header |
||
149 | * @param mixed $default |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | 40 | public static function getHeader($header, $default = null) |
|
164 | |||
165 | /** |
||
166 | * Access values contained in the superglobals as public members |
||
167 | * Order of precedence: 1. GET, 2. POST |
||
168 | * |
||
169 | * @param string $key |
||
170 | * @param null $default |
||
171 | * |
||
172 | * @return string|null |
||
173 | * @link http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
||
174 | */ |
||
175 | 75 | public static function getParam($key, $default = null) |
|
182 | |||
183 | /** |
||
184 | * Get all params from GET and POST or PUT |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | 36 | public static function getParams() |
|
194 | |||
195 | /** |
||
196 | * Get uploaded file |
||
197 | * |
||
198 | * @param string $name |
||
199 | * |
||
200 | * @return \Zend\Diactoros\UploadedFile |
||
201 | */ |
||
202 | public static function getFile($name) |
||
206 | |||
207 | /** |
||
208 | * Get the client's IP address |
||
209 | * |
||
210 | * @param bool $checkProxy |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | 1 | public static function getClientIp($checkProxy = true) |
|
222 | |||
223 | /** |
||
224 | * Get module |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 44 | public static function getModule(): string |
|
232 | |||
233 | /** |
||
234 | * Get controller |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 44 | public static function getController(): string |
|
242 | |||
243 | /** |
||
244 | * Get method |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | 25 | public static function getMethod(): string |
|
252 | |||
253 | /** |
||
254 | * Get Accept MIME Type |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | 40 | public static function getAccept(): array |
|
266 | |||
267 | /** |
||
268 | * Get Accept MIME Type |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | public static function getAcceptLanguage(): array |
||
280 | |||
281 | /** |
||
282 | * parseAcceptHeader |
||
283 | * |
||
284 | * @param string $header |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | 40 | private static function parseAcceptHeader($header): array |
|
322 | |||
323 | /** |
||
324 | * Reset accept for tests |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | 847 | public static function resetAccept(): void |
|
332 | |||
333 | /** |
||
334 | * Check Accept header |
||
335 | * |
||
336 | * @param array $allowTypes |
||
337 | * |
||
338 | * @return string|false |
||
339 | */ |
||
340 | 40 | public static function checkAccept(array $allowTypes = []) |
|
360 | |||
361 | /** |
||
362 | * Check CLI |
||
363 | * |
||
364 | * @return bool |
||
365 | */ |
||
366 | 1 | public static function isCli(): bool |
|
370 | |||
371 | /** |
||
372 | * Check HTTP |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | 3 | public static function isHttp(): bool |
|
380 | |||
381 | /** |
||
382 | * Is this a GET method request? |
||
383 | * |
||
384 | * @return bool |
||
385 | */ |
||
386 | 2 | public static function isGet(): bool |
|
390 | |||
391 | /** |
||
392 | * Is this a POST method request? |
||
393 | * |
||
394 | * @return bool |
||
395 | */ |
||
396 | 2 | public static function isPost(): bool |
|
400 | |||
401 | /** |
||
402 | * Is this a PUT method request? |
||
403 | * |
||
404 | * @return bool |
||
405 | */ |
||
406 | 1 | public static function isPut(): bool |
|
410 | |||
411 | /** |
||
412 | * Is this a DELETE method request? |
||
413 | * |
||
414 | * @return bool |
||
415 | */ |
||
416 | 1 | public static function isDelete(): bool |
|
420 | |||
421 | /** |
||
422 | * Is the request a Javascript XMLHttpRequest? |
||
423 | * |
||
424 | * @return bool |
||
425 | */ |
||
426 | 9 | public static function isXmlHttpRequest(): bool |
|
430 | } |
||
431 |