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 |
||
21 | class Request extends Base |
||
22 | { |
||
23 | /** |
||
24 | * Pointer to PHP environment variables. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $server; |
||
29 | private $get; |
||
30 | private $post; |
||
31 | private $files; |
||
32 | private $cookies; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param \Pimple\Container $container |
||
38 | * @param array $server |
||
39 | * @param array $get |
||
40 | * @param array $post |
||
41 | * @param array $files |
||
42 | * @param array $cookies |
||
43 | */ |
||
44 | public function __construct(Container $container, array $server = [], array $get = [], array $post = [], array $files = [], array $cookies = []) |
||
53 | |||
54 | /** |
||
55 | * Set GET parameters. |
||
56 | * |
||
57 | * @param array $params |
||
58 | */ |
||
59 | public function setParams(array $params) |
||
63 | |||
64 | /** |
||
65 | * Get query string string parameter. |
||
66 | * |
||
67 | * @param string $name Parameter name |
||
68 | * @param string $default_value Default value |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getStringParam($name, $default_value = '') |
||
76 | |||
77 | /** |
||
78 | * Get query string integer parameter. |
||
79 | * |
||
80 | * @param string $name Parameter name |
||
81 | * @param int $default_value Default value |
||
82 | * |
||
83 | * @return int |
||
84 | */ |
||
85 | public function getIntegerParam($name, $default_value = 0) |
||
89 | |||
90 | /** |
||
91 | * Get a form value. |
||
92 | * |
||
93 | * @param string $name Form field name |
||
94 | * |
||
95 | * @return string|null |
||
96 | */ |
||
97 | public function getValue($name) |
||
103 | |||
104 | /** |
||
105 | * Get form values and check for CSRF token. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getValues() |
||
117 | |||
118 | /** |
||
119 | * Check for CSRF token. |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | public function checkCSRFToken() |
||
129 | |||
130 | /** |
||
131 | * Get the raw body of the HTTP request. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getBody() |
||
139 | |||
140 | /** |
||
141 | * Get the Json request body. |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getJson() |
||
149 | |||
150 | /** |
||
151 | * Get the content of an uploaded file. |
||
152 | * |
||
153 | * @param string $name Form file name |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getFileContent($name) |
||
165 | |||
166 | /** |
||
167 | * Get the path of an uploaded file. |
||
168 | * |
||
169 | * @param string $name Form file name |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getFilePath($name) |
||
177 | |||
178 | /** |
||
179 | * Get info of an uploaded file. |
||
180 | * |
||
181 | * @param string $name Form file name |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public function getFileInfo($name) |
||
189 | |||
190 | /** |
||
191 | * Return HTTP method. |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function getMethod() |
||
199 | |||
200 | /** |
||
201 | * Return true if the HTTP request is sent with the POST method. |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function isPost() |
||
209 | |||
210 | /** |
||
211 | * Return true if the HTTP request is an Ajax request. |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function isAjax() |
||
219 | |||
220 | /** |
||
221 | * Check if the page is requested through HTTPS. |
||
222 | * |
||
223 | * Note: IIS return the value 'off' and other web servers an empty value when it's not HTTPS |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function isHTTPS() |
||
235 | |||
236 | /** |
||
237 | * Get cookie value. |
||
238 | * |
||
239 | * @param string $name |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getCookie($name) |
||
247 | |||
248 | /** |
||
249 | * Return a HTTP header value. |
||
250 | * |
||
251 | * @param string $name Header name |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getHeader($name) |
||
261 | |||
262 | /** |
||
263 | * Get remote user. |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | public function getRemoteUser() |
||
271 | |||
272 | /** |
||
273 | * Returns query string. |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | public function getQueryString() |
||
281 | |||
282 | /** |
||
283 | * Return URI. |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getUri() |
||
291 | |||
292 | /** |
||
293 | * Get the user agent. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | public function getUserAgent() |
||
301 | |||
302 | /** |
||
303 | * Get the IP address of the user. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getIpAddress() |
||
330 | |||
331 | /** |
||
332 | * Get start time. |
||
333 | * |
||
334 | * @return float |
||
335 | */ |
||
336 | public function getStartTime() |
||
340 | |||
341 | /** |
||
342 | * Get server variable. |
||
343 | * |
||
344 | * @param string $variable |
||
345 | * |
||
346 | * @return string |
||
347 | */ |
||
348 | public function getServerVariable($variable) |
||
352 | |||
353 | /** |
||
354 | * Check if the CSRF token from the URL is correct. |
||
355 | */ |
||
356 | protected function checkCSRFParam() |
||
366 | } |
||
367 |
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: