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 |
||
33 | class Request |
||
34 | { |
||
35 | /** |
||
36 | * Available masks for cleaning variables |
||
37 | */ |
||
38 | const MASK_NO_TRIM = 1; |
||
39 | const MASK_ALLOW_RAW = 2; |
||
40 | const MASK_ALLOW_HTML = 4; |
||
41 | |||
42 | /** |
||
43 | * Gets the request method |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 2 | public static function getMethod() |
|
53 | |||
54 | /** |
||
55 | * Fetches and returns a given variable. |
||
56 | * |
||
57 | * The default behaviour is fetching variables depending on the |
||
58 | * current request method: GET and HEAD will result in returning |
||
59 | * an entry from $_GET, POST and PUT will result in returning an |
||
60 | * entry from $_POST. |
||
61 | * |
||
62 | * You can force the source by setting the $hash parameter: |
||
63 | * |
||
64 | * - post $_POST |
||
65 | * - get $_GET |
||
66 | * - files $_FILES |
||
67 | * - cookie $_COOKIE |
||
68 | * - env $_ENV |
||
69 | * - server $_SERVER |
||
70 | * - method via current $_SERVER['REQUEST_METHOD'] |
||
71 | * - default $_REQUEST |
||
72 | * |
||
73 | * @param string $name Variable name |
||
74 | * @param mixed $default Default value if the variable does not exist |
||
75 | * @param string $hash Source of variable value (POST, GET, FILES, COOKIE, METHOD) |
||
76 | * @param string $type Return type for the variable (INT, FLOAT, BOOLEAN, WORD, |
||
77 | * ALPHANUM, CMD, BASE64, STRING, ARRAY, PATH, NONE) For more |
||
78 | * information see FilterInput::clean(). |
||
79 | * @param int $mask Filter mask for the variable |
||
80 | * |
||
81 | * @return mixed Requested variable |
||
82 | */ |
||
83 | 3 | public static function getVar($name, $default = null, $hash = 'default', $type = 'none', $mask = 0) |
|
131 | |||
132 | /** |
||
133 | * Fetches and returns a given filtered variable. The integer |
||
134 | * filter will allow only digits to be returned. This is currently |
||
135 | * only a proxy function for getVar(). |
||
136 | * |
||
137 | * See getVar() for more in-depth documentation on the parameters. |
||
138 | * |
||
139 | * @param string $name Variable name |
||
140 | * @param int $default Default value if the variable does not exist |
||
141 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
142 | * |
||
143 | * @return int Requested variable |
||
144 | */ |
||
145 | 2 | public static function getInt($name, $default = 0, $hash = 'default') |
|
149 | |||
150 | /** |
||
151 | * Fetches and returns a given filtered variable. The float |
||
152 | * filter only allows digits and periods. This is currently |
||
153 | * only a proxy function for getVar(). |
||
154 | * |
||
155 | * See getVar() for more in-depth documentation on the parameters. |
||
156 | * |
||
157 | * @param string $name Variable name |
||
158 | * @param float $default Default value if the variable does not exist |
||
159 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
160 | * |
||
161 | * @return float Requested variable |
||
162 | */ |
||
163 | 2 | public static function getFloat($name, $default = 0.0, $hash = 'default') |
|
167 | |||
168 | /** |
||
169 | * Fetches and returns a given filtered variable. The bool |
||
170 | * filter will only return true/false bool values. This is |
||
171 | * currently only a proxy function for getVar(). |
||
172 | * |
||
173 | * See getVar() for more in-depth documentation on the parameters. |
||
174 | * |
||
175 | * @param string $name Variable name |
||
176 | * @param bool $default Default value if the variable does not exist |
||
177 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
178 | * |
||
179 | * @return bool Requested variable |
||
180 | */ |
||
181 | 2 | public static function getBool($name, $default = false, $hash = 'default') |
|
185 | |||
186 | /** |
||
187 | * Fetches and returns a given filtered variable. The word |
||
188 | * filter only allows the characters [A-Za-z_]. This is currently |
||
189 | * only a proxy function for getVar(). |
||
190 | * |
||
191 | * See getVar() for more in-depth documentation on the parameters. |
||
192 | * |
||
193 | * @param string $name Variable name |
||
194 | * @param string $default Default value if the variable does not exist |
||
195 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
196 | * |
||
197 | * @return string Requested variable |
||
198 | */ |
||
199 | 2 | public static function getWord($name, $default = '', $hash = 'default') |
|
203 | |||
204 | /** |
||
205 | * Fetches and returns a given filtered variable. The cmd filter only allows the characters |
||
206 | * [A-Za-z0-9.-_] and returns in lower case. This is currently a proxy function for getVar(). |
||
207 | * |
||
208 | * See getVar() for more in-depth documentation on the parameters. |
||
209 | * |
||
210 | * @param string $name Variable name |
||
211 | * @param string $default Default value if the variable does not exist |
||
212 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
213 | * |
||
214 | * @return string Requested variable |
||
215 | */ |
||
216 | 2 | public static function getCmd($name, $default = '', $hash = 'default') |
|
220 | |||
221 | /** |
||
222 | * Fetches and returns a given filtered variable. The string |
||
223 | * filter deletes 'bad' HTML code, if not overridden by the mask. |
||
224 | * This is currently only a proxy function for getVar(). |
||
225 | * |
||
226 | * See getVar() for more in-depth documentation on the parameters. |
||
227 | * |
||
228 | * @param string $name Variable name |
||
229 | * @param string $default Default value if the variable does not exist |
||
230 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
231 | * @param int $mask Filter mask for the variable |
||
232 | * |
||
233 | * @return string Requested variable |
||
234 | */ |
||
235 | 3 | public static function getString($name, $default = '', $hash = 'default', $mask = 0) |
|
240 | |||
241 | /** |
||
242 | * Fetches and returns an array |
||
243 | * |
||
244 | * @param string $name Variable name |
||
245 | * @param mixed $default Default value if the variable does not exist |
||
246 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | 2 | public static function getArray($name, $default = array(), $hash = 'default') |
|
254 | |||
255 | /** |
||
256 | * Fetches and returns raw text |
||
257 | * |
||
258 | * @param string $name Variable name |
||
259 | * @param string $default Default value if the variable does not exist |
||
260 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
261 | * |
||
262 | * @return string Requested variable |
||
263 | */ |
||
264 | 2 | public static function getText($name, $default = '', $hash = 'default') |
|
268 | |||
269 | /** |
||
270 | * Fetches and returns a web url |
||
271 | * |
||
272 | * @param string $name Variable name |
||
273 | * @param string $default Default value if the variable does not exist |
||
274 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
275 | * |
||
276 | * @return string Requested variable |
||
277 | */ |
||
278 | 1 | public static function getUrl($name, $default = '', $hash = 'default') |
|
282 | |||
283 | /** |
||
284 | * Fetches and returns a file (or web) path |
||
285 | * |
||
286 | * @param string $name Variable name |
||
287 | * @param string $default Default value if the variable does not exist |
||
288 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
289 | * |
||
290 | * @return string Requested variable |
||
291 | */ |
||
292 | 1 | public static function getPath($name, $default = '', $hash = 'default') |
|
296 | |||
297 | /** |
||
298 | * Fetches and returns an email address |
||
299 | * |
||
300 | * @param string $name Variable name |
||
301 | * @param string $default Default value if the variable does not exist |
||
302 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
303 | * |
||
304 | * @return string email address or default if invalid |
||
305 | */ |
||
306 | 1 | public static function getEmail($name, $default = '', $hash = 'default') |
|
311 | |||
312 | /** |
||
313 | * Fetches and returns an IP address |
||
314 | * |
||
315 | * @param string $name Variable name |
||
316 | * @param string $default Default value if the variable does not exist |
||
317 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
318 | * |
||
319 | * @return string IP address or default if invalid |
||
320 | */ |
||
321 | 2 | public static function getIP($name, $default = '', $hash = 'default') |
|
326 | |||
327 | /** |
||
328 | * Return a DateTime object from a Xoops\Form\DateSelect of Xoops\Form\DateTime field |
||
329 | * |
||
330 | * @param string $name Variable name |
||
331 | * @param mixed $default Default value if the variable does not exist |
||
332 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
333 | * |
||
334 | * @return \DateTime object |
||
335 | */ |
||
336 | 1 | public static function getDateTime($name, $default = null, $hash = 'default') |
|
350 | |||
351 | /** |
||
352 | * get request header |
||
353 | * |
||
354 | * @param string $headerName name of header to retrieve, case insensitive |
||
355 | * @param string|null $default default to return if named header is not found |
||
356 | * |
||
357 | * @return string header value or default if header was not found |
||
358 | */ |
||
359 | public static function getHeader($headerName, $default = '') |
||
387 | |||
388 | /** |
||
389 | * See if a variable exists in one of the request hashes |
||
390 | * |
||
391 | * @param string $name variable to look for |
||
392 | * @param string $hash hash to check |
||
393 | * |
||
394 | * @return boolean True if hash has an element 'name', otherwise false |
||
395 | */ |
||
396 | 1 | public static function hasVar($name, $hash = 'method') |
|
410 | |||
411 | /** |
||
412 | * Set a variable in one of the request variables |
||
413 | * |
||
414 | * @param string $name Name |
||
415 | * @param string $value Value |
||
416 | * @param string $hash Hash |
||
417 | * @param boolean $overwrite Boolean |
||
418 | * |
||
419 | * @return string Previous value |
||
420 | */ |
||
421 | 2 | public static function setVar($name, $value = null, $hash = 'method', $overwrite = true) |
|
470 | |||
471 | /** |
||
472 | * Fetches and returns a request array. |
||
473 | * |
||
474 | * The default behaviour is fetching variables depending on the |
||
475 | * current request method: GET and HEAD will result in returning |
||
476 | * $_GET, POST and PUT will result in returning $_POST. |
||
477 | * |
||
478 | * You can force the source by setting the $hash parameter: |
||
479 | * |
||
480 | * - post $_POST |
||
481 | * - get $_GET |
||
482 | * - files $_FILES |
||
483 | * - cookie $_COOKIE |
||
484 | * - env $_ENV |
||
485 | * - server $_SERVER |
||
486 | * - method via current $_SERVER['REQUEST_METHOD'] |
||
487 | * - default $_REQUEST |
||
488 | * |
||
489 | * @param string $hash to get (POST, GET, FILES, METHOD) |
||
490 | * @param int $mask Filter mask for the variable |
||
491 | * |
||
492 | * @return mixed Request hash |
||
493 | */ |
||
494 | 2 | public static function get($hash = 'default', $mask = 0) |
|
530 | |||
531 | /** |
||
532 | * Sets a request variable |
||
533 | * |
||
534 | * @param array $array An associative array of key-value pairs |
||
535 | * @param string $hash The request variable to set (POST, GET, FILES, METHOD) |
||
536 | * @param boolean $overwrite If true and an existing key is found, the value is overwritten, |
||
537 | * otherwise it is ignored |
||
538 | * |
||
539 | * @return void |
||
540 | */ |
||
541 | 2 | public static function set($array, $hash = 'method', $overwrite = true) |
|
547 | |||
548 | /** |
||
549 | * Clean up an input variable. |
||
550 | * |
||
551 | * @param mixed $var The input variable. |
||
552 | * @param int $mask Filter bit mask. |
||
553 | * - 1=no trim: If this flag is cleared and the input is a string, |
||
554 | * the string will have leading and trailing whitespace trimmed. |
||
555 | * - 2=allow_raw: If set, no more filtering is performed, higher bits are ignored. |
||
556 | * - 4=allow_html: HTML is allowed, but passed through a safe HTML filter first. |
||
557 | * If set, no more filtering is performed. |
||
558 | * - If no bits other than the 1 bit is set, a strict filter is applied. |
||
559 | * @param string $type The variable type. See {@link FilterInput::clean()}. |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | 1 | protected static function cleanVar($var, $mask = 0, $type = null) |
|
599 | |||
600 | /** |
||
601 | * Clean up an array of variables. |
||
602 | * |
||
603 | * @param mixed $var The input variable. |
||
604 | * @param int $mask Filter bit mask. See {@link Request::cleanVar()} |
||
605 | * @param string $type The variable type. See {@link FilterInput::clean()}. |
||
606 | * |
||
607 | * @return string |
||
608 | */ |
||
609 | protected static function cleanVars($var, $mask = 0, $type = null) |
||
621 | } |
||
622 |
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: