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 |
||
31 | class Request |
||
32 | { |
||
33 | /** |
||
34 | * Available masks for cleaning variables |
||
35 | */ |
||
36 | const MASK_NO_TRIM = 1; |
||
37 | const MASK_ALLOW_RAW = 2; |
||
38 | const MASK_ALLOW_HTML = 4; |
||
39 | |||
40 | /** |
||
41 | * Gets the request method |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public static function getMethod() |
||
51 | |||
52 | /** |
||
53 | * Fetches and returns a given variable. |
||
54 | * |
||
55 | * The default behaviour is fetching variables depending on the |
||
56 | * current request method: GET and HEAD will result in returning |
||
57 | * an entry from $_GET, POST and PUT will result in returning an |
||
58 | * entry from $_POST. |
||
59 | * |
||
60 | * You can force the source by setting the $hash parameter: |
||
61 | * |
||
62 | * - post $_POST |
||
63 | * - get $_GET |
||
64 | * - files $_FILES |
||
65 | * - cookie $_COOKIE |
||
66 | * - env $_ENV |
||
67 | * - server $_SERVER |
||
68 | * - method via current $_SERVER['REQUEST_METHOD'] |
||
69 | * - default $_REQUEST |
||
70 | * |
||
71 | * @param string $name Variable name |
||
72 | * @param mixed $default Default value if the variable does not exist |
||
73 | * @param string $hash Source of variable value (POST, GET, FILES, COOKIE, METHOD) |
||
74 | * @param string $type Return type for the variable (INT, FLOAT, BOOLEAN, WORD, |
||
75 | * ALPHANUM, CMD, BASE64, STRING, ARRAY, PATH, NONE) For more |
||
76 | * information see FilterInput::clean(). |
||
77 | * @param int $mask Filter mask for the variable |
||
78 | * |
||
79 | * @return mixed Requested variable |
||
80 | */ |
||
81 | public static function getVar($name, $default = null, $hash = 'default', $type = 'none', $mask = 0) |
||
134 | |||
135 | /** |
||
136 | * Fetches and returns a given filtered variable. The integer |
||
137 | * filter will allow only digits to be returned. This is currently |
||
138 | * only a proxy function for getVar(). |
||
139 | * |
||
140 | * See getVar() for more in-depth documentation on the parameters. |
||
141 | * |
||
142 | * @param string $name Variable name |
||
143 | * @param int $default Default value if the variable does not exist |
||
144 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
145 | * |
||
146 | * @return int Requested variable |
||
|
|||
147 | */ |
||
148 | public static function getInt($name, $default = 0, $hash = 'default') |
||
152 | |||
153 | /** |
||
154 | * Fetches and returns a given filtered variable. The float |
||
155 | * filter only allows digits and periods. This is currently |
||
156 | * only a proxy function for getVar(). |
||
157 | * |
||
158 | * See getVar() for more in-depth documentation on the parameters. |
||
159 | * |
||
160 | * @param string $name Variable name |
||
161 | * @param float $default Default value if the variable does not exist |
||
162 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
163 | * |
||
164 | * @return float Requested variable |
||
165 | */ |
||
166 | public static function getFloat($name, $default = 0.0, $hash = 'default') |
||
170 | |||
171 | /** |
||
172 | * Fetches and returns a given filtered variable. The bool |
||
173 | * filter will only return true/false bool values. This is |
||
174 | * currently only a proxy function for getVar(). |
||
175 | * |
||
176 | * See getVar() for more in-depth documentation on the parameters. |
||
177 | * |
||
178 | * @param string $name Variable name |
||
179 | * @param bool $default Default value if the variable does not exist |
||
180 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
181 | * |
||
182 | * @return bool Requested variable |
||
183 | */ |
||
184 | public static function getBool($name, $default = false, $hash = 'default') |
||
188 | |||
189 | /** |
||
190 | * Fetches and returns a given filtered variable. The word |
||
191 | * filter only allows the characters [A-Za-z_]. This is currently |
||
192 | * only a proxy function for getVar(). |
||
193 | * |
||
194 | * See getVar() for more in-depth documentation on the parameters. |
||
195 | * |
||
196 | * @param string $name Variable name |
||
197 | * @param string $default Default value if the variable does not exist |
||
198 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
199 | * |
||
200 | * @return string Requested variable |
||
201 | */ |
||
202 | public static function getWord($name, $default = '', $hash = 'default') |
||
206 | |||
207 | /** |
||
208 | * Fetches and returns a given filtered variable. The cmd filter only allows the characters |
||
209 | * [A-Za-z0-9.-_] and returns in lower case. This is currently a proxy function for getVar(). |
||
210 | * |
||
211 | * See getVar() for more in-depth documentation on the parameters. |
||
212 | * |
||
213 | * @param string $name Variable name |
||
214 | * @param string $default Default value if the variable does not exist |
||
215 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
216 | * |
||
217 | * @return string Requested variable |
||
218 | */ |
||
219 | public static function getCmd($name, $default = '', $hash = 'default') |
||
223 | |||
224 | /** |
||
225 | * Fetches and returns a given filtered variable. The string |
||
226 | * filter deletes 'bad' HTML code, if not overridden by the mask. |
||
227 | * This is currently only a proxy function for getVar(). |
||
228 | * |
||
229 | * See getVar() for more in-depth documentation on the parameters. |
||
230 | * |
||
231 | * @param string $name Variable name |
||
232 | * @param string $default Default value if the variable does not exist |
||
233 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
234 | * @param int $mask Filter mask for the variable |
||
235 | * |
||
236 | * @return string Requested variable |
||
237 | */ |
||
238 | public static function getString($name, $default = '', $hash = 'default', $mask = 0) |
||
243 | |||
244 | /** |
||
245 | * Fetches and returns an array |
||
246 | * |
||
247 | * @param string $name Variable name |
||
248 | * @param mixed $default Default value if the variable does not exist |
||
249 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | public static function getArray($name, $default = array(), $hash = 'default') |
||
257 | |||
258 | /** |
||
259 | * Fetches and returns raw text |
||
260 | * |
||
261 | * @param string $name Variable name |
||
262 | * @param string $default Default value if the variable does not exist |
||
263 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
264 | * |
||
265 | * @return string Requested variable |
||
266 | */ |
||
267 | public static function getText($name, $default = '', $hash = 'default') |
||
271 | |||
272 | /** |
||
273 | * Fetches and returns a web url |
||
274 | * |
||
275 | * @param string $name Variable name |
||
276 | * @param string $default Default value if the variable does not exist |
||
277 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
278 | * |
||
279 | * @return string Requested variable |
||
280 | */ |
||
281 | public static function getUrl($name, $default = '', $hash = 'default') |
||
285 | |||
286 | /** |
||
287 | * Fetches and returns a file (or web) path |
||
288 | * |
||
289 | * @param string $name Variable name |
||
290 | * @param string $default Default value if the variable does not exist |
||
291 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
292 | * |
||
293 | * @return string Requested variable |
||
294 | */ |
||
295 | public static function getPath($name, $default = '', $hash = 'default') |
||
299 | |||
300 | /** |
||
301 | * Fetches and returns an email address |
||
302 | * |
||
303 | * @param string $name Variable name |
||
304 | * @param string $default Default value if the variable does not exist |
||
305 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
306 | * |
||
307 | * @return string email address or default if invalid |
||
308 | */ |
||
309 | public static function getEmail($name, $default = '', $hash = 'default') |
||
314 | |||
315 | /** |
||
316 | * Fetches and returns an IP address |
||
317 | * |
||
318 | * @param string $name Variable name |
||
319 | * @param string $default Default value if the variable does not exist |
||
320 | * @param string $hash Where the var should come from (POST, GET, FILES, COOKIE, METHOD) |
||
321 | * |
||
322 | * @return string IP address or default if invalid |
||
323 | */ |
||
324 | public static function getIP($name, $default = '', $hash = 'default') |
||
329 | |||
330 | /** |
||
331 | * get request header |
||
332 | * |
||
333 | * @param string $headerName name of header to retrieve, case insensitive |
||
334 | * @param string|null $default default to return if named header is not found |
||
335 | * |
||
336 | * @return string header value or default if header was not found |
||
337 | */ |
||
338 | public static function getHeader($headerName, $default = '') |
||
366 | |||
367 | /** |
||
368 | * See if a variable exists in one of the request hashes |
||
369 | * |
||
370 | * @param string $name variable to look for |
||
371 | * @param string $hash hash to check |
||
372 | * |
||
373 | * @return boolean True if hash has an element 'name', otherwise false |
||
374 | */ |
||
375 | public static function hasVar($name, $hash = 'method') |
||
389 | |||
390 | /** |
||
391 | * Set a variable in one of the request variables |
||
392 | * |
||
393 | * @param string $name Name |
||
394 | * @param string $value Value |
||
395 | * @param string $hash Hash |
||
396 | * @param boolean $overwrite Boolean |
||
397 | * |
||
398 | * @return string Previous value |
||
399 | */ |
||
400 | public static function setVar($name, $value = null, $hash = 'method', $overwrite = true) |
||
449 | |||
450 | /** |
||
451 | * Fetches and returns a request array. |
||
452 | * |
||
453 | * The default behaviour is fetching variables depending on the |
||
454 | * current request method: GET and HEAD will result in returning |
||
455 | * $_GET, POST and PUT will result in returning $_POST. |
||
456 | * |
||
457 | * You can force the source by setting the $hash parameter: |
||
458 | * |
||
459 | * - post $_POST |
||
460 | * - get $_GET |
||
461 | * - files $_FILES |
||
462 | * - cookie $_COOKIE |
||
463 | * - env $_ENV |
||
464 | * - server $_SERVER |
||
465 | * - method via current $_SERVER['REQUEST_METHOD'] |
||
466 | * - default $_REQUEST |
||
467 | * |
||
468 | * @param string $hash to get (POST, GET, FILES, METHOD) |
||
469 | * @param int $mask Filter mask for the variable |
||
470 | * |
||
471 | * @return mixed Request hash |
||
472 | */ |
||
473 | public static function get($hash = 'default', $mask = 0) |
||
514 | |||
515 | /** |
||
516 | * Sets a request variable |
||
517 | * |
||
518 | * @param array $array An associative array of key-value pairs |
||
519 | * @param string $hash The request variable to set (POST, GET, FILES, METHOD) |
||
520 | * @param boolean $overwrite If true and an existing key is found, the value is overwritten, |
||
521 | * otherwise it is ignored |
||
522 | * |
||
523 | * @return void |
||
524 | */ |
||
525 | public static function set($array, $hash = 'method', $overwrite = true) |
||
531 | |||
532 | /** |
||
533 | * Clean up an input variable. |
||
534 | * |
||
535 | * @param mixed $var The input variable. |
||
536 | * @param int $mask Filter bit mask. |
||
537 | * - 1=no trim: If this flag is cleared and the input is a string, |
||
538 | * the string will have leading and trailing whitespace trimmed. |
||
539 | * - 2=allow_raw: If set, no more filtering is performed, higher bits are ignored. |
||
540 | * - 4=allow_html: HTML is allowed, but passed through a safe HTML filter first. |
||
541 | * If set, no more filtering is performed. |
||
542 | * - If no bits other than the 1 bit is set, a strict filter is applied. |
||
543 | * @param string $type The variable type. See {@link FilterInput::clean()}. |
||
544 | * |
||
545 | * @return string |
||
546 | */ |
||
547 | protected static function cleanVar($var, $mask = 0, $type = null) |
||
583 | |||
584 | /** |
||
585 | * Clean up an array of variables. |
||
586 | * |
||
587 | * @param mixed $var The input variable. |
||
588 | * @param int $mask Filter bit mask. See {@link Request::cleanVar()} |
||
589 | * @param string $type The variable type. See {@link FilterInput::clean()}. |
||
590 | * |
||
591 | * @return string |
||
592 | */ |
||
593 | protected static function cleanVars($var, $mask = 0, $type = null) |
||
605 | |||
606 | /** |
||
607 | * Strips slashes recursively on an array |
||
608 | * |
||
609 | * @param string|array $value string of Array of (nested arrays of) strings |
||
610 | * |
||
611 | * @return array The input array with stripslashes applied to it |
||
612 | */ |
||
613 | protected static function stripSlashesRecursive($value) |
||
621 | } |
||
622 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.